![NPM version](https://img.shields.io/npm/v/graphql-combine.svg)
A better way to modularize your GraphQL schemas and resolver objects.
Getting started
Install it
$ npm install graphql-combine
Folder structure
graphql/
|-- author/
|-- schema.graphql
|-- resolver.js
|-- post/
|-- schema.graphql
|-- resolver.js
index.js
Files
File: graphql/author/schema.graphql
type Author {
id: Int!
firstName: String
lastName: String
books: [Book]
}
type Query {
author(id: Int!): Author
}
File: graphql/author/resolver.js
export default {
Query: {
author: () => {
}
}
}
File: graphql/book/schema.graphql
type Book {
title: String
author: Author
}
type Query {
book(id: Int!): Book
}
File: graphql/book/resolver.js
export default {
Query: {
book: () => {
}
}
}
Start the server
File: index.js
import { ApolloServer } from 'apollo-server'
import combine from 'graphql-combine'
import path from 'path'
const { typeDefs, resolvers } = combine({
typeDefs: path.join(__dirname, 'graphql/*/schema.graphql'),
resolvers: path.join(__dirname, 'graphql/*/resolver.js')
})
const server = new ApolloServer({ typeDefs, resolvers })
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`)
})
That's it 👍🏼
Example
Have a look at this simple example using graphql-combine
and apollo-server
.
API
combine(options)
The combine()
function is a top-level function exported by the graphql-combine
module.
options
typeDefs
The glob pattern for all schema filesresolvers
The glob pattern for all resolver files