Repository: gatsbyjs/gatsby
File: packages/gatsby-source-contentful/src/extend-node-type.js (JavaScript)

Code before:
          const rawEntries = Array.from(rawReferences.Entry)
          const rawAssets = Array.from(rawReferences.Asset)

          // Query for referenced nodes
          const resultEntries = await context.nodeModel.runQuery({
            query: {
              filter: {
                contentful_id: { in: rawEntries },
              },
            },
            type: `ContentfulEntry`,
          })

          const resultAssets = await context.nodeModel.runQuery({
            query: {
              filter: {
                contentful_id: { in: rawAssets },
              },
            },
            type: `ContentfulAsset`,
          })

          // Localize results
          const nodeLocale = parent.node_locale

          const findForLocaleWithFallback = (nodeList = [], referenceId) =>
            nodeList.find(
              result =>
                result.contentful_id === referenceId &&
                result.node_locale === nodeLocale
            )

          const localizedEntryReferences = rawEntries.map(referenceId =>
            findForLocaleWithFallback(resultEntries, referenceId)
          )

          const localizedAssetReferences = rawAssets.map(referenceId =>
            findForLocaleWithFallback(resultAssets, referenceId)
          )

          return [...localizedEntryReferences, ...localizedAssetReferences]
        },
      },
    }
  }

  if (type.name !== `ContentfulAsset`) {
    return {}
  }

  const getTracedSVG = async args => {

Review comment (performance):
It might be more efficient to tap into the nodes directly. But beware that this is effectively doing a `O(n*m)` search (size of all nodes of type `ContentfulEntry` * size of rawAssets), regardless. We do cache the nodes by type so that's at least a subset of all nodes. Ping me on slack to figure out how to do this in a more directly manner. We expose the api's anyways so might as well use them.

Code after:

          if (!rawReferences.Entry.size && !rawReferences.Asset.size) {
            return []
          }

          const rawEntries = Array.from(rawReferences.Entry)
          const rawAssets = Array.from(rawReferences.Asset)

          // Query for referenced nodes
          const resultEntries = await context.nodeModel.runQuery({
            query: {
              filter: {
                contentful_id: { in: rawEntries },
              },
            },
            type: `ContentfulEntry`,
          })

          const resultAssets = await context.nodeModel.runQuery({
            query: {
              filter: {
                contentful_id: { in: rawAssets },
              },
            },
            type: `ContentfulAsset`,
          })

          // Localize results
          const nodeLocale = parent.node_locale

          const findForLocaleWithFallback = (nodeList = [], referenceId) =>
            nodeList.find(
              result =>
                result.contentful_id === referenceId &&
                result.node_locale === nodeLocale
            )

          const localizedEntryReferences = rawEntries.map(referenceId =>
            findForLocaleWithFallback(resultEntries, referenceId)
          )

          const localizedAssetReferences = rawAssets.map(referenceId =>
            findForLocaleWithFallback(resultAssets, referenceId)
          )

          return [...localizedEntryReferences, ...localizedAssetReferences]
        },
      },
    }
  }