-
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
Remove schema updating from bootstrap #11131
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 looks reasonable and I want to get rid of extra schema creation step anyway. I do want someone else to have a second look.
People use the page schema for creating menus etc. So they'll add data via pageContext and then query the resulting schema. So we could remove support for this but it would be a breaking change. |
@KyleAMathews any examples in the wild where this feature is used? I want to think about possible workarounds. |
Even if there will be workarounds (and I'm sure there will be), we can't make breaking changes like that (at least during v2). Can you add some context on why you want to remove it? |
I don't know for sure what sites use this just that people talk about it. One example could be you add metadata to each page e.g. it's section and page title and then when generating a navigation, you query for pages within a section and sort by title. |
My point about workaround wasn't that we remove SitePage in general, but that we create in explicitly instead of inferring. The reason we generate schema twice is cause our schema is only generated if there is a node with such type available. If we add ability to create types by providing type def, we could always add SitePage to the schema and skip the extra schema regeneration. |
SitePage is actually already created in the first schema build step (and also includes all fields added with createNodeField), what is missing is anything that was added to page context in createPages. I was not aware that people used this other than from the query variables in page queries, thanks @KyleAMathews for the clarification! |
Right, so stuff is passed to page context and that is inferred as GraphQL types. Is there any reason to actually query pageContext from GraphQL? I know it's a breaking change, but I feel maybe we can change it to be a list of key-value pairs and thus eliminate the need of regenerating the schema. |
My main concern here is that we are getting a "bag of fields" type for that context, because practically every page (or realistically every page kind), will have their own context, but one would be able to query for all of those fields for any of the pages. It should either be an actual dict type (so list of key/val pairs) or a union of all possible page contexts. |
Ok, so this would need to wait to Gatsby 3 cause this is a breaking change. I'll summarize motivations here, it can become an RFC. Why is this important?The gatsby build structure can be divided into 4 steps.
Currently 3) can feed into 2) by defining additional page context. This means that we have a "cycle" in our logic, with pages optionally affecting the schema. Not only it's confusing directly (schema before 3 and after 3 is different), it also causes weird naming issues (all things being someGeneratedType_2 because we generated it twice). In addition this makes doing incremental and/or distributed builds harder, because schema is tied to page generation. Thus ideally we would have a directly data flow, with pages not affecting the schema. (well technically we have pages feed into the nodes when they are created, but we can consider it to be a special case). How to minimize changes?Currently you can query page context through all the possible fields every page context has. So if you added fields {
allSitePage {
edges {
node {
context {
foo
bar
}
}
}
}
} I propose to instead have page context in GraphQL be a list of {
allSitePage {
edges {
node {
context {
key
value
}
}
}
}
} This minimizes the changes that user have to make to adopt their website. |
Create RFC here gatsbyjs/rfcs#26 |
Closing this because of schema refactor. |
Currently we infer a GraphQL schema twice during bootstrap. This PR proposes to remove schema updating.
Reasoning
Schema updating was originally intended for debugging (#1268), and later in #2710 to solve a very specific problem (#2685): adding custom fields to
SitePage
nodes withcreateNodeField
, depending on whether apath
condition is met. Things to note:path
does not yet exist in the store when the schema is created, so there is nothing to infer.While infering the schema again after all pages have been created does solve this problem, the far less expensive option would have been to always create the field, but fill in the value conditionally. The following works without the need for schema updating:
Other than this usecase, is there any other reason for schema updating? Fields on page
context
would be missing, but they are available as query variables anyway.Issues
Because schema creation is not run twice, type name postfixes will differ. I tried to adjust the counting in
create-type-name
, which unfortunately did not work consistently. For a default starter, take a look at the schema diff here.