Skip to content

Instantly share code, notes, and snippets.

@johnwook
Created January 31, 2018 05:03
Show Gist options
  • Save johnwook/3645b4884e158f3d654fb27aaa468d8b to your computer and use it in GitHub Desktop.
Save johnwook/3645b4884e158f3d654fb27aaa468d8b to your computer and use it in GitHub Desktop.
micro with apollo-graphql-server
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