Gatsby: [source-drupal] weird behaviour with Drupal entity references when inside a paragraph

Created on 12 Nov 2019  ·  14Comments  ·  Source: gatsbyjs/gatsby

Description

There's some strange behaviour happening with I use an entity reference inside a paragraph with Drupal. I can see the JSON:API is formatting everything as I expect. If I have an entity reference field on its own in a content type Gatsby can see the data and I am able to query values of the reference in my local GraphiQL instance. However, as soon as that is nested inside a reference it just returns NULL

I've created two fields within my "Page" content type.

Screenshot 2019-11-12 at 14 59 13

field_test — An entity reference field

field_test2 — A paragraph field (entity reference revisions). Inside that field, there is an entity reference field called field_embed which is identical to field_test

Screenshot 2019-11-12 at 15 00 03

On my content that I am querying, I've referenced the same content. "Travel tip-offs: the best places to visit in August" (Node 56 in Drupal)

Screenshot 2019-11-12 at 15 01 24

Steps to reproduce

Either use my site's data using the following in gatsby-config.js

resolve: `gatsby-source-drupal`,
      options: {
        baseUrl: `https://app.master-7rqtwti-shreqbivaapoy.eu-2.platformsh.site/`,
        preview: true,
        apiBase: `api`, // optional, defaults to `jsonapi`
    }

or

Add a paragraph field to your content type in Drupal and then add an entity reference field in that paragraph.

Next, running the following query in GraphiQL

query pageQuery($drupal_id: String) {
  nodePage(drupal_id: {eq: $drupal_id}) {
    relationships {
      field_test {
        title
        drupal_internal__nid
        drupal_id
      }
      field_test2 {
        relationships {
          field_embed {
            title
            drupal_id
            drupal_internal__nid
          }
        }
      }
    }
  }
}

Expected result

field_embed returns the same information as field_test

Actual result

field_embed is returning NULL

Screenshot 2019-11-12 at 15 13 06

Environment

System:
OS: macOS 10.15.1
CPU: (8) x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
Shell: 3.2.57 - /bin/bash

Binaries:
Node: 8.11.1 - /usr/local/bin/node
Yarn: 1.7.0 - /usr/local/bin/yarn
npm: 6.12.1 - ~/.npm-packages/bin/npm
Languages:
Python: 2.7.15 - /usr/local/bin/python
Browsers:
Chrome: 78.0.3904.97
Firefox: 66.0.5
Safari: 13.0.3
npmPackages:
gatsby: ^2.17.2 => 2.17.2
gatsby-cli: ^2.8.5 => 2.8.5
gatsby-image: ^2.2.29 => 2.2.29
gatsby-plugin-manifest: ^2.2.23 => 2.2.23
gatsby-plugin-netlify-cache: ^1.2.0 => 1.2.0
gatsby-plugin-offline: ^2.2.10 => 2.2.10
gatsby-plugin-react-helmet: ^3.1.13 => 3.1.13
gatsby-plugin-sass: ^2.1.20 => 2.1.20
gatsby-plugin-sharp: ^2.2.32 => 2.2.32
gatsby-plugin-styled-components: ^3.1.11 => 3.1.11
gatsby-plugin-typography: ^2.3.15 => 2.3.15
gatsby-source-drupal: ^3.3.0 => 3.3.0
gatsby-source-filesystem: ^2.1.33 => 2.1.33
gatsby-transformer-sharp: ^2.3.0 => 2.3.0
npmGlobalPackages:
gatsby-cli: 2.8.0

bug

All 14 comments

@xaviemirmon I think we're running into a similar issue with paragraph and entity references. Question: Is field_test2 a multi value or single value field?

It seems like this is failing for single value (cardinality: 1) fields.

Ooh @arshad I think you are on to something here. The field itself is yes indeed a single value field. Nested inside a multivalue field. The non-nested field (field_test) is also a single value field though.

@xaviemirmon I found that if you change the following to relationships[nodeFieldName] = [referencedNodeId] it works.

https://github.com/gatsbyjs/gatsby/blob/1b72cd94b50689d78fafd230d6de3d3b37de1bd8/packages/gatsby-source-drupal/src/utils.js#L40

(This is a temporary fix while we figure out what's happening here)

@arshad This is great! That makes it work! Does anyone know why this couldn't or shouldn't be done this way? Or shall I create a PR with this change?

I also just ran into this issue and @arshad's suggested change resolved it.

@jonearley Nice. I created a PR so that we can get some feedback. https://github.com/gatsbyjs/gatsby/pull/19844

I suspect this would be a major version change for the plugin.

@jonearley Yeah let's see what breaks. Go CI :)

Hey @arshad I found the one fatal flaw in the otherwise perfect plan. 😬 When adding this bit of code, my site broke because multiple nested relationships started having an extra [0] key in the array e.g. field_example[0] This makes it a breaking change. That said I was thinking if we can find the items with a single cardinality later on in the code, we could probably just wrap that in an array to get working for both scenarios.

The plugin behaves as if the entity reference will only be of one type and doesn't account for an entity reference to multiple Drupal content types.

This bug blocks my work at the moment. I am considering forking and using the [] change for now until the plugin has an official fix.

It sounds like a duplicate of #10090 and could be potentially fixed in #19916 (should be published in gatsby 2.18.7)

Thanks @vladar I'll test this.

@vladar Bingo! 2.18.7 (#19916) fixed this. Thanks so much 👍

Great. Glad it helped!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mikestopcontinues picture mikestopcontinues  ·  3Comments

jimfilippou picture jimfilippou  ·  3Comments

hobochild picture hobochild  ·  3Comments

Oppenheimer1 picture Oppenheimer1  ·  3Comments

ghost picture ghost  ·  3Comments