1

Question: Is there a way to mock some GraphQL types/queries/mutations/subscriptions while 'forwarding' the rest to an existing GraphQL server?

Situation: The application has a schema, part of which has been implemented on the server and the rest is yet to be developed. Working on the frontend sometimes creates a dependency on the backend team which I'm trying to address.

Target setup: The setup in question should 'forward' all requests to the existing GraphQL server except the ones I want mocked. So for parts that have been implemented on the server (user auth, for example), the actual server is used while for parts that have not been implemented on the server, I can use mocked responses.

I've tried searching and while I found ways of creating a mock GraphQL server, I couldn't find something that does it this way.

I'm open to writing some code to make this happen but being new to GraphQL, I figured asking about this first would give me some helpful pointers on how to tackle this.

Fwiw, the frontend uses Apollo GraphQL (with React) and the backend uses Absinthe (with Elixir/Phoenix)

Kul
  • 1,239
  • 8
  • 18
  • 2
    Merging two GraphQL servers is called "Schema stitching". Before you start with that (spoiler alert: it's not that easy) have a look at [graphql-faker](https://github.com/APIs-guru/graphql-faker). It might cover 90% of what you need for 10% of the effort. Instead of stitching you can also try to just implement the GraphQL interface and add fake resolvers. In our server we use fake models that work on JSON data to deliver _some_ API to the frontend asap. – Herku Jan 01 '18 at 12:16
  • @Herku thanks! graphql-faker looks helpful – Kul Jan 01 '18 at 17:24

2 Answers2

0

import {mockServer} from 'graphql-tools'. Mock a schema :

import { mockServer } from 'graphql-tools';
import schema from './mySchema.graphql';

const myMockServer = mockServer(schema);
myMockServer.query(`{
  allUsers: {
    id
    name
  }
}`);
// returns
// {
//   data: {
//     allUsers:[
//       { id: 'ee5ae76d-9b91-4270-a007-fad2054e2e75', name: 'lorem ipsum' }, 
//       { id: 'ca5c182b-99a8-4391-b4b4-4a20bd7cb13a', name: 'quis ut' }
//     ]
//   }
// }

to read more on graphql customizing mock data please see full examples in graphql official site : http://graphql.org/blog/mocking-with-graphql/

toufek khoury
  • 2,974
  • 2
  • 8
  • 15
0

Maybe you can use easygraphql to solve this challenge!! There are multiple packages that can be used:

  1. easygraphql-now: you can create a script on your package.json that will run "easygraphql-now schema.gql --graphiql --local -p=7000" where you pass the schema route, local and graphiql flag, and the port... so when you run it; it will create a mocked server of the passed schema, so your application will make request to a server that will respond a mock of your query/mutation.

  2. easygraphql-mock: if you want to return a complete mock of the type, you can use this package, and with this, you don't have to create fixtures for each type.

  3. easygraphql-tester: It's similar to easygraphql-mock but with the difference that you can return the mock of the query, check the docs

So, you can choose which routes should make a request to easygraphql-now or just add the response of the query/mutation using easygraphql-tester