Cheat Sheet #day7 - GraphQL

Cheat Sheet #day7 - GraphQL

GraphQL Cheat Sheet

GraphQL is a query language for APIs that allows clients to request exactly the data they need, and nothing more. It enables efficient and flexible data fetching, making it a powerful tool for modern web development. Below is a comprehensive cheat sheet covering the basics of GraphQL syntax and operations.

Basic Syntax

  • Queries: Request data.
  • Mutations: Modify data.
  • Subscriptions: Real-time data updates.
  • Types: Define the shape of data.

Queries

  • Basic Query:

    {
      fieldName
    }
    
  • Query with Arguments:

    {
      fieldName(argumentName: argumentValue) {
        subField
      }
    }
    
  • Alias: Rename the result of a field.

    {
      customName: fieldName {
        subField
      }
    }
    
  • Fragments: Reuse parts of queries.

    {
      ...fragmentName
    }
    
    fragment fragmentName on TypeName {
      field1
      field2
    }
    

Mutations

  • Basic Mutation:

    mutation {
      fieldName(input: { argumentName: argumentValue }) {
        subField
      }
    }
    
  • Mutation with Variables:

    mutation($input: InputType!) {
      fieldName(input: $input) {
        subField
      }
    }
    

    Variables:

    {
      "input": {
        "argumentName": "argumentValue"
      }
    }
    

Subscriptions

  • Basic Subscription:
    subscription {
      fieldName {
        subField
      }
    }
    

Types

  • Scalar Types: Basic data types.

    • Int: Integer.
    • Float: Floating-point number.
    • String: Text.
    • Boolean: true or false.
    • ID: Unique identifier.
  • Object Type:

    type TypeName {
      field1: FieldType
      field2: FieldType
    }
    
  • Input Type:

    input InputTypeName {
      field1: FieldType
      field2: FieldType
    }
    
  • Enum Type:

    enum EnumName {
      VALUE1
      VALUE2
    }
    
  • Interface Type:

    interface InterfaceName {
      field: FieldType
    }
    
  • Union Type:

    union UnionName = Type1 | Type2
    

Directives

  • Include/Skip Fields Conditionally:
    query($includeField: Boolean!) {
      field @include(if: $includeField) {
        subField
      }
    }
    

Schema Definition

  • Schema:

    schema {
      query: QueryType
      mutation: MutationType
      subscription: SubscriptionType
    }
    
  • Root Query Type:

    type Query {
      fieldName: FieldType
    }
    
  • Root Mutation Type:

    type Mutation {
      fieldName(input: InputType): ReturnType
    }
    
  • Root Subscription Type:

    type Subscription {
      fieldName: ReturnType
    }
    

Example Schema and Queries

  • Example Schema:

    type Query {
      user(id: ID!): User
      users: [User]
    }
    
    type Mutation {
      createUser(input: CreateUserInput): User
    }
    
    type User {
      id: ID!
      name: String!
      email: String!
    }
    
    input CreateUserInput {
      name: String!
      email: String!
    }
    
  • Example Query:

    {
      user(id: "1") {
        id
        name
        email
      }
    }
    
  • Example Mutation:

    mutation {
      createUser(input: { name: "Cloud Tuned", email: "theboss@cloudtuned.com" }) {
        id
        name
        email
      }
    }
    
  • Example Subscription:

    subscription {
      userCreated {
        id
        name
        email
      }
    }
    

Common Patterns

  • Pagination:

    {
      users(first: 10, after: "cursor") {
        edges {
          node {
            id
            name
          }
        }
        pageInfo {
          endCursor
          hasNextPage
        }
      }
    }
    
  • Error Handling: Errors are returned in the errors field of the response.

    {
      "errors": [
        {
          "message": "Error message",
          "locations": [{ "line": 2, "column": 3 }],
          "path": ["fieldName"]
        }
      ]
    }
    

This cheat sheet provides a quick reference to essential GraphQL concepts and syntax, helping you build and query your GraphQL APIs efficiently. For more detailed information, refer to the GraphQL documentation.

Did you find this article valuable?

Support Cloud Tuned by becoming a sponsor. Any amount is appreciated!