Created
January 31, 2018 05:03
-
-
Save johnwook/3645b4884e158f3d654fb27aaa468d8b to your computer and use it in GitHub Desktop.
micro with apollo-graphql-server
This file contains hidden or 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 { microGraphiql, microGraphql } = require("apollo-server-micro"); | |
const { makeExecutableSchema } = require("graphql-tools"); | |
const { send } = require("micro"); | |
const { get, post, router } = require("microrouter"); | |
// Some fake data | |
const books = [ | |
{ | |
title: "Harry Potter and the Sorcerer's stone", | |
author: 'J.K. Rowling', | |
}, | |
{ | |
title: 'Jurassic Park', | |
author: 'Michael Crichton', | |
}, | |
]; | |
// The GraphQL schema in string form | |
const typeDefs = ` | |
type Query { books: [Book] } | |
type Book { title: String, author: String } | |
`; | |
// The resolvers | |
const resolvers = { | |
Query: { books: () => books }, | |
}; | |
// Put together a schema | |
const schema = makeExecutableSchema({ | |
typeDefs, | |
resolvers, | |
}); | |
const graphqlHandler = microGraphql({ schema }); | |
const graphiqlHandler = microGraphiql({ endpointURL: "/graphql" }); | |
const handler = router( | |
get("/graphql", graphqlHandler), | |
post("/graphql", graphqlHandler), | |
get("/graphiql", graphiqlHandler), | |
(_, res) => send(res, 404, "not found") | |
); | |
module.exports = handler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment