Last active
October 23, 2017 20:22
-
-
Save rlancer/7fe3e7cab42ab00ed00ee1aee5e9fb92 to your computer and use it in GitHub Desktop.
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
const { GraphQLSchema, GraphQLObjectType, GraphQLString, GraphQLID, GraphQLFloat, GraphQLInt, GraphQLNonNull, GraphQLList } = require('graphql') | |
const express = require('express') | |
const graphqlHTTP = require('express-graphql') | |
const DataLoader = require('dataloader') | |
// Spoofed data | |
const DATA_PAYMENT = [ | |
{ | |
id: '1', | |
user_id: '1', | |
message: 'For logo creation', | |
amount: 33 | |
}, | |
// ... | |
] | |
const DATA_USERS = { | |
'1': { id: '1', name: 'Jen' }, | |
// ... | |
} | |
const User = new GraphQLObjectType({ | |
name: 'User', | |
fields: { | |
id: { type: GraphQLString }, | |
name: { type: GraphQLString } | |
} | |
}) | |
const Payment = new GraphQLObjectType({ | |
name: 'Payment', | |
fields: { | |
id: { type: GraphQLID }, | |
user_id: { type: GraphQLString }, | |
message: { type: GraphQLString }, | |
amount: { type: GraphQLFloat }, | |
user: { | |
type: User, | |
resolve: ({ user_id }, _, ctx) => ctx.userLoader.load(user_id) | |
} | |
}, | |
date_created: { type: GraphQLString } | |
}) | |
const queryType = new GraphQLObjectType({ | |
name: 'RootQueryType', | |
fields: { | |
payments: { | |
type: new GraphQLList(Payment), | |
async resolve(_, args) { | |
return DATA_PAYMENT | |
} | |
} | |
} | |
}) | |
const schema = new GraphQLSchema({ query: queryType }) | |
const app = express() | |
const loadUsers = async keys => keys.map(key => DATA_USERS[key]) | |
app.use( | |
'/graphql', | |
graphqlHTTP({ | |
schema, | |
graphiql: true, | |
context: { | |
userLoader: new DataLoader(async keys => await loadUsers(keys)) | |
} | |
}) | |
) | |
const port = 4000 | |
app.listen(port) | |
console.log('Listing on port ', port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment