{"id":13255,"library":"graphql-mocks","title":"GraphQL Mocks Core","description":"`graphql-mocks` is the foundational package providing core primitives for robustly mocking GraphQL APIs, primarily designed for testing and development environments. Its current stable version is 0.12.0. The library offers key differentiators such as the `GraphQLHandler` for orchestrating mock resolution, `Resolver Map Middlewares` for modifying resolver behavior, `Resolver Wrappers` for aspect-oriented resolver logic, and `Highlight` for debugging complex mock interactions. This package is part of a larger ecosystem of `graphql-mocks` libraries, including network adapters for various HTTP mocking tools. The project exhibits an active release cadence, with frequent updates across its monorepo packages, indicating ongoing development and support. It ships with comprehensive TypeScript types, promoting type-safe development.","status":"active","version":"0.12.0","language":"javascript","source_language":"en","source_url":"https://github.com/graphql-mocks/graphql-mocks","tags":["javascript","graphql-mocks","graphql","mocking","typescript"],"install":[{"cmd":"npm install graphql-mocks","lang":"bash","label":"npm"},{"cmd":"yarn add graphql-mocks","lang":"bash","label":"yarn"},{"cmd":"pnpm add graphql-mocks","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core GraphQL library for schema definition and execution, required as a peer dependency.","package":"graphql","optional":false}],"imports":[{"note":"`graphql-mocks` is primarily designed for ESM usage, but CJS is supported with appropriate tooling. Always prefer named imports.","wrong":"const { GraphQLHandler } = require('graphql-mocks');","symbol":"GraphQLHandler","correct":"import { GraphQLHandler } from 'graphql-mocks';"},{"note":"A utility function to structure resolver maps, imported as a named export from the main package.","wrong":"import createResolverMap from 'graphql-mocks/createResolverMap';","symbol":"createResolverMap","correct":"import { createResolverMap } from 'graphql-mocks';"},{"note":"The debugging utility `highlight` is a named export, not a class or default export.","wrong":"import { Highlight } from 'graphql-mocks';","symbol":"highlight","correct":"import { highlight } from 'graphql-mocks';"}],"quickstart":{"code":"import { buildSchema } from 'graphql';\nimport { GraphQLHandler, createResolverMap } from 'graphql-mocks';\n\nconst typeDefs = `\n  type User {\n    id: ID!\n    name: String!\n    email: String\n  }\n\n  type Query {\n    hello: String!\n    user(id: ID!): User\n    users: [User!]!\n  }\n\n  type Mutation {\n    createUser(name: String!, email: String): User!\n  }\n`;\n\n// Create a GraphQL schema from the type definitions\nconst schema = buildSchema(typeDefs);\n\n// Define a resolver map with mock data and logic\nconst resolverMap = createResolverMap({\n  Query: {\n    hello: () => 'Hello GraphQL Mocks!',\n    user: (parent, { id }) => ({ id, name: `User ${id}`, email: `user${id}@example.com` }),\n    users: () => [\n      { id: '1', name: 'Alice', email: 'alice@example.com' },\n      { id: '2', name: 'Bob', email: 'bob@example.com' }\n    ]\n  },\n  Mutation: {\n    createUser: (parent, { name, email }) => ({ id: String(Math.random()), name, email })\n  }\n});\n\n// Initialize the GraphQLHandler with the schema and resolver map\nconst handler = new GraphQLHandler({\n  schema,\n  resolverMap\n});\n\nasync function runMockOperations() {\n  // Execute a mock query\n  const query = `\n    query GetUsers {\n      users {\n        id\n        name\n        email\n      }\n    }\n  `;\n  const queryResult = await handler.query(query);\n  console.log('Mocked Query Result:', queryResult.data);\n\n  // Execute a mock mutation\n  const mutation = `\n    mutation CreateNewUser {\n      createUser(name: \"Charlie\", email: \"charlie@example.com\") {\n        id\n        name\n      }\n    }\n  `;\n  const mutationResult = await handler.mutate(mutation);\n  console.log('Mocked Mutation Result:', mutationResult.data);\n}\n\nrunMockOperations();","lang":"typescript","description":"This quickstart demonstrates how to set up a `GraphQLHandler` with a schema and a resolver map, then execute a mock query and mutation against it, simulating a GraphQL API."},"warnings":[{"fix":"Upgrade your Node.js environment to version 24.0.0 or newer.","message":"The package requires Node.js version 24.0.0 or higher. Users on older Node.js versions will encounter compatibility issues.","severity":"breaking","affected_versions":">=0.12.0"},{"fix":"Ensure your project's `graphql` dependency matches the peer dependency range specified by `graphql-mocks`. Use `npm install graphql@^16` or `yarn add graphql@^16` if necessary.","message":"This library has a peer dependency on `graphql` version `^16.0.0 <17`. Mismatched `graphql` versions in your project can lead to unexpected errors or runtime issues.","severity":"gotcha","affected_versions":">=0.12.0"},{"fix":"Consult the project's GitHub releases or changelog for specific breaking changes and migration guides during updates.","message":"As a 0.x.x versioned library, `graphql-mocks` may introduce breaking changes without a major version bump. Always review release notes carefully when updating.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Run `npm install graphql-mocks` or `yarn add graphql-mocks`. Verify your import statement uses `from 'graphql-mocks'`.","cause":"The `graphql-mocks` package is not installed or the import path is incorrect.","error":"Error: Cannot find module 'graphql-mocks'"},{"fix":"Ensure you are using `buildSchema` from `graphql` to create your schema, or that the schema object is correctly instantiated.","cause":"The `schema` provided to `GraphQLHandler` is not a valid `GraphQLSchema` object from the `graphql` library.","error":"Error: Expected `schema` to be a GraphQLSchema instance, but got [object Object]"},{"fix":"Review your GraphQL schema (`typeDefs`) and the query you are sending to ensure all fields are correctly defined and match.","cause":"Attempting to query a field that is not defined in the GraphQL schema provided to the `GraphQLHandler`.","error":"GraphQLError: Cannot query field \"unknownField\" on type \"Query\"."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null}