In order to understand how GraphQL works, we need to first have an understanding of what GraphQL is.
GraphQL has a large, quick moving community, which is great, but because of the size of the ecosystem and how fast it's growing, sometimes it's hard to understand what exactly GraphQL is and what you need (or don't need) to use it.
So, what is GraphQL?
You can use GraphQL without adding anything to your client. You can use
fetch() or anything else that can make a request to a GraphQL server. You don't need Apollo or Relay. Eric emphasizes that
A GraphQL request can be made in any way. It could come as a custom header, use protoBuf or even as a cookie!
The community has largely standardized on HTTP POST requests, but it's still up to the system to determine how to send GraphQL requests.
Once the request is made, there's a request lifecycle.
The first thing that happens is Lexical Analysis. Much like a grade-schooler learning to de-construct a sentence and identify nouns, verbs and pronouns, GraphQL's Lexer identifies the pieces of the GraphQL query.
Once the query has been parsed, it goes through a validation process to make sure the request is executable against the Schema.
This is a stage of the request that can be skipped.
Persisted Queries are pre-validated query strings stored on the server that can be executed by requesting a specific identifier connected to the intended query. Using persisted queries can reduce the upload time of requests, but can also allow for servers to bypass Lexing, Parsing and Validation and speed up execution as the queries are already known ahead of time.
While this might be right for some systems, Eric points out that this may not be the right option for everyone and may not even be possible in some server implementations.
First, GraphQL identifies all the operations. A GraphQL request can contain multiple operations.
Then it moves on to resolve each operation.
At this point, the selection set is executed.
A GraphQL server, when preparing a field of a given scalar type, must uphold the contract the scalar type describes, either by coercing the value or producing an error.
If the field in the selection set is a Scalar, GraphQL will resolve the field, otherwise it recurses until it resolves to a Scalar.
And finally, a field is resolved.