GraphQL Schema Mocking with GraphQL Tools

7 Feb 2022

type Query {
  users: [User!]!
  posts: [Post!]!
}

type User {
  id: ID!
  name: String!
  email: String!
  age: Int!
  posts: [Post]
}

type Post {
  id: ID!
  title: String!
  content: String!
  author: User!
}
const schema = makeExecutableSchema({ typeDefs });

const schemaWithMocks = addMocksToSchema({
  schema,
  mocks: {
    String: () => casual.sentence,
    Int: () => casual.integer(1, 100),
    User: () => ({
      id: casual.uuid,
      name: casual.name,
      email: casual.email,
      age: casual.integer(18, 122),
    }),
    Post: () => ({
      id: casual.uuid,
      title: casual.sentence,
      content: casual.sentences(5),
    }),
  },
});