GraphQL Mesh

repository·master·Indexed 25 days ago

https://github.com/ardatan/graphql-mesh

A GraphQL Federation framework and gateway that enables developers to access data from diverse sources—including REST, gRPC, OpenAPI, and various databases—using a unified GraphQL interface. It acts as a proxy that can be run locally or deployed as a gateway layer.

Tokens
105.2K
Snippets
378
Records
600
Agent score
86%

What's inside graphql-mesh

  1. Overview of Omnigraph

    master

    Omnigraph is a set of libraries and tools designed to generate local GraphQLSchema instances from various non-GraphQL API specifications, including JSON Schema, MySQL, SOAP, OpenAPI, and RAML.

    The generated GraphQLSchema can be consumed by standard tools in the GraphQL ecosystem, such as:

    • GraphQL Config
    • GraphQL Code Generator
    • GraphQL ESLint

    You can also bind the generated schema to GraphQL servers like Envelop, Express-GraphQL, GraphQL Helix, Apollo Server, or GraphQL Yoga.

  2. Overview of GraphQL Mesh

    master

    GraphQL Mesh is a framework for building GraphQL gateways and APIs by aggregating data from various sources. It acts as a proxy that allows you to use GraphQL queries and mutations to interact with non-GraphQL services like REST, gRPC, OpenAPI/Swagger, OData, SOAP/WSDL, Apache Thrift, Mongoose, and databases such as MongoDB, MySQL, PostgreSQL, and Neo4j.

    Key capabilities include:

    • Schema Modification: Modify output schemas, link types across schemas, and merge schema types.
    • Customization: Add custom GraphQL types and resolvers.
    • Data Fetching Control: Control how data is fetched to overcome backend implementation issues or legacy API limitations.
    • Deployment Options: Run as a local GraphQL schema (using GraphQL execute) or deploy as a gateway layer for internal services.
  3. Introduction to GraphQL Mesh

    master

    GraphQL Mesh is a framework designed to build GraphQL Gateways on top of both GraphQL and non-GraphQL services (such as REST, gRPC, and SOAP). It allows you to consume multiple API/Source types and unify them into a single GraphQL schema.

    Key capabilities include:

    • Multi-source consumption: Support for SOAP, REST, gRPC, Databases, and more.
    • High-performance server: Uses GraphQL Yoga and Envelop as the embedded server.
    • Schema transformations: Apply transforms (e.g., naming conventions) to the unified schema.
    • Extensibility: Extend the unified schema with custom resolvers.
    • Developer features: Built-in caching strategies and support for adding mocks for schema development.

    Common use cases:

    • Building a unified GraphQL schema following industry best practices.
    • Combining multiple GraphQL services into a single schema with subscription support.
    • Progressively migrating non-GraphQL services (REST, gRPC, SOAP) to a unified GraphQL API.
    • Translating existing REST APIs to GraphQL quickly.
  4. Understand Type Merging in GraphQL Mesh

    master

    Type merging allows you to define partial definitions of a type across multiple subschemas. These partial definitions are merged into a single unified type in the gateway schema.

    When a query is made for a merged type, the gateway intelligently delegates portions of the request to the relevant subschemas in the correct dependency order and combines the results. This is the preferred method for combining subgraphs (similar to Apollo Federation) and replaces the older schema extensions method.

  5. Understand the core concepts of GraphQL Mesh

    master

    GraphQL Mesh operates using four primary concepts to create a unified gateway:

    1. Sources: Sub-services such as GraphQL APIs or REST APIs.
    2. Handlers: Components that translate a Source into a GraphQL Schema (e.g., translating a Swagger definition into a schema).
    3. Unified Schema: The final, merged GraphQL schema resulting from combining all Sources' schemas.
    4. Transforms: Optional transformations applied to the Unified Schema to modify its structure or behavior.

    In a typical workflow, Mesh fetches definitions from Sources, uses Handlers to convert them to GraphQL, merges them into a Unified Schema, and optionally applies Transforms.

  6. Compare GraphQL Gateway approaches

    master

    When choosing a GraphQL Gateway solution, evaluate based on productivity/maintainability, unified schema design capabilities, and sub-service support.

    • GraphQL Mesh: High productivity via configuration-based setup with built-in server, caching, Envelop plugins, and wide sub-service support. Offers flexible schema design using Transforms and custom resolvers.
    • GraphQL Tools: Programmatic approach. Good for schema design but primarily supports GraphQL sub-services out of the box.
    • Apollo Server with DataSources: Requires significant manual coding and maintenance at the DataSource level.
    • Apollo Federation: High productivity for Apollo-centric workflows (Rover CLI, Apollo Studio), but limited to 'Federation compliant' GraphQL sub-services.
    • Hasura: Plug-and-play configuration-based solution, but the unified schema is tightly coupled to the underlying database or sub-service design, and it is limited to GraphQL, REST, and specific databases.
  7. Available GraphQL Mesh cache storage strategies

    master

    GraphQL Mesh supports several caching strategies to boost performance. You can choose from the following storage options depending on your environment and requirements:

    • File Cache: Stores cached data in files.
    • LocalForage: Uses LocalForage for storage (typically useful in browser-based or specific runtime environments).
    • CloudFlare Workers KV: Leverages CloudFlare Workers Key-Value storage.
    • Redis Cache: Uses a Redis instance for distributed caching.
  8. Use dynamic values in headers from Context

    master

    When running mesh dev or mesh start, you can use dynamic values in your header configuration by accessing the GraphQL Context (which represents the incoming HTTP request). Expressions must follow JavaScript syntax.

    • Accessing Headers: Use {context.headers['header-name']} to retrieve a specific header value. Important: Do not use capital letters when accessing header names (e.g., use x-my-api-token instead of X-My-Api-Token).
    • Accessing Cookies: Use {context.cookies.cookieName} to access cookie values.
    sources:
      - name: Example
        handler:
          <handler-name>:
            operationHeaders:
              # Accessing a header value
              Authorization: Bearer {context.headers['x-my-api-token']}
              # Accessing a cookie value
              Authorization: Bearer {context.cookies.myApiToken}
  9. Enforce session-based caching with `PRIVATE` scope

    master

    To ensure certain data is only cached when a user session is present, use the scope: PRIVATE argument with the @cacheControl directive.

    Any query that contains a type or a field marked with scope: PRIVATE will only be cached if a session is present.

    type Query {
      me: User @cacheControl(scope: PRIVATE)
    }
    
    type User @cacheControl(scope: PRIVATE) {
      #...
    }