Socket
Socket
Sign inDemoInstall

graphql-automock

Package Overview
Dependencies
13
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    graphql-automock

Automock GraphQL schemas for better testing and development


Version published
Weekly downloads
2
increased by100%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

⭐️ graphql-automock ⭐️

Automock GraphQL schemas for better testing.

Getting started

Simply pass your GraphQL type definitions to mockSchema and you're ready to go:

import { mockSchema } from "graphql-automock";
import { graphql } from "graphql";

const types = `
  type Query {
    recentPosts: [Post!]!
  }

  type Post {
    id: ID!
    content: String!
    likes: Int!
  }
`;

const mocked = mockSchema(types);

graphql(
  mocked,
  `
    query {
      recentPosts {
        id
        content
        likes
      }
    }
  `
);

Without any further configuration, this query will return:

{
  "data": {
    "recentPosts": [
      {
        "id": "recentPosts.0.id",
        "content": "recentPosts.0.content",
        "likes": 2
      },
      {
        "id": "recentPosts.1.id",
        "content": "recentPosts.1.content",
        "likes": 2
      }
    ]
  }
}

Deterministic mocking

To ensure that tests are reliable, the values generated by graphql-automock are 100% deterministic. The following default values are used:

  • Boolean: true
  • Int: 2
  • Float: 3.14
  • String: Path to value
  • ID: Path to value
  • Enum: The first enum value, sorted alphabetically by name
  • Interface: The first possible implementation, sorted alphabetically by name
  • Union: The first possible member type, sorted alphabetically by name
  • List length: 2

Customizing mocks

Automatically mocking the entire schema with sensible, deterministic data allows test code to customize only the data that affects the test. This results in test code that is more concise and easier to understand:

test("likes count is hidden when there are no likes", () => {
  // Set up test data
  const mocked = mockSchema({
    schema: types,
    mocks: {
      Post: () => ({
        likes: 0 // Only the likes field is customized
      })
    }
  });

  // Continue with test...
});

API Reference

mockSchema

function mockSchema(schema: String | GraphQLSchema);

function mockSchema({
  schema: String | GraphQLSchema,
  mocks: { [String]: MockTypeResolverFn }
});

type MockTypeResolverFn = (source, args, context, info) => any;

FAQs

Last updated on 15 Jul 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc