-
Notifications
You must be signed in to change notification settings - Fork 20
[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?
[WIP] Work on using external modules #158
Conversation
❌ Deploy Preview for grats failed.
|
- [ ] reenable global validations | ||
- [ ] "modular" mode? like no full schema, but parts of schema but with full validation by resolving 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 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.
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 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.
- [ ] 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? | ||
- [ ] all tests to add fixtures for metadata/resolver map |
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.
TODO.md
Outdated
- [ ] 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? |
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.
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.
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 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.
import { | ||
SomeType as _SomeType, | ||
SomeInterface as _SomeInterface, | ||
} from "./nonGratsPackage.ignore"; |
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.
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.
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.
Will work in a separate PR, together with test fixes.
@captbaritone Could you approve the workflow for me? |
Done |
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.
Overall looks good! Left some observations/ideas inline, but the overall approach looks good!
src/Errors.ts
Outdated
} | ||
|
||
export function noModuleInGqlExternal() { | ||
return `Grats: @gqlExternal must include a module name in double quotes. For example: /** @gqlExternal "myModule" */`; |
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.
Diagnostics do not need to be prefixed with Grats:
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.
You do have it in tsConfigNotFound, is that a mistake too?
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.
Looking pretty good! Left some comments. What would you like to do next? Do you need this to merge to further validate, or should we try to work toward stabilizing it?
I think next steps for that would be:
- Documentation (both for the tag and for the overall use case probably as an additional subpage of this section.
- An example project in the examples directory. Could probably just copy/paste/tweak this one https://github.com/captbaritone/grats/tree/main/examples/incremental-migration
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 tried checking this out and trying to make use of it. Along the way I fell into a number of confusing edge cases which didn't have helpful error messages or validations. I've called them out in comments.
But, I was able to get it to work!
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 comment
The 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?
|
||
/** | ||
* @gqlType MyType | ||
* @gqlExternal |
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.
Should this error since there's no schema path specified? I don't see an error for this in the output below.
node.type.kind === ts.SyntaxKind.TypeReference && | ||
this.hasTag(node, EXTERNAL_TAG) |
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.
What is the user experience if they use @gqlExternal
but it's not a type reference? Does it just silently ignore the @gqlExternal
? If so, can we avoid that with a custom error message for that case?
// 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 && |
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.
What is the user experience if they use @gqlExternal
but it's not a type reference? Does it just silently ignore the @gqlExternal
? If so, can we avoid that with a custom error message for that case?
|
||
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 comment
The 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 @gqlExternal
on a non-type reference
const externalDocs: DocumentNode[] = []; | ||
const errors: ts.Diagnostic[] = []; | ||
for (const schemaPath of importedSchemas) { | ||
const text = fs.readFileSync(path.resolve(schemaPath), "utf-8"); |
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.
Can we handle the case where this path does not exist? Right now it just crashes Grats.
/Users/captbaritone/projects/grats/dist/src/transforms/addImportedSchemas.js:81
finally { if (e_2) throw e_2.error; }
^
Error: ENOENT: no such file or directory, open '/Users/captbaritone/projects/grats/examples/production-app/models/SomeExternalThing'
at Object.readFileSync (node:fs:453:20)
at addImportedSchemas (/Users/captbaritone/projects/grats/dist/src/transforms/addImportedSchemas.js:54:27)
at /Users/captbaritone/projects/grats/dist/src/lib.js:130:90
at ResultPipe.andThen (/Users/captbaritone/projects/grats/dist/src/utils/Result.js:78:35)
at /Users/captbaritone/projects/grats/dist/src/lib.js:130:14
at ResultPipe.andThen (/Users/captbaritone/projects/grats/dist/src/utils/Result.js:78:35)
at extractSchemaAndDoc (/Users/captbaritone/projects/grats/dist/src/lib.js:93:10)
at watchHost.afterProgramCreate (/Users/captbaritone/projects/grats/dist/src/cli.js:97:58)
at synchronizeProgram (/Users/captbaritone/projects/grats/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/typescript.js:131052:12)
at updateProgram (/Users/captbaritone/projects/grats/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/typescript.js:131244:9) {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/Users/captbaritone/projects/grats/examples/production-app/models/SomeExternalThing'
}
Node.js v20.10.0
ELIFECYCLE Command failed with exit code 1.
```
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.
Whatever error we show here would be a great palace to add context about how path resolution is done. So if they defined the right file but used the wrong relative path, we can help them fix it.
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.
Also interesting: This dependency on non-ts files is problematic for watch mode. Ideally we would rerun Grats if any of these external GraphQL files changed.
import { SomeUnion as _SomeUnion } from "./nonGratsPackage.ignore"; | ||
|
||
/** | ||
* @gqlType MyUnion |
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.
Shouldn't this need to be @gqlUnion
?
In general, it feels like a bug if I define a type here and then get an error "UnknownType MyUnion". I think we need to find a way to validate that the types marked as being external are actually defined in the referenced external schema and match the type kind mentioned here.
Maybe we can add an additional validation pass to do that?
.map((doc) => mergeExtensions(ctx, doc)) | ||
// Perform custom validations that reimplement spec validation rules | ||
// with more tailored error messages. | ||
.andThen((doc) => customSpecValidations(doc)) |
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've noticed that this will fail with external types since it notices the type has no fields. Can we move this below addImportedSchemas
?
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.
Hmm, actually even disabling this seems to trigger validation errors on line 135 saying the external type has no fields.
No description provided.