-
Notifications
You must be signed in to change notification settings - Fork 18
[WIP] Work on using external modules #158
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
base: main
Are you sure you want to change the base?
Changes from all commits
a995481
69bba95
8a1fa3b
a431599
d9acd95
08a8573
da25562
f312118
58fedca
fda55a0
1b801c5
f889bfd
78cb2c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
- [x] SDL should extend type for external types - I guess marking types in SDL | ||
- [x] can't generate graphql-js stuff, don't want to do it for externs - don't support graphql-js for this? | ||
- [x] all imported types (so support interfaces etc) | ||
- [x] Read SDL to actually do validation | ||
- [x] reenable global validations | ||
- [x] "modular" mode? like no full schema, but parts of schema but with full validation by resolving it? | ||
- [?] treat query/mutation/subscription as "import" type and extend it | ||
- [ ] all tests to add fixtures for metadata/resolver map | ||
- [ ] pluggable module resolution - too many variables there, use filepath by default, let users customize it | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
- [ ] first try ts project resolution | ||
- [ ] how to handle overimporting? Improting whole SDL module "infects" the schema with types that might not be requested. | ||
- [ ] another check on error handling - I think eg enums and scalars accept stuff they shouldn't accept? |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,8 @@ import { | |
InputValueDefinitionNodeOrResolverArg, | ||
ResolverArgument, | ||
} from "./resolverSignature"; | ||
import path = require("path"); | ||
import { GratsConfig } from "./gratsConfig"; | ||
import { Parser } from "graphql/language/parser"; | ||
|
||
export const LIBRARY_IMPORT_NAME = "grats"; | ||
|
@@ -71,6 +73,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, | ||
|
@@ -82,6 +86,17 @@ export const ALL_TAGS = [ | |
INPUT_TAG, | ||
DIRECTIVE_TAG, | ||
ANNOTATE_TAG, | ||
EXTERNAL_TAG, | ||
] as const; | ||
export type AllTags = (typeof ALL_TAGS)[number]; | ||
|
||
export const EXTERNAL_TAG_VALID_TAGS = [ | ||
TYPE_TAG, | ||
INPUT_TAG, | ||
INTERFACE_TAG, | ||
UNION_TAG, | ||
SCALAR_TAG, | ||
ENUM_TAG, | ||
]; | ||
|
||
const DEPRECATED_TAG = "deprecated"; | ||
|
@@ -120,8 +135,9 @@ type FieldTypeContext = { | |
*/ | ||
export function extract( | ||
sourceFile: ts.SourceFile, | ||
options: GratsConfig, | ||
): DiagnosticsResult<ExtractionSnapshot> { | ||
const extractor = new Extractor(); | ||
const extractor = new Extractor(options); | ||
return extractor.extract(sourceFile); | ||
} | ||
|
||
|
@@ -139,7 +155,7 @@ class Extractor { | |
errors: ts.DiagnosticWithLocation[] = []; | ||
gql: GraphQLConstructor; | ||
|
||
constructor() { | ||
constructor(private _options: GratsConfig) { | ||
this.gql = new GraphQLConstructor(); | ||
} | ||
|
||
|
@@ -151,8 +167,9 @@ 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. | ||
|
@@ -277,6 +294,26 @@ class Extractor { | |
], | ||
}); | ||
break; | ||
case EXTERNAL_TAG: | ||
if (!this._options.EXPERIMENTAL__emitResolverMap) { | ||
this.report(tag.tagName, E.externalNotInResolverMapMode()); | ||
} else if ( | ||
!EXTERNAL_TAG_VALID_TAGS.some((tag) => this.hasTag(node, tag)) | ||
) { | ||
this.report( | ||
tag.tagName, | ||
E.externalOnWrongNode( | ||
ts | ||
.getJSDocTags(node) | ||
.filter( | ||
(t) => | ||
t.tagName.text !== EXTERNAL_TAG && | ||
ALL_TAGS.includes(t.tagName.text as AllTags), | ||
)[0]?.tagName.text, | ||
), | ||
); | ||
} | ||
break; | ||
default: | ||
{ | ||
const lowerCaseTag = tag.tagName.text.toLowerCase(); | ||
|
@@ -557,6 +594,8 @@ class Extractor { | |
extractInterface(node: ts.Node, tag: ts.JSDocTag) { | ||
if (ts.isInterfaceDeclaration(node)) { | ||
this.interfaceInterfaceDeclaration(node, tag); | ||
} else if (ts.isTypeAliasDeclaration(node)) { | ||
this.interfaceTypeAliasDeclaration(node, tag); | ||
} else { | ||
this.report(tag, E.invalidInterfaceTagUsage()); | ||
} | ||
|
@@ -648,6 +687,9 @@ class Extractor { | |
types.push(this.unionMemberDeclaration(member)); | ||
} | ||
} else if (ts.isTypeReferenceNode(node.type)) { | ||
if (this.hasTag(node, EXTERNAL_TAG)) { | ||
return this.externalModule(node, name, "UNION"); | ||
} | ||
types.push(this.unionMemberDeclaration(node.type)); | ||
} else { | ||
return this.report(node, E.expectedUnionTypeNode()); | ||
|
@@ -1031,6 +1073,11 @@ class Extractor { | |
if (name == null) return null; | ||
|
||
const description = this.collectDescription(node); | ||
|
||
if (this.hasTag(node, EXTERNAL_TAG)) { | ||
return this.externalModule(node, name, "SCALAR"); | ||
} | ||
|
||
this.recordTypeName(node, name, "SCALAR"); | ||
|
||
// TODO: Can a scalar be deprecated? | ||
|
@@ -1047,32 +1094,43 @@ class Extractor { | |
if (name == null) return null; | ||
|
||
const description = this.collectDescription(node); | ||
this.recordTypeName(node, name, "INPUT_OBJECT"); | ||
|
||
let fields: InputValueDefinitionNode[] | null = null; | ||
if ( | ||
node.type.kind === ts.SyntaxKind.TypeReference && | ||
this.hasTag(node, EXTERNAL_TAG) | ||
Comment on lines
+1099
to
+1100
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the user experience if they use |
||
) { | ||
return this.externalModule(node, name, "INPUT_OBJECT"); | ||
} else { | ||
this.recordTypeName(node, name, "INPUT_OBJECT"); | ||
let fields: InputValueDefinitionNode[] | null = null; | ||
|
||
const directives = this.collectDirectives(node); | ||
if (ts.isUnionTypeNode(node.type)) { | ||
directives.push( | ||
this.gql.constDirective(node, this.gql.name(node.type, ONE_OF_TAG), []), | ||
); | ||
const directives = this.collectDirectives(node); | ||
if (ts.isUnionTypeNode(node.type)) { | ||
directives.push( | ||
this.gql.constDirective( | ||
node, | ||
this.gql.name(node.type, ONE_OF_TAG), | ||
[], | ||
), | ||
); | ||
|
||
fields = this.extractOneOfInputFields(node.type); | ||
} else { | ||
fields = this.collectInputFields(node); | ||
} | ||
fields = this.extractOneOfInputFields(node.type); | ||
} else { | ||
fields = this.collectInputFields(node); | ||
} | ||
|
||
if (fields == null) return; | ||
if (fields == null) return; | ||
|
||
this.definitions.push( | ||
this.gql.inputObjectTypeDefinition( | ||
node, | ||
name, | ||
fields, | ||
directives, | ||
description, | ||
), | ||
); | ||
this.definitions.push( | ||
this.gql.inputObjectTypeDefinition( | ||
node, | ||
name, | ||
fields, | ||
directives, | ||
description, | ||
), | ||
); | ||
} | ||
} | ||
|
||
inputInterfaceDeclaration(node: ts.InterfaceDeclaration, tag: ts.JSDocTag) { | ||
|
@@ -1334,6 +1392,11 @@ 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 && | ||
captbaritone marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the user experience if they use |
||
this.hasTag(node, EXTERNAL_TAG) | ||
) { | ||
return this.externalModule(node, name, "TYPE"); | ||
} else { | ||
return this.report(node.type, E.typeTagOnAliasOfNonObjectOrUnknown()); | ||
} | ||
|
@@ -1641,6 +1704,24 @@ class Extractor { | |
); | ||
} | ||
|
||
interfaceTypeAliasDeclaration( | ||
node: ts.TypeAliasDeclaration, | ||
tag: ts.JSDocTag, | ||
) { | ||
const name = this.entityName(node, tag); | ||
if (name == null || name.value == null) { | ||
return; | ||
} | ||
|
||
if ( | ||
node.type.kind === ts.SyntaxKind.TypeReference && | ||
this.hasTag(node, EXTERNAL_TAG) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. I think we need to handle the case where the user uses |
||
) { | ||
return this.externalModule(node, name, "INTERFACE"); | ||
} | ||
return this.report(node.type, E.nonExternalTypeAlias(INTERFACE_TAG)); | ||
} | ||
|
||
collectFields( | ||
members: ReadonlyArray<ts.ClassElement | ts.TypeElement>, | ||
): Array<FieldDefinitionNode> { | ||
|
@@ -1934,7 +2015,7 @@ class Extractor { | |
); | ||
} | ||
|
||
enumEnumDeclaration(node: ts.EnumDeclaration, tag: ts.JSDocTag): void { | ||
enumEnumDeclaration(node: ts.EnumDeclaration, tag: ts.JSDocTag) { | ||
const name = this.entityName(node, tag); | ||
if (name == null || name.value == null) { | ||
return; | ||
|
@@ -1952,15 +2033,16 @@ class Extractor { | |
); | ||
} | ||
|
||
enumTypeAliasDeclaration( | ||
node: ts.TypeAliasDeclaration, | ||
tag: ts.JSDocTag, | ||
): void { | ||
enumTypeAliasDeclaration(node: ts.TypeAliasDeclaration, tag: ts.JSDocTag) { | ||
const name = this.entityName(node, tag); | ||
if (name == null || name.value == null) { | ||
return; | ||
} | ||
|
||
if (this.hasTag(node, EXTERNAL_TAG)) { | ||
return this.externalModule(node, name, "ENUM"); | ||
} | ||
|
||
const values = this.enumTypeAliasVariants(node); | ||
if (values == null) return; | ||
|
||
|
@@ -2131,6 +2213,38 @@ class Extractor { | |
return this.gql.name(id, id.text); | ||
} | ||
|
||
externalModule( | ||
node: ts.DeclarationStatement, | ||
name: NameNode, | ||
kind: NameDefinition["kind"], | ||
) { | ||
const tag = this.findTag(node, EXTERNAL_TAG); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: In most places we check for the tag before calling this. Can we pass the external tag as an argument here and avoid this error case? |
||
if (!tag) { | ||
return this.report(node, E.noModuleInGqlExternal()); | ||
} | ||
|
||
let externalModule; | ||
if (tag.comment != null) { | ||
const commentText = ts.getTextOfJSDocComment(tag.comment); | ||
if (commentText) { | ||
const match = commentText.match(/^\s*"(.*)"\s*$/); | ||
|
||
if (match && match[1]) { | ||
externalModule = match[1]; | ||
} | ||
} | ||
} | ||
if (!externalModule) { | ||
return this.report(node, E.noModuleInGqlExternal()); | ||
} | ||
return this.recordTypeName( | ||
node, | ||
name, | ||
kind, | ||
path.resolve(path.dirname(node.getSourceFile().fileName), externalModule), | ||
); | ||
} | ||
|
||
methodDeclaration( | ||
node: ts.MethodDeclaration | ts.MethodSignature | ts.GetAccessorDeclaration, | ||
): FieldDefinitionNode | null { | ||
|
There was a problem hiding this comment.
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.