GraphQL Yoga

repository·main·Indexed 11 days ago

https://github.com/dotansimha/graphql-yoga

A GraphQL server ecosystem focusing on Envelop plugins and cross-runtime compatibility. It supports deployment to CloudFlare Workers, Azure Functions, AWS Lambda, Vercel Functions, and Docker containers. The ecosystem includes examples for Apollo Federation, @defer and @stream directives, and integrations with GraphQL-Helix, graphql-http, and Socket.io.

Tokens
213.4K
Snippets
655
Records
813
Agent score
93%

What's inside GraphQL Yoga

  1. Overview of GraphQL-Yoga E2E testing

    main

    GraphQL-Yoga uses end-to-end (e2e) testing to ensure compatibility across various popular runtimes. The testing process involves provisioning real resources in each environment using Pulumi (via the Automation API with TypeScript), executing smoke tests, and then destroying the resources.

    Smoke tests consist of:

    1. GET -> GraphiQL (verifying the GraphiQL interface is accessible).
    2. POST -> Execute GraphQL (verifying GraphQL query execution).

    This workflow is automated on every Pull Request to the GraphQL-Yoga repository to catch runtime or compatibility issues early in the development cycle.

  2. What is Envelop?

    main

    Envelop is a lightweight JavaScript/TypeScript library designed to customize the GraphQL execution layer. It allows developers to build, share, and compose plugins that enhance GraphQL server capabilities such as logging, monitoring, caching, rate-limiting, and error handling.

    Key characteristics:

    • Framework Agnostic: It does not dictate your GraphQL transport or framework and can be used with any GraphQL server that follows the GraphQL specification.
    • Zero Dependencies: The core of Envelop has zero dependencies and only alters the execution phases that your plugins specifically hook into.
    • Environment Agnostic: It works in any environment (Node.js or browser) and any workflow (client/server, client-side, or server-to-server).
  3. Overview of GraphQL Yoga features

    main

    GraphQL Yoga is a fully-featured GraphQL server designed for ease of setup and high performance. Key features include:

    • Environment Agnostic: Built on the WHATWG Fetch API, allowing it to run on Node, Deno, Bun, Cloudflare Workers, and AWS Lambda.
    • Built-in Subscriptions: Supports real-time capabilities via Server-Sent Events (SSE).
    • Spec Compliant: Follows the GraphQL over HTTP spec and supports GraphQL Multipart Request spec for file uploads.
    • Developer Experience: Includes GraphiQL out of the box, is fully typed with TypeScript, and supports ESM.
    • Performance: Includes built-in parsing and validation caching, and supports Automatic Persisted Queries (APQ).
    • Extensibility: Fully compatible with all envelop plugins.
  4. Use @envelop/generic-auth for custom authentication

    main

    The @envelop/generic-auth plugin enables custom authentication flows by allowing you to provide a user resolver that operates on the original HTTP request. Once a user is resolved, they are injected into the GraphQL execution context, making the user object available to your resolvers.

    This plugin supports three primary implementation patterns:

    1. Complete Protection: Protects the entire GraphQL schema from unauthenticated access. You can selectively allow unauthenticated access to specific fields using the @skipAuth directive or a skipAuth field extension.
    2. Manual Validation: The plugin resolves the user and injects them into the context, but does not perform any automatic schema field validation. You are responsible for checking the user's presence or permissions within your resolvers.
    3. Granular Field Access: Automatically protects specific GraphQL fields by looking for the @authenticated directive or an authenticated extension field in your schema.
  5. How to use @envelop/instrumentation

    main

    The @envelop/instrumentation package provides utilities to manage instrumentation across Envelop, Yoga, whatwg-node, and Hive Gateway plugins.

    By default, Envelop automatically composes instrumentations together. You only need to use this package manually if you require a specific execution order that differs from the default (for example, if you need instrumentation and hooks to execute in a different sequence than the standard plugin lifecycle).

  6. Use @graphql-yoga/redis-event-target for distributed GraphQL subscriptions

    main
    The @graphql-yoga/redis-event-target package allows you to implement distributed GraphQL subscriptions using Redis. This is essential when running multiple instances of a GraphQL server (e.g., in a load-balanced environment) to ensure that subscription events are broadcasted across all server nodes, allowing clients to receive updates regardless of which specific instance they are connected to.
  7. Compare GraphQL Yoga with Apollo Server

    main

    GraphQL Yoga is designed as a high-performance, portable alternative to Apollo Server. Key differentiators include:

    • Performance: Significantly lower latency and higher request rates in popular benchmarks, including Apollo Federation scenarios.
    • Architecture: Built on the W3C Request/Response (Fetch API) specification, whereas Apollo Server relies on framework-specific adapters (e.g., apollo-server-express).
    • Portability: Runs natively on Deno, Cloudflare Workers, Bun, and AWS Lambda without code changes. Apollo Server's support for Cloudflare Workers is community-maintained.
    • Plugin System: Built on top of Envelop, allowing access to a vast ecosystem of plugins (rate limiting, caching, Auth, etc.). Apollo Server is not fully compatible with Envelop.
    • Spec Compliance: Fully compliant with the GraphQL over HTTP specification and incremental delivery.
    • Features: Built-in support for file uploads (GraphQL-Multipart-Request) and subscriptions via Server-Sent Events (SSE) without extra libraries.
    • IDE: Ships with the latest open-source GraphiQL 3 IDE.
  8. What is a GraphQL Schema?

    main

    A GraphQL schema serves as the API contract between a consumer and a provider. It consists of:

    • Types: Defined using GraphQL SDL, these describe the shape of the data.
    • Fields: Connections between types that form a graph.
    • Entry Points: Special types named Query, Mutation, and Subscription that act as the starting points for accessing the graph.
    • Resolvers: The implementation logic that fetches or calculates the data for specific fields.
    • Operations: The client-side request (often called a query) that selects specific fields from the schema to retrieve data.
  9. What is an Envelop plugin and how does it work?

    main

    Envelop plugins are objects containing handler functions that provide contextual implementations for running logic during different phases of the GraphQL execution lifecycle. They allow you to hook into phases like parsing and execution to run logic both before and after specific operations.

    If you are using TypeScript, you should import the Plugin interface from the @envelop/core package to ensure full type safety when defining your plugin object.