Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Work on using external modules #158

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- [x] SDL should extend type for external types - I guess marking types in SDL
- [ ] can't generate graphql-js stuff, don't want to do it for externs - don't support graphql-js for this?
- [ ] all imported types (so support interfaces etc)
- [ ] Read SDL to actually do validation
- [ ] reenable global validations
- [ ] "modular" mode? like no full schema, but parts of schema but with full validation by resolving it?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy pasting from chat:

So I'm realizing it's not rational to expect Grats to own emitting the final GraphQLSchema object if you have external types. Those types will have resolvers that Grats can't know about. So, I think if you use @gqlExternal we must assume that Grats is going to be producing a schema slice, and the consumer will be responsible for stiching it togehter with the other schemas. That could be done with resolver maps or graphql-tools schema merge.

In short, I think as soon as you use @gqlExternal you are implicitly opting into modular mode. We will validate against the full schema, but only emit SDL/resolver maps/GraphQLSchema for the part of the schema that Grats owns.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I have a setup that should work - you can only have gqlExternal in resolver mode and it does full validation, but only generates for local types.

- [ ] all tests to add fixtures for metadata/resolver map
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm onboard with this. Happy to have this as a separate PR if you want to merge that first.

- [ ] pluggable module resolution - too many variables there, use filepath by default, let users customize it
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory we only need this path in order to perform validation (is that right?). I wonder if there is a TypeScript API which will let us use the same (or most of) TypeScript's implementation of module resolution. I would need to avoid expecting the file to have a .js/.ts/.mjs/etc extension, but maybe something like that is exposed? I suspect that would be sufficient we could find something like that.

I did see this: https://github.com/microsoft/TypeScript/blob/6a00bd2422ffa46c13ac8ff81d7b4b1157e60ba7/src/server/project.ts#L484

Which could be a good place to start looking.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure any non-ts files are included in how the typescript resolves it. There is also the whole story of "lib" in package.json and stuff like this. Ultimately there is no "well-known" spot for the GraphQL files, so it feels like it all might just break weirdly. I will experiment on this ofc, but I feel that there might not be an easy solution.

