Repository: gatsbyjs/gatsby
File: packages/gatsby-source-drupal/src/utils.js (JavaScript)

Code before:
      headers,
      params,
    }),
  ])

  const enabledLanguages = (
    availableLanguagesResponse
    .data
    .data
    .filter(
      language => [
        `und`,
        `zxx`
      ].indexOf(language.attributes.drupal_internal__id) === -1
    )
    .map(language => language.attributes.drupal_internal__id)
  )

  const defaultLanguage = enabledLanguages[0]

  const translatableEntities = (
    translatableEntitiesResponse
    .data
    .data
    .map(entity => ({
      type: entity.attributes.target_entity_type_id,
      bundle: entity.attributes.target_bundle,
      id: `${
        entity.attributes.target_entity_type_id
      }--${
        entity.attributes.target_bundle
      }`
    }))
  )

  return {
    defaultLanguage,
    enabledLanguages,
    translatableEntities,
  }
}

exports.fetchLanguageConfig = fetchLanguageConfig

const handleReferences = (node, languageConfig, { getNode, createNodeId }) => {
  const relationships = node.relationships
  const rootNodeLanguage = node.langcode

  if (node.drupal_relationships) {
    const referencedNodes = []
    _.each(node.drupal_relationships, (v, k) => {

Review comment (suggestion):
Don't need "type" or "bundle" since we are using "id". Might be better to just create a list of type strings preformatted eg. `node--article`

Code after:
    const referencedNodes = []
    _.each(node.drupal_relationships, (v, k) => {
      if (!v.data) return
      const nodeFieldName = `${k}___NODE`
      if (_.isArray(v.data)) {
        relationships[nodeFieldName] = _.compact(
          v.data.map(data => {
            const referencedNodeId = createNodeId(
              createNodeIdWithVersion(
                data.id,
                data.type,
                rootNodeLanguage,
                data.meta?.target_version,
                entityReferenceRevisions
              )
            )
            if (!getNode(referencedNodeId)) {
              return null
            }

            referencedNodes.push(referencedNodeId)
            return referencedNodeId
          })
        )

        const meta = _.compact(
          v.data.map(data => (!_.isEmpty(data.meta) ? data.meta : null))
        )
        // If there's meta on the field and it's not an existing/internal one
        // create a new node's field with that meta. It can't exist on both
        // @see https://jsonapi.org/format/#document-resource-object-fields
        if (!_.isEmpty(meta) && !(k in node)) {
          node[k] = meta
        }
      } else {
        const referencedNodeId = createNodeId(
          createNodeIdWithVersion(
            v.data.id,
            v.data.type,
            rootNodeLanguage,
            v.data.meta?.target_revision_id,
            entityReferenceRevisions
          )
        )
        if (getNode(referencedNodeId)) {
          relationships[nodeFieldName] = referencedNodeId
          referencedNodes.push(referencedNodeId)
        }
        // If there's meta on the field and it's not an existing/internal one
        // create a new node's field with that meta. It can't exist on both
        // @see https://jsonapi.org/format/#document-resource-object-fields