Last active
June 22, 2019 05:18
-
-
Save haldarmahesh/293f56742ca06e1b361b733f1b82cdeb 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/controller/graphqlController.js | |
const graphqlHTTP = require('express-graphql'); | |
const { buildSchema } = require('graphql'); | |
// Construct a schema, using GraphQL schema language | |
const schema = buildSchema(` | |
type Query { | |
employee(id: Int!): Employee | |
employees: [Employee], | |
} | |
type Mutation { | |
createEmployee(name: String!, department: String!): Employee, | |
deleteEmployee(id: Int!): DeleteEmployeeResponse | |
} | |
input EmployeeInput { | |
name: String!, | |
department: String! | |
} | |
type DeleteEmployeeResponse { | |
id: Int! | |
} | |
type Employee { | |
id: Int!, | |
name: String!, | |
department: String! | |
} | |
`); | |
const rootResolver = { | |
employee: graphqlInput => employeesService.getById(graphqlInput && graphqlInput.id), | |
employees: employeesService.getAll(), | |
createEmployee: graphqlInput => employeesService.save(graphqlInput), | |
deleteEmployee: graphqlInput => employeesService.deleteById(graphqlInput.id), | |
}; | |
const graphql = graphqlHTTP({ | |
schema, | |
rootValue: rootResolver, | |
graphiql: true, // this creates the interactive GraphQL API explorer with documentation. | |
}); | |
module.exports = graphql; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment