-
Notifications
You must be signed in to change notification settings - Fork 17
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
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
@@ -607,3 +613,17 @@ 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" */`; |
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?
/** | ||
* Helper class for chaining together a series of `Result` operations that accepts also promises. | ||
*/ | ||
export class ResultPipeAsync<T, E> { |
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.
Looks like maybe you were able to get away without needing this?
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 had to resort to readFileSync, which isn't ideal. Ideally all imports should be resolved asynchronously. I didn't get to fix the actual pipeline because it was more complicated.
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 readFileSync is fine for now. Let's remove this.
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
@@ -153,6 +155,10 @@ export function typeTagOnAliasOfNonObjectOrUnknown() { | |||
return `Expected \`@${TYPE_TAG}\` type to be an object type literal (\`{ }\`) or \`unknown\`. For example: \`type Foo = { bar: string }\` or \`type Query = unknown\`.`; | |||
} | |||
|
|||
export function nonExternalTypeAlias(tag: AllTags) { | |||
return `Expected \`@${tag}\` to be a type alias only if used with \`@${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.
The constraint here is just that it not be a runtime construct. Can we expand this to also support interfaces?
} | ||
|
||
export function externalOnWrongNode(existingTag: string) { | ||
return `Unexpected \`@${EXTERNAL_TAG}\` on type with \`${existingTag}\`. \`@${EXTERNAL_TAG}\` can only be used on type declarations.`; |
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.
"type declaration" here is a bit ambiguous. Does it mean a TypeScript type alias? A GraphQL object type
? Maybe it would be clearer (if a bit awkward) to just enumerate the six tags which are valid?
| typeof UNION_TAG | ||
| typeof INPUT_TAG | ||
| typeof 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.
Can we derive this from ALL_TAGS
?
const x = ["A", "B", "C"] as const;
type X = typeof x[number]
} | ||
|
||
// 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, |
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.
// 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, |
} | ||
if ( |
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.
If the feature is not enabled, I think it's better to not report errors related to the feature.
} | |
if ( | |
} else if ( |
E.externalOnWrongNode( | ||
ts | ||
.getJSDocTags(node) | ||
.filter((t) => t.tagName.text !== EXTERNAL_TAG)[0].tagName |
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 this would also report non-@gql
tags, like @deprecated
or other non-Grats tags.
@@ -164,7 +170,7 @@ function buildSchemaFromDoc( | |||
const diagnostics = validateSchema(schema) | |||
// FIXME: Handle case where query is not defined (no location) | |||
.filter((e) => e.source && e.locations && e.positions); | |||
|
|||
// |
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.
// |
for (const schemaPath of importedSchemas) { | ||
const text = fs.readFileSync(path.resolve(schemaPath), "utf-8"); | ||
try { | ||
const parsedAst = parse(text); |
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.
/** | ||
* Helper class for chaining together a series of `Result` operations that accepts also promises. | ||
*/ | ||
export class ResultPipeAsync<T, E> { |
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 readFileSync is fine for now. Let's remove this.
No description provided.