From c28e59ed8baddc16cefa1784629569eeeda68046 Mon Sep 17 00:00:00 2001 From: nodkz Date: Mon, 28 Mar 2016 15:37:47 +0600 Subject: [PATCH] fix(type): add to input-object ability for self-nesting --- src/type/custom/to-input-object.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/type/custom/to-input-object.js b/src/type/custom/to-input-object.js index ff19b93..2c57aa3 100644 --- a/src/type/custom/to-input-object.js +++ b/src/type/custom/to-input-object.js @@ -7,14 +7,24 @@ import { GraphQLScalarType, GraphQLInputObjectType, GraphQLEnumType, - GraphQLID, + GraphQLID } from 'graphql'; +const cachedTypes = {}; + function createInputObject(type) { - return new GraphQLInputObjectType({ - name: `${type.name}Input`, - fields: filterFields(type.getFields(), (field) => (!field.noInputObject)), // eslint-disable-line - }); + const typeName = `${type.name}Input`; + + if (!cachedTypes.hasOwnProperty(typeName)) { + cachedTypes[typeName] = new GraphQLInputObjectType({ + name: typeName, + fields: {} + }); + cachedTypes[typeName]._typeConfig.fields = + () => filterFields(type.getFields(), (field) => (!field.noInputObject)); // eslint-disable-line + } + + return cachedTypes[typeName]; } function filterFields(obj, filter) {