Created
August 19, 2018 18:35
-
-
Save mcordell/42316df865787f1fb27a6eb702dec8dc to your computer and use it in GitHub Desktop.
Mock Graphql Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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