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

behind the scenes docs #7510

Merged
merged 49 commits into from
Sep 9, 2018
Merged

behind the scenes docs #7510

merged 49 commits into from
Sep 9, 2018

Conversation

Moocar
Copy link
Contributor

@Moocar Moocar commented Aug 21, 2018

I found myself having to relearn how the guts of Gatsby work every time I make a change. So I created some docs to stop myself having to learn from scratch each time.

I'm sure some of it could be moved into other parts of the docs (e.g ___NODE and frontmatter___date). But most isn't important for regular users. Just those looking to learn about how Gatsby works behind the scenes.

I've linked in quite a few places to lines of code on Github. I'm 50/50 on this approach. It's great to link from high level descriptions of the code straight to the code itself, but I'm worried these links will become stale. Open to suggestions here.

Lots more to document, but it's a start. Hopefully others knowledgeable with the code base can add to it over time.

I've also used the new gatsby-remark-graphviz to display graphs, which has the benefit of being able to source control graphs via a language vs an image drawing application.

@Moocar
Copy link
Contributor Author

Moocar commented Aug 27, 2018

Closing this PR as I've updated the docs significantly. Will reopen when they're pushed.

@Moocar Moocar closed this Aug 27, 2018
@Moocar Moocar reopened this Aug 28, 2018
@m-allanson m-allanson added type: documentation An issue or pull request for improving or updating Gatsby's documentation and removed type: documentation labels Aug 28, 2018
m-allanson
m-allanson previously approved these changes Sep 4, 2018
Copy link
Contributor

@m-allanson m-allanson left a comment

Choose a reason for hiding this comment

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

Woah this is great stuff @Moocar!

I started reviewing this but tbh it's mainly nitpicking. It probably makes more sense to merge this in, then followup with any changes if that sounds good to you?

title: Behind the Scenes
---

Curious how Gatsby works under the hood? This pages in this section describe how a Gatsby build works from an internal code/architecture point of view. It should be useful for anyone who needs to work on the internals of Gatsby, or for those who are simply curious how it all works, or perhaps you're a plugin author and need to understand how core works to track down a bug? Come one, come all!
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo

- This pages in this section
+ The pages in this section


During the schema generation phase, we must generate what's called a `ProcessedNodeType` in Gatsby. This is a simple structure that builds on top of a [graphql-js GraphQLObjectType](https://graphql.org/graphql-js/type/#graphqlobjecttype). Our goal in the below steps is to infer and construct this object for each unique node type in redux.

