Last active
February 26, 2018 01:30
-
-
Save nicolasdao/418aaa279c85dd8fb2bd7e4edc5753c4 to your computer and use it in GitHub Desktop.
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
// src/schema.js | |
const { makeExecutableSchema } = require('graphql-tools') | |
const _ = require('lodash') | |
const schema = ` | |
type Product { | |
id: ID! | |
name: String! | |
shortDescription: String | |
} | |
type Variant { | |
id: ID! | |
name: String! | |
shortDescription: String | |
} | |
type Query { | |
# ### GET products | |
# | |
# _Arguments_ | |
# - **id**: Product's id (optional) | |
products(id: Int): [Product] | |
# ### GET variants | |
# | |
# _Arguments_ | |
# - **id**: Variant's id (optional) | |
variants(id: Int): [Variant] | |
} | |
` | |
const productMocks = [{ id: 1, name: 'Product A', shortDescription: 'First product.' }, { id: 2, name: 'Product B', shortDescription: 'Second product.' }] | |
const productResolver = { | |
Query: { | |
products(root, { id }, context) { | |
const results = id ? productMocks.filter(p => p.id == id) : productMocks | |
if (results.length > 0) | |
return results | |
else | |
throw new Error(`Product with id ${id} does not exist.`) | |
} | |
} | |
} | |
const variantMocks = [{ id: 1, name: 'Variant A', shortDescription: 'First variant.' }, { id: 2, name: 'Variant B', shortDescription: 'Second variant.' }] | |
const variantResolver = { | |
Query: { | |
variants(root, { id }, context) { | |
const results = id ? variantMocks.filter(p => p.id == id) : variantMocks | |
if (results.length > 0) | |
return results | |
else | |
throw new Error(`Variant with id ${id} does not exist.`) | |
} | |
} | |
} | |
const executableSchema = makeExecutableSchema({ | |
typeDefs: schema, | |
resolvers: _.merge(productResolver, variantResolver) | |
}) | |
module.exports = { | |
executableSchema | |
} |
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
// index.js | |
const { app } = require('webfunc') | |
const { graphqlHandler } = require('graphql-serverless') | |
const { executableSchema } = require('./src/schema') | |
const graphqlOptions = { | |
schema: executableSchema, | |
graphiql: { | |
endpoint: '/graphiql' | |
} | |
} | |
app.all(['/', '/graphiql'], graphqlHandler(graphqlOptions)) | |
app.listen(4000) |
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
- src/ | |
|__ schema.js | |
- index.js | |
- package.json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment