Created
May 7, 2020 21:57
-
-
Save Happytreat/6712488f89832a65c937c622a226d349 to your computer and use it in GitHub Desktop.
GraphQL Boilerplate index.ts
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
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