The flow is summarized by the below graph. It shows the intermediate transformations that are performed by code in the Gatsby [schema folder](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby/src/schema), finally resulting in the [ProcessedNoteType](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/schema/build-node-types.js#L182). It uses the example of building a `File` GraphQL type.
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo

- resulting in the [ProcessedNoteType]
+ resulting in the [ProcessedNodeType]

"resolve" [ label = "ProcessedNodeType.resolve()", shape = box ];
"gqlType" [ label = "gqlType (GraphQLObjectType)\l{\l fields,\l name: `File`\l}\l ", shape = box ];
"parentChild" [ label = "Parent/Children fields\lnode {\l childMarkdownRemark { html }\l parent { id }\l}\l ", shape = "box" ];
"objectFields" [ label = "Object node fields\l node {\l relativePath,\l accessTime }\l }\l ", shape = "box" ];
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like there's a spare closing bracket here:

screen shot 2018-09-04 at 16 28 18

digraph graphname {
"pluginFields" [ label = "custom plugin fields\l{\l publicURL: {\l type: GraphQLString,\l resolve(file, a, c) { ... }\l }\l}\l ", shape = box ];
"typeNodes" [ label = "all redux nodes of type\le.g internal.type === `File`", shape = "box" ];
"exampleValue" [ label = "exampleValue\l{\l relativePath: `bogs/my-blog.md`,\l accessTime: 8292387234\l}\l ", shape = "box" ];
Copy link
Contributor

Choose a reason for hiding this comment

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

less bogs, more blogs :)

- relativePath: `bogs/my
+ relativePath: `blogs/my

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My favorite typo


#### 2. Create a "GQLType"

This step is quite complex, but at its most basic, it infers GraphQL Fields by constructing an `exampleObject` that merges all fields of the type in Redux. It uses this to infer all possible fields and their types, and construct GraphQL versions of them. It does the same for fields created by plugins (like in step 2). This step is explained in detail in [GraphQL Node Types Creation](/docs/schema-gql-type).
Copy link
Contributor

Choose a reason for hiding this comment

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

update step number?

- (like in step 2)
+ (like in step 1)


#### Foreign Key reference (`___NODE`)

If not a mapping field, it might instead end in `___NODE`, signifying that its value is an ID that is a foreign key reference to another node in redux. Check out the [Create a Source Plugin](/docs/create-source-plugin/#create-source-plugin) for how this works from a user point of view. Behind the scenes, the field inference is handled by [inferFromFieldName](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/schema/infer-graphql-type.js#L204).
Copy link
Contributor

Choose a reason for hiding this comment

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

+ Check out the [Create a Source Plugin]
- Check out [Create a Source Plugin]

}
```

To resolve `file.childMarkdownRemark`, we filter over all the node's (that we're resolving) `children`, until we find one of type `markdownRemark`, which is returned. Remember that in redux, `children` is a collection of IDs. So as part of this, we lookup the node by ID in redux too.
Copy link
Contributor

Choose a reason for hiding this comment

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

- all the node's 
+ all the nodes 

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this one is correct as is, but the brackets between node's and children confuse it. I'll rearrange the sentence.


#### parent field

When a node is created as a child of some node, that fact is stored as a `parent` field on the redux node whose value is the ID of the parent. The parent GraphQL field resolver simply looks up the resolving node's `parent` field, and then looks up the parent in the redux `nodes` namespace map. It also creates a [page dependency](TODO have this in own section), as is done by child resolvers.
Copy link
Contributor

Choose a reason for hiding this comment

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

I found this paragraph quite confusing. I understood it as the parent field is added to the parent node, which doesn't seem right?


- field key is`elemMatch`? Change to `$elemMatch`. Recurse on value object
- field value is regex? Apply regex cleaning
- field value is globl, use [minimatch](https://www.npmjs.com/package/minimatch) library to convert to Regex
Copy link
Contributor

Choose a reason for hiding this comment

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

- globl
+ glob


The `resolve()` function calls `run-sift.js`, and provides it with the following arguments:

- GraphQLArgs (as js object). Within a filter. E.g `wordcount: { paragraphs: { eg: 4 } }`
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a few eg's instead of eq's following this:

- eg: 4
+ eq: 4

@Moocar
Copy link
Contributor Author

Moocar commented Sep 5, 2018

@m-allanson Sounds good! Yeah, reviewing 1300+ lines of documentation is no easy feat. Thanks for the initial pass. I agree we should just merge it and fix typos/clarifications etc on the fly.

m-allanson
m-allanson previously approved these changes Sep 5, 2018
Copy link
Contributor

@m-allanson m-allanson left a comment

Choose a reason for hiding this comment

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

👌

Copy link
Contributor

@calcsam calcsam left a comment

Choose a reason for hiding this comment

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

Per earlier conversation, I'll 👍 this so you can merge when you're ready @Moocar

@Moocar
Copy link
Contributor Author

Moocar commented Sep 6, 2018

@calcsam Thanks :). But looks like I might need elevated privileges to merge it.

@Moocar Moocar merged commit ca7d0ed into gatsbyjs:master Sep 9, 2018
@fk
Copy link
Contributor

fk commented Sep 10, 2018

🎉 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation An issue or pull request for improving or updating Gatsby's documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants