Giter VIP home page Giter VIP logo

graphql-mock-server's Introduction

Example

https://github.com/sandiiarov/cypress-graphql-mock-server-example

Install

yarn global add graphql-mock-server

or

yarn add --dev graphql-mock-server

Usage

graphql-mock-server ./schema.graphql -p 4000
gms ./schema.graphql -p 4000

Basic example

import { serialize } from 'graphql-mock-server';

const mock = {
  Query: () => ({
    users: () => [
      {
        id: '1',
        name: 'Foo',
      },
      {
        id: '2',
        name: 'Baz',
      },
    ],
  }),
};

const data = serialize(mock);

// Set mock
fetch('http://localhost:4000/mock', {
  method: 'POST',
  body: JSON.stringify(data),
});

// Reset mock
fetch('http://localhost:4000/reset', {
  method: 'POST',
});

How to write mocks https://www.apollographql.com/docs/graphql-tools/mocking.html

Accessing arguments in mock resolvers

Since the mock functions on fields are actually just GraphQL resolvers, you can use arguments and context in them as well:

{
  Query: () => ({
    // the number of friends in the list now depends on numPages
    paginatedFriends: (root, { numPages }) => new MockList(numPages * PAGE_SIZE),
  }),
}

Cypress

yarn add --dev cypress-graphql-mock-server
// cypress.json

{
  "env": {
    "GRAPHL_MOCK_SERVER": "http://localhost:<PORT>"
  }
}
// support/index.js

import 'cypress-graphql-mock-server';

beforeEach(() => cy.resetGQLMock());

If you are using TypeScript

// tsconfig.json

{
  "compilerOptions": {
    ...
    "types": ["cypress-graphql-mock-server"]
  }
}

Generate types

You can also generate TS types using GraphQL schema. You need to use https://github.com/dotansimha/graphql-code-generator with plugins:

  • graphql-codegen-typescript-common
  • graphql-codegen-typescript-server
  • graphql-codegen-typescript-mocks

graphql-mock-server's People

Contributors

christopherscott avatar renovate-bot avatar renovate[bot] avatar sandiiarov avatar

Stargazers

 avatar  avatar

Watchers

 avatar

graphql-mock-server's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

npm
package.json
  • apollo-server-express 2.26.2
  • body-parser 1.20.2
  • commander 2.20.3
  • express 4.19.2
  • graphql 14.7.0
  • lunar-core 1.0.0
  • @pika/pack 0.5.0
  • @pika/plugin-build-node 0.9.2
  • @pika/plugin-simple-bin 0.9.2
  • @pika/plugin-ts-standard-pkg 0.9.2
  • typescript 3.9.10

  • Check this box to trigger a request for Renovate to run again on this repository

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.