graphql-mocks API Reference

website·Indexed 32 days ago

https://www.graphql-mocks.com/

API documentation for graphql-mocks, featuring utilities for schema creation, resolver mapping, and Relay pagination. Includes functions for attaching resolvers and scalars to schemas, type utilities like isRootQueryType and unwrap, and types for Relay pagination results and edges.

Tokens
81.2K
Snippets
617
Records
751
Agent score
51%

What's inside graphql-mocks

  1. Overview of Paper validations

    Validations in graphql-paper ensure the DocumentStore remains consistent after transactions. Validators are categorized into Document Validators (document-level) and Field Validators (field-level). They are configured on a Paper instance via the validators property.
    paper.validators = {
      document: [], // Array of Document Validators
      field: [],    // Array of Field Validators
    }
  2. Overview of Paper validations

    Validations in graphql-paper ensure the DocumentStore remains consistent after transactions. They are managed on a Paper instance via the validators property, which contains two arrays: document for Document Validators and field for Field Validators.
    paper.validators = {
      document: [], // Array of Document Validators
      field: [],    // Array of Field Validators
    }
  3. Overview of graphql-mocks capabilities

    graphql-mocks is a library for testing, mocking, and prototyping GraphQL APIs. Instead of a single configuration convention, it provides a composable system of tools to create flexible mock APIs. Key features include:

    • GraphQL Paper: Provides out-of-the-box stateful queries using an in-memory store.
    • spyWrapper: Allows wrapping Sinon spies around resolvers for testing.
    • logWrapper: Provides logging for resolver activity.
    • Highlight: A query-like technique used to conditionally apply wrappers and middlewares to a GraphQL schema.
    • Resolver Map Middlewares & Resolver Wrappers: Tools to organize and create reusable abstractions for common mocking scenarios.
  4. Overview of graphql-paper API structure

    The graphql-paper library is organized into several key namespaces and a primary class. The core functionality is centered around the Paper class, with supporting logic distributed across the following namespaces:

    • documentHelpers: Utilities for handling GraphQL documents.
    • operations: Logic related to GraphQL operations.
    • types: Type definitions and handling.
    • validators: Validation logic for GraphQL schemas or documents.
  5. Overview of GraphQL Paper features

    GraphQL Paper is a TypeScript-based, immutable in-memory store derived from a GraphQL Schema. It is designed for testing and development to reflect the state of the world, handle data connections, and process updates via mutations. It can be used independently or integrated with graphql-mocks and supports official default GraphQL resolvers. Key capabilities include:

    • Browser and Node.js compatibility
    • Support for relationships and connections between types
    • Serialization and deserialization
    • Access via native JavaScript APIs
    • Support for Hooks, Events, Transaction Operations, and Validations
  6. Overview of graphql-paper API Structure

    The graphql-paper library is organized into several namespaces and a core class to handle GraphQL document operations and validation. Key components include:

    • Namespaces:
      • documentHelpers: Utilities for manipulating GraphQL documents.
      • operations: Logic related to GraphQL operations.
      • types: Type definitions and handling.
      • validators: Validation logic for GraphQL schemas and documents.
    • Core Class:
      • Paper: The primary class used to interact with the library's functionality.
  7. Overview of graphql-mocks capabilities

    graphql-mocks is a composable toolkit for creating mock GraphQL APIs. Instead of a single configuration method, it provides a set of utilities, Resolver Map Middlewares, and Resolver Wrappers that can be layered to build reusable, declarative mock systems. Key features include:

    • GraphQL Paper: Provides out-of-the-box stateful queries using an in-memory store.
    • spyWrapper: Integrates Sinon spies around resolvers for testing.
    • logWrapper: Provides logging for resolver activity.
    • Highlight: A query-like technique used to conditionally apply wrappers and middlewares to a GraphQL schema.
  8. Overview of GraphQL Paper features

    GraphQL Paper is a TypeScript-based, immutable in-memory store that uses a GraphQL Schema to manage data. It is designed for testing and development to reflect the current state of the world, handle connections between data, and support updates via mutations. It can be used independently or integrated with graphql-mocks.

    Key capabilities include:

    • Compatibility with both Browser and Node.js environments.
    • Support for official default GraphQL resolvers.
    • Handling of relationships and connections between GraphQL types.
    • Serialization and deserialization support.
    • Access via native JavaScript APIs, Hooks, Events, Transaction Operations, and Validations.
  9. Understand GraphQL Paper storage and immutability

    GraphQL Paper uses immer to manage state changes and optimize reference sharing for unchanged object trees. Documents and DocumentStores are treated as stale-on-arrival and must not be edited directly. All modifications must occur within a Mutate Transaction to ensure immutability and enable versioning. Comparing different versions of documents is performed using their unique Document Keys.
  10. Use wrapperOptions for resolver context

    Both createWrapper and generic wrapper functions receive a wrapperOptions object. This object provides contextual details about the resolver being wrapped, such as:

    • wrapperOptions.type: The GraphQL type associated with the resolver.
    • wrapperOptions.field: The specific field being wrapped (available when wrapping a Field Resolver).
  11. Manage stateful mock data with GraphQL Paper

    GraphQL Paper is an in-memory store designed for stateful handling of data based on a GraphQL Schema. While it integrates deeply with graphql-mocks to provide a complete mocking story, it can also be used as a standalone library. It supports customization via events, hooks, and custom validations.
  12. Execute queries with the GraphQL Handler

    The GraphQL Handler is a utility that takes a GraphQL query and variables, then applies them against a GraphQL Schema and its associated Resolver Map to produce a result. If you already have a handler (e.g., from graphql-tools), you can use the pack utility to create a compatible Resolver Map containing all necessary Middlewares and Wrappers.