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

Commit

Permalink
nested types support
Browse files Browse the repository at this point in the history
  • Loading branch information
gyzerok committed Sep 23, 2015
1 parent 1ca8463 commit 3a93ac4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
16 changes: 16 additions & 0 deletions src/utils/__tests__/parseSchema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const todoType = new GraphQLObjectType({
},
text: {
type: GraphQLString,
},
nested: {
type: new GraphQLList(nestedType),
}
})
});
Expand All @@ -33,10 +36,23 @@ const userType = new GraphQLObjectType({
},
todos: {
type: new GraphQLList(todoType),
},
nested: {
type: nestedType,
}
})
});

const nestedType = new GraphQLObjectType({
name: 'Nested',
fields: () => ({
hello: {
type: GraphQLString,
resolve: () => 'world',
}
}),
});

const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
Expand Down
2 changes: 1 addition & 1 deletion src/utils/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function normalize(parsedSchema, data) {
keys.every(key => parsedSchema.Mutation.hasOwnProperty(key))
);

let bag = {};
const bag = {};
if (isQuery) {
normalizeAny(parsedSchema, 'Query', bag, data);
}
Expand Down
20 changes: 14 additions & 6 deletions src/utils/parseSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ export default function parseSchema(schema) {
const userTypes = reduce(typeMap, (acc, val, key) => {
if (key.startsWith('__')) return acc;
if (isScalar(val)) return acc;
if (isNested(val)) return acc;

return {
...acc,
[key]: {
...reduce(val.getFields(), (memo, field, name) => {
const typename = parseType(field.type);
return typename !== undefined ? { ...memo, [name]: typename } : memo;
return typename ? { ...memo, [name]: typename } : memo;
}, {}),
},
};
Expand All @@ -22,14 +24,12 @@ export default function parseSchema(schema) {
}

function parseType(type) {
if (isDefined(type)) return type.name;
if (isDefined(type) && !isNested(type)) return type.name;
if (isComplex(type)) return parseType(type.ofType);
if (isList(type)) {
const typename = type.ofType.name;
return isDefined(typename) ? ([typename]) : undefined;
const childType = type.ofType;
if (isDefined(childType) && !isNested(childType)) return [childType.name];
}

return undefined;
}

function isComplex(type) {
Expand All @@ -51,3 +51,11 @@ function isDefined(type) {
isScalar(type)
);
}

function isNested(type) {
return (
!type.getFields().hasOwnProperty('id') &&
type.name !== 'Query' &&
type.name !== 'Mutation'
);
}

0 comments on commit 3a93ac4

Please # to comment.