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

Commit

Permalink
Added field resolvers (fixes #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasconstantino committed Nov 21, 2016
1 parent 42d6f63 commit 39cfc9e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ Say you have a system with books and authors - two modules that are interdepende
<details>
<summary>`/books.js`</summary>
```js
import authors from './authors'
import authors, { data as authorList } from './authors'

const data = [
export const data = [
{ id: 1, title: 'JavaScript: The Good Parts', author: 1 },
{ id: 2, title: 'End to end testing with Protractor', author: 2 }
]
Expand All @@ -96,6 +96,9 @@ Say you have a system with books and authors - two modules that are interdepende
queries: {
books,
book
},
Book: {
author: book => authorList.find(author => author.id === book.author)
}
}

Expand All @@ -113,9 +116,9 @@ In this file, we define a schema, queries, and resolvers. At the end, we export
<details>
<summary>`/authors.js`</summary>
```js
import books from './books'
import books, { data as bookList } from './books'

const data = [
export const data = [
{ id: 1, name: 'Douglas Crockford' },
{ id: 2, name: 'Walmyr Lima' }
]
Expand All @@ -139,6 +142,9 @@ In this file, we define a schema, queries, and resolvers. At the end, we export
queries: {
authors,
author
},
Author: {
books: author => bookList.filter(book => book.author === author.id)
}
}

Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"name": "graphql-modules",
"version": "0.1.4",
"version": "0.1.5",
"description": "GraphQL module library for Apollo.",
"main": "index.js",
"scripts": {
"build": "babel src -d lib",
"compile": "babel src -d lib",
"lint": "eslint index.js src",
"test": "mocha --require babel-register",
"test:watch": "npm test -- --watch",
"prepublish": "npm test && npm run lint && npm run build"
"prepublish": "npm test && npm run lint && npm run compile"
},
"keywords": [
"graphql",
Expand Down
2 changes: 2 additions & 0 deletions src/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ export default (modules = [], options = {}) => {
const queriesResolvers = Object.assign.apply(null, modules.map(module => module.resolvers && module.resolvers.queries || {}))
const mutationsResolvers = Object.assign.apply(null, modules.map(module => module.resolvers && module.resolvers.mutations || {}))
const subscriptionsResolvers = Object.assign.apply(null, modules.map(module => module.resolvers && module.resolvers.subscriptions || {}))
const fieldResolvers = Object.assign.apply(null, modules.map(({ resolvers: { queries, mutations, subscriptions, ...fieldResolvers } = {} }) => fieldResolvers))

const resolvers = {
...(queries ? { RootQuery: queriesResolvers } : {}),
...(mutations ? { RootMutation: mutationsResolvers } : {}),
...(subscriptions ? { RootSubscription: subscriptionsResolvers } : {}),
...fieldResolvers
}

const RootQuery = queries.length ? `
Expand Down
15 changes: 15 additions & 0 deletions test/bundle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ describe('bundle', () => {
expect(resolvers).to.have.property('RootQuery')
})

it('should register field resolvers', () => {
const module = {
resolvers: {
Type: { field: () => null }
},
}
const { resolvers } = bundle([module])

expect(resolvers).to.have.property('Type')
})

it('should register multiple module resolvers', () => {
const moduleA = {
queries: 'queryA(arg: String): String',
Expand Down Expand Up @@ -256,3 +267,7 @@ describe('bundle', () => {
})
})
})

describe('execution', () => {
it('should have execution tests using makeExecutableSchema')
})

0 comments on commit 39cfc9e

Please sign in to comment.