0.0.5 • Published 7 years ago
vtex-graphql-builder v0.0.5
vtex-graphql-builder
Helper for building vtex apps with graphql resolvers
Usage
When creating an app for masterdata-graphql, some annoying common boilerplate is needed for the colossus resolvers, such as:
- Parsing the request's body with a module like
co-body
. - Manually selecting what resolver to run based on the request's path.
- Building error responses in the right format (i.e.
{error: 'Error message here'}
). - Setting the final response's status code and type
This tool can help minimize this boilerplate, letting you just define the actual resolver logic you need.
For example, let's say you have the following schema:
type Book {
id: String
name: String
author: String
}
type Query {
books: [Book]
book(id: String): Book
}
type Mutation {
deleteBook(id: String): Boolean
}
Then, with vtex-graphql-builder
you can build resolvers for these by having a file
like this at service/main.js
:
const {buildResolvers} = require('vtex-graphql-builder')
export default buildResolvers({
Query: {
book: async ({data, root, cookie}, {account, workspace}, originalReq) => {
// Handle the `book` query here and return the results as {data}.
return {data: results}
},
books: async ({data, root, cookie}, {account, workspace}, originalReq) => {
// Handle the `books` query here and return the results as {data}.
return {data: results}
}
},
Mutation: {
deleteBook: async ({data, root, cookie}, {account, workspace}, originalReq) => {
// Handle the `deleteBook` mutation here and return the results as {data}.
return {data: results}
}
},
})