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

Code before:
const _ = require(`lodash`)
const axios = require('axios')
const { nodeFromData, downloadFile, isFileNode } = require(`./normalize`)

const backRefsNamesLookup = new WeakMap()
const referencedNodesLookup = new WeakMap()

const fetchLanguageConfig = async ({ translation, baseUrl, apiBase, basicAuth, headers, params }) => {
  if (!translation) {
    return {
      defaultLanguage: 'und',
      enabledLanguages: ['und'],
      translatableEntities: [],
    }
  }

  const [availableLanguagesResponse, translatableEntitiesResponse] = await Promise.all([
    axios.get(`${baseUrl}/${apiBase}/configurable_language/configurable_language?sort=weight`, {
      auth: basicAuth,
      headers,
      params,
    }),
    axios.get(`${baseUrl}/${apiBase}/language_content_settings/language_content_settings?filter[language_alterable]=true`, {
      auth: basicAuth,
      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

Review comment (bug):
One minor note is that JSONAPI will only return 50 results. In the off chance the site has more than 50 languages enabled or more than 50 entity types with language selection enabled, this will miss any that are not on the first page.

I think this is an edge case, but might be worth accounting for now. We would need to check that data.links.next does not have a URL. If it does then use axios.get and combine the results.

Code after:
const _ = require(`lodash`)
const axios = require(`axios`)

const {
  nodeFromData,
  downloadFile,
  isFileNode,
  getHref,
  createNodeIdWithVersion,
} = require(`./normalize`)

const { getOptions } = require(`./plugin-options`)

const backRefsNamesLookup = new WeakMap()
const referencedNodesLookup = new WeakMap()

const handleReferences = (
  node,
  { getNode, createNodeId, entityReferenceRevisions = [] }
) => {
  const relationships = node.relationships
  const rootNodeLanguage = getOptions().languageConfig ? node.langcode : `und`

  if (node.drupal_relationships) {
    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
          })
        )