diff --git a/packages/gatsby/src/schema/resolvers.ts b/packages/gatsby/src/schema/resolvers.ts index 3253f99d62247..1b4014cc8a774 100644 --- a/packages/gatsby/src/schema/resolvers.ts +++ b/packages/gatsby/src/schema/resolvers.ts @@ -1,6 +1,5 @@ import systemPath from "path" import normalize from "normalize-path" -import _ from "lodash" import { GraphQLList, GraphQLType, @@ -29,6 +28,9 @@ import { IGatsbyNode } from "../redux/types" type ResolvedLink = IGatsbyNode | Array | null +type nestedListOfStrings = Array +type nestedListOfNodes = Array + export function findMany( typeName: string ): GatsbyResolver { @@ -353,15 +355,22 @@ export function fileByPath( args, context, info - ): Promise { + ): Promise { const resolver = fieldConfig.resolve || context.defaultFieldResolver - const fieldValue = await resolver(source, args, context, { - ...info, - from: options.from || info.from, - fromNode: options.from ? options.fromNode : info.fromNode, - }) + const fieldValue: nestedListOfStrings = await resolver( + source, + args, + context, + { + ...info, + from: options.from || info.from, + fromNode: options.from ? options.fromNode : info.fromNode, + } + ) - if (fieldValue == null) return null + if (fieldValue == null) { + return null + } // Find the File node for this node (we assume the node is something // like markdown which would be a child node of a File node). @@ -370,34 +379,40 @@ export function fileByPath( node => node.internal && node.internal.type === `File` ) - const findLinkedFileNode = (relativePath: string): any => { - // Use the parent File node to create the absolute path to - // the linked file. - const fileLinkPath = normalize( - systemPath.resolve(parentFileNode.dir, relativePath) - ) + async function queryNodesByPath( + relPaths: nestedListOfStrings + ): Promise { + const arr: nestedListOfNodes = [] + for (let i = 0; i < relPaths.length; ++i) { + arr[i] = await (Array.isArray(relPaths[i]) + ? queryNodesByPath(relPaths[i] as nestedListOfStrings) + : queryNodeByPath(relPaths[i] as string)) + } + return arr + } - // Use that path to find the linked File node. - const linkedFileNode = _.find( - context.nodeModel.getAllNodes({ type: `File` }), - n => n.absolutePath === fileLinkPath - ) - return linkedFileNode + function queryNodeByPath(relPath: string): Promise { + return context.nodeModel.runQuery({ + query: { + filter: { + absolutePath: { + eq: normalize(systemPath.resolve(parentFileNode.dir, relPath)), + }, + }, + }, + firstOnly: true, + type: `File`, + }) } - return resolveValue(findLinkedFileNode, fieldValue) + if (Array.isArray(fieldValue)) { + return queryNodesByPath(fieldValue) + } else { + return queryNodeByPath(fieldValue) + } } } -function resolveValue( - resolve: (a: any) => any, - value: any | Array -): any | Array { - return Array.isArray(value) - ? value.map(v => resolveValue(resolve, v)) - : resolve(value) -} - function getProjectedField( info: GraphQLResolveInfo, fieldName: string