Skip to content

Commit

Permalink
use official API to get typeMap and fields from schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Helfer committed Apr 4, 2016
1 parent e4d8687 commit 5abb2b3
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/schemaGenerator.js
Expand Up @@ -2,7 +2,11 @@

import { parse } from 'graphql/language';
import { buildASTSchema } from 'graphql/utilities';
import { GraphQLScalarType, getNamedType } from 'graphql/type';
import {
GraphQLScalarType,
getNamedType,
GraphQLObjectType,
} from 'graphql/type';

// @schemaDefinition: A GraphQL type schema in shorthand
// @resolvers: Definitions for resolvers to be merged with schema
Expand Down Expand Up @@ -44,12 +48,14 @@ const generateSchema = (
};

function forEachField(schema, fn) {
Object.keys(schema._typeMap).forEach((typeName) => {
const type = schema._typeMap[typeName];

if (!getNamedType(type).name.startsWith('__') && type._fields) {
Object.keys(type._fields).forEach((fieldName) => {
const field = type._fields[fieldName];
const typeMap = schema.getTypeMap();
Object.keys(typeMap).forEach((typeName) => {
const type = typeMap[typeName];

if (!getNamedType(type).name.startsWith('__') && type instanceof GraphQLObjectType) {
const fields = type.getFields();
Object.keys(fields).forEach((fieldName) => {
const field = fields[fieldName];
fn(field, typeName, fieldName);
});
}
Expand All @@ -58,20 +64,20 @@ function forEachField(schema, fn) {

function addResolveFunctionsToSchema(schema, resolveFunctions) {
Object.keys(resolveFunctions).forEach((typeName) => {
const type = schema._typeMap[typeName];
const type = schema.getType(typeName);
if (!type) {
throw new SchemaError(
`"${typeName}" defined in resolvers, but not in schema`
);
}

Object.keys(resolveFunctions[typeName]).forEach((fieldName) => {
if (!type._fields[fieldName]) {
if (!type.getFields()[fieldName]) {
throw new SchemaError(
`${typeName}.${fieldName} defined in resolvers, but not in schema`
);
}
const field = type._fields[fieldName];
const field = type.getFields()[fieldName];
field.resolve = resolveFunctions[typeName][fieldName];
});
});
Expand Down

0 comments on commit 5abb2b3

Please sign in to comment.