Skip to content

Latest commit

 

History

History
89 lines (60 loc) · 4.14 KB

GraphQL.md

File metadata and controls

89 lines (60 loc) · 4.14 KB

GraphQL

A Brief Overview

  • What is GraphQL?

    GraphQL is a query language and runtime for APIs (Application Programming Interfaces) that allows clients to request only the data they need, making it a more efficient and flexible alternative to traditional REST APIs.

  • Key Concepts

    Query Language: Clients can specify the exact data they need using a specific query language.

    Types: GraphQL defines data types, which can be scalar or complex.

    Operations: GraphQL has three primary operations: Queries, Mutations, and Subscriptions.

    Single Endpoint: GraphQL typically uses a single endpoint for all data operations.

GraphQL Operations

GraphQL operations are requests made to a GraphQL server to retrieve or manipulate data. In GraphQL, there are three main types of operations: queries, mutations, and subscriptions.

  • Queries: A query is used to request data from a GraphQL server. It resembles a read operation in traditional REST APIs. You define the shape and structure of the data you want to retrieve in the query, and the server responds with exactly that data structure.

    For example:

    query {
        user(id: 1) {
            name
            email
        }
    }    
    

    In this example, the query asks for the name and email fields of a user with id equal to 1.

  • Mutations: Mutation: A mutation is used to modify data on the server. It can be used for creating, updating, or deleting data. Mutations are similar to POST, PUT, or DELETE requests in RESTful APIs.

    For example:

    mutation {
        updateUser(id: 1, input: { name: "New Name", email: "new@email.com" }) {
            id
            name
            email
        }   
    }
    

    This mutation updates the user with id 1 with the provided new name and email.

  • Subscriptions: A subscription is a real-time feature in GraphQL that allows clients to subscribe to specific events and receive updates when those events occur. Subscriptions are often used for implementing real-time features such as chat applications, live notifications, or live data feeds.

    For example:

    subscription {
     newMessage(channelId: "123") {
        text
        sender
     }
    }
    

    This subscription listens for new messages in a channel with channelId 123 and receives updates when new messages are posted.

Components of Queries and Mutations

  • Fields: Properties or attributes on object types. Define the data to be queried.

  • Arguments: Values provided for specific fields.

  • Variables: Enable dynamic arguments.

  • Aliases: Used to rename query result fields.

  • Fragments: Reusable parts of queries or mutations.

GraphQL Schema

GraphQL, a schema serves as a fundamental contract between the client and the server. It defines the structure and capabilities of the API, specifying what data can be queried, how it should be structured in the response, and how mutations (changes to data) can be performed. The schema is a critical component of any GraphQL API.

A GraphQL schema typically consists of two main parts:

  • Types: Types represent the different kinds of data that can be queried or mutated. There are two primary types of types in a GraphQL schema:

    Scalar Types: These represent individual pieces of data, such as integers, strings, booleans, or custom scalar types defined by the developer.

    Object Types: These represent complex data structures and are composed of fields. Each field can have its own type, which can be a scalar type or another object type. Object types define the hierarchical structure of the data.

  • Queries and Mutations

GraphQL introspection

GraphQL introspection is a feature that allows you to query information about the schema of a GraphQL API using GraphQL itself. It's like asking the API about its own structure and capabilities, and it's a powerful tool for exploring and understanding the schema without needing external documentation. Introspection is especially useful during development, for generating documentation, and for building tools and clients that interact with GraphQL APIs.