Skip to content

Instantly share code, notes, and snippets.

@mcordell
Created August 19, 2018 18:35
Show Gist options
  • Save mcordell/42316df865787f1fb27a6eb702dec8dc to your computer and use it in GitHub Desktop.
Save mcordell/42316df865787f1fb27a6eb702dec8dc to your computer and use it in GitHub Desktop.
Mock Graphql Server
const { ApolloServer, gql } = require('apollo-server');
import {
makeExecutableSchema,
addMockFunctionsToSchema,
MockList
} from 'graphql-tools';
import casual from 'casual'
// Construct a schema, using GraphQL schema language
const typeDefs = gql`
type User {
id: Int
name: String
posts(limit: Int): [Post]
}
type Post {
id: Int
title: String
views: Int
author: User
}
type Query {
aString: String
aBoolean: Boolean
anInt: Int
author(id: Int): User
topPosts(limit: Int): [Post]
}
`;
const schema = makeExecutableSchema({ typeDefs });
const mocks = {
User: () => ({
name: casual.name,
id: () => casual.integer(1, 120),
}),
Post: () => ({
id: () => casual.integer(1, 120),
title: () => casual.words(10),
}),
Query: () => ({
topPosts: () => new MockList([2,6]),
})
};
addMockFunctionsToSchema({ schema, mocks });
const server = new ApolloServer({ schema });
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment