GraphQL

Write an awesome doc for GraphQL. A very nice an practical one extracted from GraphQL official documentation.

View on GitHub

Lifecycle of a request

[!NOTE]

You can see this YouTube video after you read this doc or before it to get the most out of them:

Eric Baer - GraphQL Under the Hood.

  1. Document parsing:
    • AKA Lexing and Parsing.
    • Transform the query (which is string) into something it understands.
  2. Validation using the type system: is it valid when checked against the API’s schema?
  3. Execution.
  4. Mirror the returned data from resolver into the requested shape.
    • GraphQL place each resolved field into a key-value map with the field name (or alias) as the key and the resolved value as the value.
    • It starts from leaves and works its way back up to the root (this why our responses resemble our requests).
    • Scalar coercion converts values into the types expected by the schema (e.g. enums will be coerced).
    • GraphQL waits for promises to be resolved before trying to construct the response (just look at my createUser resolver, I do NOT have await and it is working just fine).
  5. Return response:
    • GraphQL returns a JSON as a response for query operation which contains:
      • A data key:
        • This is mandated by GraphQL spec (ref).
        • May include partial data for the requested fields if errors were raised during the execution of some field resolvers (learn more here).
      • An error key:
        • Learn about different reasons that an error might occur here.
      • An extension key:
        • No restriction on what can goes inside it.
        • Spec only tell that it can be sent back as a top-level key.
        • Depends on the GraphQL implementation.
        • Can contain additional info about response, things like:
    • At least one of data or errors will be present on the response.