Skip to content
This repository has been archived by the owner on Jun 9, 2019. It is now read-only.

modificated GraphQL modules library for Apollo server

License

Notifications You must be signed in to change notification settings

cognitivim/graphql-modules

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GraphQL Modules

A library to simplify modularization of Apollo server applications.

This is the fork!

Original repository: https://github.com/lucasconstantino/graphql-modules

Changes:

  • fixed resolvers root keys
  • migrate default options to new graphql naming convention (without Root-)

Installation

git submodules tool: mgit2

Basic usage

// module-a.js
// -----------
const moduleA = {
  queries: `
    helloWorld: String
  `
  resolvers: {
    queries: {
      helloWorld: () => 'Hello World!'
    }
  }
}

export default moduleA

// module-b.js
// -----------
const moduleB = {
  queries: `
    helloYou(name: String): String
  `,
  resolvers: {
    queries: {
      helloYou: (root, { name }) => `Hello ${name}!`
    }
  }
}

export default moduleB

// schema.js
// ---------
import { bundle } from 'graphql-modules'
import { makeExecutableSchema } from 'graphql-tools'

import moduleA from './module-a'
import moduleB from './module-b'

const modules = [moduleA, moduleB]


export default makeExecutableSchema(bundle(modules))

Complex needs

This library handles complex situations such as cyclic dependencies between modules. For that to work, each module can be a factory function - besides being a module object. That allows for dependencies to be handled on runtime, making ES6 module binding work as desired. This is pretty much what is proposed by the official Apollo docs.

Say you have a system with books and authors - two modules that are interdependent; books have authors and authors have books. You could end up with something like this:

`/books.js` ```js import authors, { data as authorList } from './authors'

export const data = [ { id: 1, title: 'JavaScript: The Good Parts', author: 1 }, { id: 2, title: 'End to end testing with Protractor', author: 2 } ]

const schema = type Book { id: String title: String author: Author }

export const queries = books(): [Book] book(id: Int): Book const books = () => data const book = (root, args) => data.find(book => book.id === args.id)

const resolvers = { queries: { books, book }, Book: { author: book => authorList.find(author => author.id === book.author) } }

export default = () => ({ schema, queries, resolvers, modules: [authors] })

</details>

In this file, we define a schema, queries, and resolvers. At the end, we export those assets in a single object - the module.

<details>
<summary>`/authors.js`</summary>
```js
import books, { data as bookList } from './books'

export const data = [
   { id: 1, name: 'Douglas Crockford' },
   { id: 2, name: 'Walmyr Lima' }
]

const schema = `
   type Author {
     id: String
     name: String
     books: [Book]
   }
`

export const queries = `
   authors(): [Author]
   author(id: Int): Author
`
const authors = () => data
const author = (root, args) => data.find(author => author.id === args.id)

const resolvers = {
  queries: {
    authors,
    author
  },
  Author: {
    books: author => bookList.filter(book => book.author === author.id)
  }
}

export default () => ({
  schema,
  queries,
  resolvers,
  modules: [books]
})

This file is almost copy and paste from the previous.

`/schema.js` ```js import { bundle } from 'graphql-modules' import { makeExecutableSchema } from 'graphql-tools'

import books from './books'

const modules = [books]

export default makeExecutableSchema(bundle(modules))

</details>

At this last file, we create our schema (for this example, we are using [graphql-tools](https://github.com/apollostack/graphql-tools)'s `makeExecutableSchema`).

## Further steps

This project is a work in process, a proof of concept, and can be expanded as wish. I believe this should some day be integrated into the *graphql-tools* project somehow.

About

modificated GraphQL modules library for Apollo server

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%