1.0.1 • Published 5 years ago

graphql-combine v1.0.1

Weekly downloads
324
License
MIT
Repository
github
Last release
5 years ago

NPM version

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

// Dependencies
import { ApolloServer } from 'apollo-server'
import combine from 'graphql-combine'
import path from 'path'

// Get combined typeDefs and resolvers
const { typeDefs, resolvers } = combine({
  // TypeDefs glob pattern
  typeDefs: path.join(__dirname, 'graphql/*/schema.graphql'),
 
  // Resolvers glob pattern
  resolvers: path.join(__dirname, 'graphql/*/resolver.js')
})

// Initialize server
const server = new ApolloServer({ typeDefs, resolvers })

// Start the server
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 files
    • resolvers The glob pattern for all resolver files