# 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**:
  ```graphql
  {
    fieldName
  }
  ```

- **Query with Arguments**:
  ```graphql
  {
    fieldName(argumentName: argumentValue) {
      subField
    }
  }
  ```

- **Alias**: Rename the result of a field.
  ```graphql
  {
    customName: fieldName {
      subField
    }
  }
  ```

- **Fragments**: Reuse parts of queries.
  ```graphql
  {
    ...fragmentName
  }

  fragment fragmentName on TypeName {
    field1
    field2
  }
  ```

#### Mutations

- **Basic Mutation**:
  ```graphql
  mutation {
    fieldName(input: { argumentName: argumentValue }) {
      subField
    }
  }
  ```

- **Mutation with Variables**:
  ```graphql
  mutation($input: InputType!) {
    fieldName(input: $input) {
      subField
    }
  }
  ```

  Variables:
  ```json
  {
    "input": {
      "argumentName": "argumentValue"
    }
  }
  ```

#### Subscriptions

- **Basic Subscription**:
  ```graphql
  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**:
  ```graphql
  type TypeName {
    field1: FieldType
    field2: FieldType
  }
  ```

- **Input Type**:
  ```graphql
  input InputTypeName {
    field1: FieldType
    field2: FieldType
  }
  ```

- **Enum Type**:
  ```graphql
  enum EnumName {
    VALUE1
    VALUE2
  }
  ```

- **Interface Type**:
  ```graphql
  interface InterfaceName {
    field: FieldType
  }
  ```

- **Union Type**:
  ```graphql
  union UnionName = Type1 | Type2
  ```

#### Directives

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

#### Schema Definition

- **Schema**:
  ```graphql
  schema {
    query: QueryType
    mutation: MutationType
    subscription: SubscriptionType
  }
  ```

- **Root Query Type**:
  ```graphql
  type Query {
    fieldName: FieldType
  }
  ```

- **Root Mutation Type**:
  ```graphql
  type Mutation {
    fieldName(input: InputType): ReturnType
  }
  ```

- **Root Subscription Type**:
  ```graphql
  type Subscription {
    fieldName: ReturnType
  }
  ```

#### Example Schema and Queries

- **Example Schema**:
  ```graphql
  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**:
  ```graphql
  {
    user(id: "1") {
      id
      name
      email
    }
  }
  ```

- **Example Mutation**:
  ```graphql
  mutation {
    createUser(input: { name: "Cloud Tuned", email: "theboss@cloudtuned.com" }) {
      id
      name
      email
    }
  }
  ```

- **Example Subscription**:
  ```graphql
  subscription {
    userCreated {
      id
      name
      email
    }
  }
  ```

#### Common Patterns

- **Pagination**:
  ```graphql
  {
    users(first: 10, after: "cursor") {
      edges {
        node {
          id
          name
        }
      }
      pageInfo {
        endCursor
        hasNextPage
      }
    }
  }
  ```

- **Error Handling**: Errors are returned in the `errors` field of the response.
  ```json
  {
    "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](https://graphql.org/learn/).
