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

Conversation

freiksenet
Copy link

No description provided.

Copy link

netlify bot commented Dec 11, 2024

Deploy Preview for grats failed.

Name Link
🔨 Latest commit fda55a0
🔍 Latest deploy log https://app.netlify.com/sites/grats/deploys/676164129eaf5400086da19f

- [ ] 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
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.

- [ ] 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
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.

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?
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.

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.

@freiksenet
Copy link
Author

@captbaritone Could you approve the workflow for me?

@captbaritone
Copy link
Owner

@captbaritone Could you approve the workflow for me?

Done

Copy link
Owner

@captbaritone captbaritone left a 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" */`;
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?

src/Errors.ts Outdated Show resolved Hide resolved
src/Errors.ts Outdated Show resolved Hide resolved
src/Extractor.ts Outdated Show resolved Hide resolved
src/Extractor.ts Outdated Show resolved Hide resolved
src/transforms/addImportedSchemas.ts Outdated Show resolved Hide resolved
src/transforms/addImportedSchemas.ts Outdated Show resolved Hide resolved
/**
* Helper class for chaining together a series of `Result` operations that accepts also promises.
*/
export class ResultPipeAsync<T, E> {
Copy link
Owner

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?

Copy link
Author

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.

Copy link
Owner

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.

src/transforms/mergeExtensions.ts Show resolved Hide resolved
src/transforms/addImportedSchemas.ts Outdated Show resolved Hide resolved
Copy link
Owner

@captbaritone captbaritone left a 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:

  1. Documentation (both for the tag and for the overall use case probably as an additional subpage of this section.
  2. 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}\``;
Copy link
Owner

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.`;
Copy link
Owner

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;

Copy link
Owner

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,
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,

Comment on lines +253 to +254
}
if (
Copy link
Owner

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.

Suggested change
}
if (
} else if (

E.externalOnWrongNode(
ts
.getJSDocTags(node)
.filter((t) => t.tagName.text !== EXTERNAL_TAG)[0].tagName
Copy link
Owner

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);

//
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
//

for (const schemaPath of importedSchemas) {
const text = fs.readFileSync(path.resolve(schemaPath), "utf-8");
try {
const parsedAst = parse(text);
Copy link
Owner

Choose a reason for hiding this comment

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

I think we need to pass a Source object here:

Screenshot 2025-01-02 at 11 00 42 AM

/**
* Helper class for chaining together a series of `Result` operations that accepts also promises.
*/
export class ResultPipeAsync<T, E> {
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants