Comparing version 5.3.2 to 5.4.0
'use strict'; | ||
const Assert = require('assert'); | ||
const Boom = require('boom'); | ||
@@ -102,3 +103,43 @@ const Graphql = require('graphql'); | ||
// Inspired by graphql-tools | ||
exports.makeExecutableSchema = ({ schema, resolvers = {} }) => { | ||
const parsed = Graphql.parse(schema); | ||
const astSchema = Graphql.buildASTSchema(parsed, { commentDescriptions: true }); | ||
for (const resolverName of Object.keys(resolvers)) { | ||
const type = astSchema.getType(resolverName); | ||
Assert(type, `Missing schema definition for resolver: ${resolverName}`); | ||
const typeResolver = resolvers[resolverName]; | ||
// go through field resolvers for the parent resolver type | ||
for (const fieldName of Object.keys(typeResolver)) { | ||
const fieldResolver = typeResolver[fieldName]; | ||
Assert(typeof fieldResolver === 'function', `${resolverName}.${fieldName} resolver must be a function`); | ||
if (type instanceof Graphql.GraphQLScalarType) { | ||
type[fieldName] = fieldResolver; | ||
continue; | ||
} | ||
if (type instanceof Graphql.GraphQLEnumType) { | ||
const fieldType = type.getValue(fieldName); | ||
Assert(fieldType, `${resolverName}.${fieldName} enum definition missing from schema`); | ||
fieldType.value = fieldResolver; | ||
continue; | ||
} | ||
// no need to set resolvers unless we are dealing with a type that needs resolvers | ||
if (!(type instanceof Graphql.GraphQLObjectType) && !(type instanceof Graphql.GraphQLInterfaceType)) { | ||
continue; | ||
} | ||
const fields = type.getFields(); | ||
fields[fieldName].resolve = fieldResolver; | ||
} | ||
} | ||
return astSchema; | ||
}; | ||
internals.graphqlHandler = async function (request, h) { | ||
@@ -105,0 +146,0 @@ if (request.method.toUpperCase() === 'OPTIONS') { |
{ | ||
"name": "graphi", | ||
"version": "5.3.2", | ||
"version": "5.4.0", | ||
"description": "hapi graphql plugin", | ||
@@ -37,3 +37,3 @@ "main": "lib", | ||
"boom": "7.x.x", | ||
"graphql": "0.12.x", | ||
"graphql": "0.13.x", | ||
"graphql-server-module-graphiql": "1.3.x", | ||
@@ -40,0 +40,0 @@ "lodash.merge": "4.6.x" |
@@ -16,3 +16,7 @@ # graphi | ||
## API | ||
- `graphql` - exported Graphql module that graphi uses | ||
- `makeExecutableSchema({ schema, resolvers })` - combine resolvers with the schema definition into a `GraphQLSchema`. | ||
## Usage | ||
@@ -19,0 +23,0 @@ |
@@ -935,3 +935,88 @@ 'use strict'; | ||
describe('makeExecutableSchema()', () => { | ||
it('converts a graphql schema into executable graphql objects', () => { | ||
const schema = ` | ||
input Someone { | ||
name: String | ||
} | ||
interface IPerson { | ||
firstname: String | ||
} | ||
type Person implements IPerson { | ||
firstname: String! | ||
lastname: String! | ||
email: String! | ||
description: People | ||
ability: Ability | ||
search: SearchResult | ||
} | ||
scalar People | ||
enum Ability { | ||
COOK | ||
PROGRAM | ||
} | ||
union SearchResult = Person | String | ||
type Query { | ||
person(firstname: String!): Person! | ||
} | ||
`; | ||
const resolvers = { | ||
Query: { | ||
person: () => {} | ||
}, | ||
People: { | ||
description: () => {} | ||
}, | ||
Ability: { | ||
COOK: () => {} | ||
}, | ||
Person: { | ||
ability: () => {}, | ||
description: () => {}, | ||
search: () => {} | ||
}, | ||
IPerson: { | ||
firstname: () => {} | ||
}, | ||
Someone: { | ||
name: () => {} | ||
} | ||
}; | ||
const executable = Graphi.makeExecutableSchema({ schema, resolvers }); | ||
expect(executable instanceof Graphi.graphql.GraphQLSchema).to.be.true(); | ||
}); | ||
it('errors when resolver missing from schema', () => { | ||
const schema = ` | ||
type Person { | ||
firstname: String! | ||
lastname: String! | ||
email: String! | ||
} | ||
type Query { | ||
person(firstname: String!): Person! | ||
} | ||
`; | ||
let err; | ||
try { | ||
Graphi.makeExecutableSchema({ schema, resolvers: { Query: { human: () => {} } } }); | ||
} catch (ex) { | ||
err = ex; | ||
} | ||
expect(err).to.be.error(); | ||
}); | ||
}); | ||
// auth token strategy plugin | ||
@@ -938,0 +1023,0 @@ |
56246
1043
101
+ Addedgraphql@0.13.2(transitive)
+ Addediterall@1.3.0(transitive)
- Removedgraphql@0.12.3(transitive)
- Removediterall@1.1.3(transitive)
Updatedgraphql@0.13.x