Catalog / GraphQL API Cheatsheet
GraphQL API Cheatsheet
A comprehensive cheatsheet covering GraphQL syntax, queries, mutations, schema definition, and best practices for designing and implementing GraphQL APIs.
GraphQL Basics
Core Concepts
GraphQL: A query language for your API and a server-side runtime for executing queries by using a type system you define for your data. |
Schema: The backbone of any GraphQL API. It defines the structure of the data, including the types, fields, and relationships. |
Query: Used to request data from the GraphQL API. Queries specify exactly what data the client needs, and nothing more. |
Mutation: Used to modify data on the server. Mutations can create, update, or delete data. |
Resolver: A function attached to a field in the GraphQL schema. It fetches the data for that field. |
GraphQL vs REST
GraphQL |
REST |
Single endpoint. |
Multiple endpoints. |
Client specifies the data required. |
Server defines the data returned. |
Strongly typed schema. |
Loosely defined data structures. |
Efficient data fetching (no over-fetching or under-fetching). |
Potential for over-fetching and under-fetching. |
GraphQL Schema Definition Language (SDL)
Defining Types
Use SDL to define the structure and types of your data.
|
Scalars: Basic data types like Non-Null: Use Lists: Use |
Queries and Mutations in Schema
Define entry points for querying and mutating data.
|
Interfaces and Unions
Interface: |
Defines a set of fields that concrete types must implement.
|
Union: |
Defines a set of possible types a field can return.
|
GraphQL Queries
Basic Query Structure
A GraphQL query specifies what data to fetch.
|
The query selects the |
Arguments
Pass arguments to fields to filter or modify the results.
|
The query fetches the 10 most recently created posts. |
Aliases
Use aliases to rename fields in the response, especially when querying the same field with different arguments.
|
This query fetches both the 5 most recent posts and the 3 most liked posts, each with their own alias. |
Fragments
Use fragments to reuse field selections across multiple queries.
|
The |
GraphQL Mutations
Basic Mutation Structure
A GraphQL mutation modifies data on the server.
|
This mutation creates a new user with the provided name and email, and returns the |
Variables
Use variables to make mutations dynamic.
Variables:
|
This mutation uses variables |
Updating and Deleting Data
Mutations can also be used to update and delete data.
|
These mutations update the title of a post and delete a post, respectively. |