From 5bb3da534cceaf310bea1d12ab860c055ce0f688 Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Fri, 26 Jan 2018 09:10:20 +0100 Subject: [PATCH] move trying to infer Files from string paths to inferGraphQLType function where rest of value based inferring is done --- .../gatsby/src/schema/infer-graphql-type.js | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/gatsby/src/schema/infer-graphql-type.js b/packages/gatsby/src/schema/infer-graphql-type.js index 577c280e31e0b..3999e69336bcb 100644 --- a/packages/gatsby/src/schema/infer-graphql-type.js +++ b/packages/gatsby/src/schema/infer-graphql-type.js @@ -40,11 +40,21 @@ export type ProcessedNodeType = { function inferGraphQLType({ exampleValue, selector, + nodes, + types, ...otherArgs }): ?GraphQLFieldConfig<*, *> { if (exampleValue == null || isEmptyObjectOrArray(exampleValue)) return null let fieldName = selector.split(`.`).pop() + // Check this before checking for array as FileType has + // builtin support for inferring array of files and inferred + // array type will have faster resolver than resolving array + // of files separately. + if (FileType.shouldInfer(nodes, selector, exampleValue)) { + return _.isArray(exampleValue) ? FileType.getListType() : FileType.getType() + } + if (Array.isArray(exampleValue)) { exampleValue = exampleValue[0] @@ -54,6 +64,8 @@ function inferGraphQLType({ ...otherArgs, exampleValue, selector, + nodes, + types, }) invariant( inferredType, @@ -100,6 +112,8 @@ function inferGraphQLType({ ...otherArgs, exampleValue, selector, + nodes, + types, }), }), } @@ -329,13 +343,6 @@ export function inferObjectStructureFromNodes({ } else if (_.includes(key, `___NODE`)) { ;[fieldName] = key.split(`___`) inferredField = inferFromFieldName(value, nextSelector, types) - - // Third if the field (whether a string or array of string(s)) is - // pointing to a file (from another file). - } else if (FileType.shouldInfer(nodes, nextSelector, value)) { - inferredField = _.isArray(value) - ? FileType.getListType() - : FileType.getType() } // Finally our automatic inference of field value type.