dfernandeza / graphql-mimic

Client side mocking library for GraphQL.
4 stars 0 forks source link

graphql-mimic

Client side mocking library for GraphQL.

Using the Rick and Morty GraphQL API for example purposes.

npx apollo client:download-schema schema.json --endpoint=https://rickandmortyapi.com/graphql

General idea

Combine information from the GraphQL schema introspection query result and a provided GraphQL AST (a graphql-tag query) to automatically generate response mocks. i.e.

import * as schema from "./schema.json";

const QUERY = gql`
  query {
    characters {
      results {
        id
        name
      }
    }
  }
`;

Mimic.config({ schema });
const mock = Mimic.mock(QUERY);

// {
//   data: {
//     characters: {
//       results: [
//         {
//           id: "1",
//           name: "lorem ipsum"
//         },
//         {
//           id: "2",
//           name: "lorem ipsum"
//         }
//       ];
//     }
//   }
// }

GraphQL-Mimic will generate a mock where each field is assigned a default value. Default values are assigned according to the field scalar type as follows:

Notice that the ID scalar type could be assigned with a value of "1" or "2" because by default every list in the mocked response will contain 2 items.

Additional notes

graphql-mimic is intended to be client agnostic, but we plan to provide "adapters" to make it easy to work with common GraphQL clients like Apollo Client.

:warning: This is still a work in progress, any collaboration would be greatly appreciated.