Skip to content

Instantly share code, notes, and snippets.

@johnwook

johnwook/index.js

Created Jan 31, 2018
Embed
What would you like to do?
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