Skip to content

Latest commit

 

History

History
78 lines (65 loc) · 2.77 KB

graphql-query-language-breakdown.md

File metadata and controls

78 lines (65 loc) · 2.77 KB

Diagnose GraphQL query language

Breaking down a query

  • In a GraphQL service we define our schema and then the incoming requests will be validated against it.
  • Approaches in defining the schema:
    • All-in-One Definition: define the schema directly in your code.
    • Using Schema Definition Language (SDL): define schema in a separate file.
    • Schema inferred from code: you have resolvers annotated with decorators, then a lib will generate the schema based on your code.
    • Infer schema and resolver from the underlying database.

Jargons

Breakdown a schema type

Arguments and default value

GraphQL Object type
A type with some fields.
Field
Fields that can appear in any part of a GraphQL query that operates on the Character type.
List type
An array of the specified type.
Cannot define a non-empty list (an array with at least one member).
A type modifier.
Non-Null type
The GraphQL service promises to give you a value whenever you query this field.
A type modifier.
Argument
Arguments in GraphQL are passed by name specifically. Order does not matter!
All arguments are named.
Every field on a GraphQL Object type can have zero or more arguments.
Can be required or optional.

Tip

[type] VS [type]! VS [type!] VS [type!]!

Non-Null and list

The Query, Mutation, and Subscription types

Important

  • Every GraphQL schema must support query operations.
  • The entry point of a "root" operation type is a regular Object type called Query by default.

Mandatory Query object type in GraphQL

  • Query, Mutation, and Subscription are entry points of my GraphQL API.

    • Note that they should start with a capital letter and the same name as they are listed here.

    • If you want to use a custom name, you can do it. But then you need to tell GraphQL about it:

      type MySubscriptionType {
        todoStatusChange: Todo!
      }
      type MyMutationType {
        createTodo: Todo!
      }
      type MyQueryType {
        getTodos: [Todo]
      }
      schema {
        query: MyQueryType
        mutation: MyMutationType
        subscription: MySubscriptionType
      }

      Learn more about Subscription here