4 changes: 4 additions & 0 deletions src/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,7 @@ export function noTypesDefined() {
export function tsConfigNotFound(cwd: string) {
return `Grats: Could not find \`tsconfig.json\` searching in ${cwd}.\n\nSee https://www.typescriptlang.org/download/ for instructors on how to add TypeScript to your project. Then run \`npx tsc --init\` to create a \`tsconfig.json\` file.`;
}

export function noModuleInGqlExternal() {
return `Grats: @gqlExternal must include a module name in double quotes. For example: /** @gqlExternal "myModule" */`;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Diagnostics do not need to be prefixed with Grats:

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do have it in tsConfigNotFound, is that a mistake too?

}
66 changes: 51 additions & 15 deletions src/Extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export const INFO_TAG = "gqlInfo";
export const IMPLEMENTS_TAG_DEPRECATED = "gqlImplements";
export const KILLS_PARENT_ON_EXCEPTION_TAG = "killsParentOnException";

export const EXTERNAL_TAG = "gqlExternal";

// All the tags that start with gql
export const ALL_TAGS = [
TYPE_TAG,
Expand All @@ -67,6 +69,7 @@ export const ALL_TAGS = [
ENUM_TAG,
UNION_TAG,
INPUT_TAG,
EXTERNAL_TAG,
];

const DEPRECATED_TAG = "deprecated";
Expand Down Expand Up @@ -131,12 +134,12 @@ class Extractor {
node: ts.DeclarationStatement,
name: NameNode,
kind: NameDefinition["kind"],
externalImportPath: string | null = null,
): void {
this.nameDefinitions.set(node, { name, kind });
this.nameDefinitions.set(node, { name, kind, externalImportPath });
}

// Traverse all nodes, checking each one for its JSDoc tags.
// If we find a tag we recognize, we extract the relevant information,
// Traverse all nodes, checking each one for its JSDoc tags. // If we find a tag we recognize, we extract the relevant information,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Traverse all nodes, checking each one for its JSDoc tags. // If we find a tag we recognize, we extract the relevant information,
// Traverse all nodes, checking each one for its JSDoc tags.
// If we find a tag we recognize, we extract the relevant information,

// reporting an error if it is attached to a node where that tag is not
// supported.
extract(sourceFile: ts.SourceFile): DiagnosticsResult<ExtractionSnapshot> {
Expand Down Expand Up @@ -254,6 +257,12 @@ class Extractor {
}
break;
}
case EXTERNAL_TAG:
if (!this.hasTag(node, TYPE_TAG)) {
this.report(tag.tagName, E.specifiedByOnWrongNode());
}
break;

default:
{
const lowerCaseTag = tag.tagName.text.toLowerCase();
Expand Down Expand Up @@ -980,6 +989,7 @@ class Extractor {
let interfaces: NamedTypeNode[] | null = null;

let hasTypeName = false;
let externalImportPath: string | null = null;

if (ts.isTypeLiteralNode(node.type)) {
this.validateOperationTypes(node.type, name.value);
Expand All @@ -990,24 +1000,33 @@ class Extractor {
// This is fine, we just don't know what it is. This should be the expected
// case for operation types such as `Query`, `Mutation`, and `Subscription`
// where there is not strong convention around.
} else if (
node.type.kind === ts.SyntaxKind.TypeReference &&
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason we can't also support external interfaces? I think you could define an interface which extends the interface from the other library.

this.hasTag(node, EXTERNAL_TAG)
) {
const externalTag = this.findTag(node, EXTERNAL_TAG) as ts.JSDocTag;
externalImportPath = this.externalModule(node, externalTag);
console.log("DEBUG - External import path", externalImportPath);
} else {
return this.report(node.type, E.typeTagOnAliasOfNonObjectOrUnknown());
}

const description = this.collectDescription(node);
this.recordTypeName(node, name, "TYPE");
this.recordTypeName(node, name, "TYPE", externalImportPath);

this.definitions.push(
this.gql.objectTypeDefinition(
node,
name,
fields,
interfaces,
description,
hasTypeName,
null,
),
);
if (!externalImportPath) {
freiksenet marked this conversation as resolved.
Show resolved Hide resolved
this.definitions.push(
this.gql.objectTypeDefinition(
node,
name,
fields,
interfaces,
description,
hasTypeName,
null,
),
);
}
}

checkForTypenameProperty(
Expand Down Expand Up @@ -1784,6 +1803,23 @@ class Extractor {
return this.gql.name(id, id.text);
}

externalModule(node: ts.Node, tag: ts.JSDocTag) {
let externalModule;
if (tag.comment != null) {
const commentText = ts.getTextOfJSDocComment(tag.comment);
if (commentText) {
const match = commentText.match(/^\s*"(.*)"\s*$/);
if (match && match[0]) {
externalModule = match[0];
}
}
}
if (!externalModule) {
return this.report(node, E.noModuleInGqlExternal());
}
return externalModule;
}

methodDeclaration(
node: ts.MethodDeclaration | ts.MethodSignature | ts.GetAccessorDeclaration,
): FieldDefinitionNode | null {
Expand Down
5 changes: 5 additions & 0 deletions src/GraphQLAstExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ declare module "graphql" {
exportName: string | null;
};
}

export interface UnionTypeDefinitionNode {
/**
* Grats metadata: Indicates that the type was materialized as part of
Expand All @@ -58,6 +59,10 @@ declare module "graphql" {
* or a type.
*/
mayBeInterface?: boolean;
/**
* Grats metadata: Indicates whether this extension is for external type
*/
isOnExternalType?: boolean;
freiksenet marked this conversation as resolved.
Show resolved Hide resolved
}

export interface FieldDefinitionNode {
Expand Down
20 changes: 18 additions & 2 deletions src/TypeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type NameDefinition = {
| "ENUM"
| "CONTEXT"
| "INFO";
externalImportPath: string | null;
};

type TsIdentifier = number;
Expand Down Expand Up @@ -61,7 +62,12 @@ export class TypeContext {
self._markUnresolvedType(node, typeName);
}
for (const [node, definition] of snapshot.nameDefinitions) {
self._recordTypeName(node, definition.name, definition.kind);
self._recordTypeName(
node,
definition.name,
definition.kind,
definition.externalImportPath,
);
}
return self;
}
Expand All @@ -76,9 +82,10 @@ export class TypeContext {
node: ts.Declaration,
name: NameNode,
kind: NameDefinition["kind"],
externalImportPath: string | null = null,
) {
this._idToDeclaration.set(name.tsIdentifier, node);
this._declarationToName.set(node, { name, kind });
this._declarationToName.set(node, { name, kind, externalImportPath });
}

// Record that a type references `node`
Expand All @@ -90,6 +97,15 @@ export class TypeContext {
return this._declarationToName.values();
}

getNameDefinition(name: NameNode): NameDefinition | null {
for (const def of this.allNameDefinitions()) {
if (def.name.value === name.value) {
return def;
}
}
return null;
}

findSymbolDeclaration(startSymbol: ts.Symbol): ts.Declaration | null {
const symbol = this.resolveSymbol(startSymbol);
const declaration = symbol.declarations?.[0];
Expand Down
25 changes: 13 additions & 12 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ export function extractSchemaAndDoc(
// `@killsParentOnException`.
.andThen((doc) => applyDefaultNullability(doc, config))
// Merge any `extend` definitions into their base definitions.
.map((doc) => mergeExtensions(doc))
.map((doc) => mergeExtensions(ctx, doc))
// Perform custom validations that reimplement spec validation rules
// with more tailored error messages.
// TODO
.andThen((doc) => customSpecValidations(doc))
// Sort the definitions in the document to ensure a stable output.
.map((doc) => sortSchemaAst(doc))
Expand Down Expand Up @@ -151,19 +152,19 @@ function buildSchemaFromDoc(
// (`String`, `Int`, etc). However, if we pass a second param (extending an
// existing schema) we do! So, we should find a way to validate that we don't
// shadow builtins.
const validationErrors = validateSDL(doc);
if (validationErrors.length > 0) {
return err(validationErrors.map(graphQlErrorToDiagnostic));
}
// const validationErrors = validateSDL(doc);
// if (validationErrors.length > 0) {
// return err(validationErrors.map(graphQlErrorToDiagnostic));
// }
const schema = buildASTSchema(doc, { assumeValidSDL: true });

const diagnostics = validateSchema(schema)
// FIXME: Handle case where query is not defined (no location)
.filter((e) => e.source && e.locations && e.positions);

if (diagnostics.length > 0) {
return err(diagnostics.map(graphQlErrorToDiagnostic));
}
// const diagnostics = validateSchema(schema)
// FIXME: Handle case where query is not defined (no location)
// .filter((e) => e.source && e.locations && e.positions);
//
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//

// if (diagnostics.length > 0) {
// return err(diagnostics.map(graphQlErrorToDiagnostic));
// }

return ok(schema);
}
Expand Down
10 changes: 6 additions & 4 deletions src/tests/TestRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ export default class TestRunner {
const filterRegex = filter != null ? new RegExp(filter) : null;
for (const fileName of readdirSyncRecursive(fixturesDir)) {
if (testFilePattern.test(fileName)) {
this._testFixtures.push(fileName);
const filePath = path.join(fixturesDir, fileName);
if (filterRegex != null && !filePath.match(filterRegex)) {
this._skip.add(fileName);
if (!(ignoreFilePattern && ignoreFilePattern.test(fileName))) {
this._testFixtures.push(fileName);
const filePath = path.join(fixturesDir, fileName);
if (filterRegex != null && !filePath.match(filterRegex)) {
this._skip.add(fileName);
}
}
} else if (!ignoreFilePattern || !ignoreFilePattern.test(fileName)) {
this._otherFiles.add(fileName);
Expand Down
7 changes: 7 additions & 0 deletions src/tests/fixtures/externals/nonGratsPackage.ignore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type SomeType = {
id: string;
};

export type SomeInterface = {
id: string;
};
19 changes: 19 additions & 0 deletions src/tests/fixtures/externals/nonGratsPackageWrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// { "EXPERIMENTAL__emitResolverMap": true }

import {
SomeType as _SomeType,
SomeInterface as _SomeInterface,
} from "./nonGratsPackage.ignore";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I'd like to add eventually is the ability for a fixture file to model multiple files. I did something similar for Relay:

Example: https://github.com/facebook/relay/blob/main/compiler/crates/relay-compiler/tests/relay_compiler_integration/fixtures/client_mutation_extension.input
Implemented here: https://github.com/facebook/relay/blob/main/compiler/crates/graphql-test-helpers/src/project_fixture.rs

I think that would help for tests like this which need to encode relationships between different files.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will work in a separate PR, together with test fixes.


/**
* @gqlType MyType
* @gqlExternal "./test-sdl.graphql"
*/
export type SomeType = _SomeType;

/**
* @gqlField
*/
export function someField(parent: SomeType): string {
return parent.id;
}
Empty file.
7 changes: 7 additions & 0 deletions src/tests/fixtures/externals/test-sdl.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type MyType {
id: ID!
}

interface MyInterface {
id: ID!
}
19 changes: 10 additions & 9 deletions src/tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ import {
validateGratsOptions,
} from "../gratsConfig";
import { SEMANTIC_NON_NULL_DIRECTIVE } from "../publicDirectives";
import { applySDLHeader, applyTypeScriptHeader } from "../printSchema";
import {
applySDLHeader,
applyTypeScriptHeader,
printExecutableSchema,
} from "../printSchema";
import { extend } from "../utils/helpers";

const TS_VERSION = ts.version;
Expand Down Expand Up @@ -77,7 +81,7 @@ const testDirs = [
{
fixturesDir,
testFilePattern: /\.ts$/,
ignoreFilePattern: null,
ignoreFilePattern: /\.ignore\.ts$/,
transformer: (code: string, fileName: string): string | false => {
const firstLine = code.split("\n")[0];
let config: Partial<GratsConfig> = {
Expand Down Expand Up @@ -136,14 +140,11 @@ const testDirs = [
const { schema, doc, resolvers } = schemaResult.value;

// We run codegen here just ensure that it doesn't throw.
const executableSchema = applyTypeScriptHeader(
const executableSchema = printExecutableSchema(
schema,
resolvers,
parsedOptions.raw.grats,
codegen(
schema,
resolvers,
parsedOptions.raw.grats,
`${fixturesDir}/${fileName}`,
),
`${fixturesDir}/${fileName}`,
);

const LOCATION_REGEX = /^\/\/ Locate: (.*)/;
Expand Down
5 changes: 4 additions & 1 deletion src/transforms/makeResolverSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export function makeResolverSignature(documentAst: DocumentNode): Metadata {
};

for (const declaration of documentAst.definitions) {
if (declaration.kind !== Kind.OBJECT_TYPE_DEFINITION) {
if (
declaration.kind !== Kind.OBJECT_TYPE_DEFINITION &&
declaration.kind !== Kind.OBJECT_TYPE_EXTENSION
) {
continue;
}
if (declaration.fields == null) {
Expand Down
17 changes: 14 additions & 3 deletions src/transforms/mergeExtensions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { DocumentNode, FieldDefinitionNode, visit } from "graphql";
import { extend } from "../utils/helpers";
import { TypeContext } from "../TypeContext";

/**
* Takes every example of `extend type Foo` and `extend interface Foo` and
* merges them into the original type/interface definition.
*
* Do not merge the ones that are external
*/
export function mergeExtensions(doc: DocumentNode): DocumentNode {
export function mergeExtensions(
ctx: TypeContext,
doc: DocumentNode,
): DocumentNode {
const fields = new MultiMap<string, FieldDefinitionNode>();

// Collect all the fields from the extensions and trim them from the AST.
Expand All @@ -14,8 +20,13 @@ export function mergeExtensions(doc: DocumentNode): DocumentNode {
if (t.directives != null || t.interfaces != null) {
throw new Error("Unexpected directives or interfaces on Extension");
}
fields.extend(t.name.value, t.fields);
return null;
const nameDef = ctx.getNameDefinition(t.name);
if (nameDef && nameDef.externalImportPath) {
return t;
freiksenet marked this conversation as resolved.
Show resolved Hide resolved
} else {
fields.extend(t.name.value, t.fields);
return null;
}
},
InterfaceTypeExtension(t) {
if (t.directives != null || t.interfaces != null) {
Expand Down