Skip to content

Instantly share code, notes, and snippets.

@Happytreat
Created May 7, 2020 21:57
Show Gist options
  • Save Happytreat/6712488f89832a65c937c622a226d349 to your computer and use it in GitHub Desktop.
Save Happytreat/6712488f89832a65c937c622a226d349 to your computer and use it in GitHub Desktop.
GraphQL Boilerplate index.ts
import express from 'express'
import { ApolloServer, gql } from 'apollo-server-express'
// Some fake data (to be removed in later section)
const books = [
{
title: "Harry Potter and the Sorcerer's stone",
author: 'J.K. Rowling',
},
{
title: 'Jurassic Park',
author: 'Michael Crichton',
},
];
const typeDefs = gql`
type Query { books: [Book] }
type Book { title: String, author: String }
`;
const resolvers = {
Query: { books: () => books },
};
const server = new ApolloServer({ typeDef, resolvers })
const app: express.Application = express()
server.applyMiddleware({ app })
// Start the server
// process.env.PORT will be used by GAE
const port = process.env.PORT || 4000
app.listen({ port: port }, () =>
console.log(`Server is ready at http://localhost:4000${server.graphqlPath}`)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment