-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
feat(gatsby): Allow printing type definitions to file (schema lock-down) #16291
Conversation
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.
This is great!
What is the lifecycle to lock the schema and then read it if it's available? At this moment it looks quite complicated, should there be a cli command or a plugin that handles that?
You would do it like this: const fs = require(`fs`)
const SAVED_SCHEMA = `my-typedefs.gql`
exports.createSchemaCustomization = ({ actions }) => {
if (fs.existsSync(SAVED_SCHEMA)) {
actions.createTypes(fs.readFileSync(SAVED_SCHEMA, { encoding: `utf-8` }))
} else {
actions.printTypeDefinitions({
path: SAVED_SCHEMA,
exclude: { types: [`TypeWeDontWant`] },
})
}
} I can see this put into a plugin that takes the file path, and include/exclude from |
@stefanprobst We probably need a separate action for loading schema like this from file. Imagine a situtation where you use such plugin, but then there is also In addition, all the types defined in this "schema-loading" plugin will have ownership set by this plugin. We should have a directive (that probably should only work in this action, not in |
|
It might get more relevant in future, so let's make sure it works correctly.
Will it merge or replace that?
I actually feel your initial intuition with plugin and action is a better long term solution. We can call actions in CLI too, after all. |
If the type definitions are loaded from file and added in
It will be merged.
I'm not yet very clear on what the added value of a plugin should be over the short snippet above. What do you think? |
It just feels like a nice reusable way use that. Add "gatsby-freeze-schema" plugin, have a schema that won't get changed. However we don't need to add it now.
OK, now I see it. You are correct. |
Thanks for merging!
I'll work on that next. I guess one other advantage of a plugin is that we get a full README to document all config options instead of hiding somewhere in the overlong schema customization docs :) |
@stefanprobst will this also be a cli command to print the schema to a file? |
@patrick91 first step is to add this as a plugin (#16561), if it's useful we could also add a cli command. wrt |
The main reason for creating that plugin was: I wanted squiggly lines in my editor when I made a typo in a query in my .js files. (ideally, also autocomplete, but that didn't happen) Since publishing that plugin, a version that does that got integrated into Gatsby iirc. From what I read here, the created PS: I haven't been keeping up with it and though I was (almost) the sole user of that plugin, this made my day 😄 |
Similar to @NickyMeuleman, I'm using graphql-codegen to generate types from the schema, and I want to do that on CI too :) |
Hey. Quick reply to share my use case. |
While the Schema Customization APIs allow defining types with the
createTypes
action and thus "lock down" a schema, it is cumbersome to do this manually for a lot of typedefs. It would be cool if we could save the current schema (defined+inferred types) to file, and then feed this back into Gatsby. This is what this PR enables.We don't save the whole schema, but try to include as little as necessary, i.e. derived types for filtering, sorting etc. are not included, neither are fieldtypes added in
setFieldsOnGraphQLNodeType
or anything else that can be recreated from the provided typedefs.By default, it does however include field types, inlined types, and interfaces. Configuration what to include/exclude is possible by typenames and by plugins. Types owned by
internal-data-bridge
, as well as built-in scalars,Internal
andNode
are excluded.Every type implementing the Node interface automatically gets the
dontInfer
directive, so inference can be skipped when feeding the typedefs back in.Fixes: #3344
Example:
To recreate the schema, just feed it into
createTypes
:The whole thing turned out to be more complicated than expected, because
graphql-js
's schema printer does not include directives. So this includes a modified version.Could use some cleanup, but posting this now to hopefully get some real-world testing and feedback. Cleanup especially wrt to the schema print stuff -- importing from
graphql-js
internals is no good - this has actually changed from v14.1=>v14.2