Last active
August 21, 2020 19:23
-
-
Save JeremyTheModernist/cb9bde9da8506653cc6b217d5bf73ed6 to your computer and use it in GitHub Desktop.
Quickly scaffold and express server with graphql
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 express = require('express'); | |
const bodyParser = require('body-parser'); | |
const { graphqlHTTP } = require('express-graphql'); | |
// build schema allows you to build your schema as a string literal, processed through | |
// then build schema converts it into a js object | |
// then use it in the "schema" property in the express graphql middleware | |
// alternatively you can create these schemas in object form using graphql's GraphQLObjectType, and types like GraphQLString, | |
// GraphQL object types act more like components with resolvers included | |
// learn more here: https://graphql.org/graphql-js/basic-types/#:~:text=The%20GraphQL%20schema%20language%20supports,any%20of%20the%20scalar%20types. | |
const { buildSchema } = require('graphql'); | |
const app = express(); | |
const events = []; | |
app.use(bodyParser.json()); | |
// only one endpoint for requests to go to | |
// graphqlHttp middleware goes here | |
// allows graphql to talk with express | |
// schema is the shape of your data | |
// buildSchema expects a `schema` type with `query` and `mutation` | |
// rootValue — a resolver for your schema | |
// rootValue resolvers must match the name of the queries | |
// with template literals it has some overlap with ts syntax | |
// input type allows you to define argument inputs | |
app.use( | |
'/graphql', | |
graphqlHTTP({ | |
schema: buildSchema(` | |
type Event { | |
_id: ID | |
title: String! | |
description: String! | |
price: Float! | |
date: String! | |
} | |
input EventInput { | |
title: String! | |
description: String! | |
price: Float! | |
date: String! | |
} | |
type RootQuery { | |
events: [Event!]! | |
} | |
type RootMutation { | |
createEvent(eventInput: EventInput): Event | |
} | |
schema { | |
query: RootQuery | |
mutation: RootMutation | |
} | |
`), | |
rootValue: { | |
events: () => { | |
return events; | |
}, | |
// you get an args object for each incoming request | |
createEvent: (args) => { | |
// have to destructure the event input object off of the arguments | |
var { eventInput } = args; | |
const event = { | |
_id: Math.random().toString(), | |
title: eventInput.title, | |
description: eventInput.title, | |
price: +eventInput.price, | |
date: new Date().toISOString(), | |
}; | |
events.push(event); | |
console.log(args); | |
return event; | |
}, | |
}, | |
graphiql: true, | |
}) | |
); | |
app.listen(3000, () => { | |
console.log('listening on port 3000'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment