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

Commit

Permalink
support for enum types inside nested
Browse files Browse the repository at this point in the history
  • Loading branch information
gyzerok committed Oct 20, 2015
1 parent a11b262 commit ad56e8d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
2 changes: 2 additions & 0 deletions example/src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { Adrenaline } from '../../../src';
import schema from 'shared/schema';
import Loader from './components/Loader';

console.log(schema.getTypeMap());

const rootNode = document.getElementById('root');
React.render(
<Adrenaline schema={schema} renderLoading={Loader}>
Expand Down
17 changes: 17 additions & 0 deletions example/src/shared/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
GraphQLInt,
GraphQLList,
GraphQLNonNull,
GraphQLEnumType,
GraphQLSchema,
} from 'graphql';

Expand Down Expand Up @@ -66,6 +67,18 @@ const userType = new GraphQLObjectType({
}),
});

const enumType = new GraphQLEnumType({
name: 'Test',
values: {
ONE: {
value: 1,
},
TWO: {
value: 2,
},
},
});

export default new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
Expand All @@ -79,6 +92,10 @@ export default new GraphQLSchema({
return root.findUser();
},
},
test: {
type: enumType,
resolve: () => 1,
},
}),
}),
mutation: new GraphQLObjectType({
Expand Down
18 changes: 14 additions & 4 deletions src/utils/parseSchema.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
/* @flow */

import { reduce } from 'lodash';
import { GraphQLList, GraphQLNonNull, GraphQLScalarType } from 'graphql';
import {
GraphQLList,
GraphQLNonNull,
GraphQLScalarType,
GraphQLEnumType,
} from 'graphql';

export default function parseSchema(schema) {
const typeMap = schema.getTypeMap();
const userTypes = reduce(typeMap, (acc, val, key) => {
if (key.startsWith('__')) return acc;
if (isScalar(val)) return acc;
if (isNested(val)) return acc;
if (isScalar(val) || isEnum(val) || isNested(val)) return acc;

return {
...acc,
Expand Down Expand Up @@ -44,6 +48,10 @@ function isScalar(type) {
return type instanceof GraphQLScalarType;
}

function isEnum(type) {
return type instanceof GraphQLEnumType;
}

function isDefined(type) {
return !(
isList(type)
Expand All @@ -54,7 +62,9 @@ function isDefined(type) {

function isNested(type) {
return (
!type.getFields().hasOwnProperty('id')
!isScalar(type)
&& !isEnum(type)
&& !type.getFields().hasOwnProperty('id')
&& type.name !== 'Query'
&& type.name !== 'Mutation'
);
Expand Down

0 comments on commit ad56e8d

Please sign in to comment.