datocms / gatsby-source-datocms

Official GatsbyJS source plugin to pull content from DatoCMS
MIT License
140 stars 51 forks source link

[BUG] Gatsby custom resolvers error in v2.5.0+ #128

Closed tbrannam closed 3 years ago

tbrannam commented 3 years ago

Some custom resolvers that we have implemented fail when using gatsby-source-datocms version 2.5.0+.

The shape of the first argument of the resolver (shown in gatsby-node.js in the examples) receives different shapes of data between these versions.

resolve: async (item, { siteId, currency }, context)

Note that the CMS Model fields in nested into entityPayload.attributes instead of being at the root in prior versions

catalogItem {
  id: 'DatoCmsCatalogItem-xxxx-en-US',
  parent: null,
  children: [],
  internal: {
    type: 'DatoCmsCatalogItem',
    contentDigest: '2020-08-07T20:48:29.942+01:00',
    counter: 155,
    owner: 'gatsby-source-datocms'
  },
  locale: 'en-US',
  entityPayload: {
    id: '5808923',
    type: 'item',
    attributes: {
      product_id: 1,
      sku: 'xxxxx',
      product: '5808943',
      site_id: 'xxxx',
      gatsbypreview: null,
      updated_at: '2020-08-07T20:48:29.942+01:00',
      created_at: '2020-08-07T17:26:05.010+01:00'
    },
    relationships: { item_type: [Object], creator: [Object] },
    meta: {
      created_at: '2020-08-07T17:26:05.010+01:00',
      updated_at: '2020-08-07T20:48:29.942+01:00',
      published_at: '2020-08-07T20:48:29.962+01:00',
      publication_scheduled_at: null,
      first_published_at: '2020-08-07T17:26:05.020+01:00',
      is_valid: true,
      status: 'published',
      current_version: '10677674'
    }
  },
  seoMetaTags___NODE: 'DatoCmsSeoMetaTags-DatoCmsCatalogItem-5808923-en-US',
  __gatsby_resolved: { product: {}, siteId: 'xxxx', locale: 'en-US' }
}

I've isolated the code into these two sandboxes. They require an update gatsby-config.js with a Dato apiKey.

https://codesandbox.io/s/dato-source-graphql-v241-public-p35io?file=/README.md

https://codesandbox.io/s/dato-source-graphql-v250-public-z4wfs?file=/README.md

Using one of my Dato environments I can make this query using 2.4.1, but fails on 2.5.0 and newer

query MyQuery {
  datoCmsProductPage {
    defaultCatalogItem {
      price(currency: USD, siteId: "flamingo_us") {
        value
        currency
      }
    }
  }
}
stefanoverna commented 3 years ago

Yes, in recent versions, to achieve better loading performance, we had to make changes to how we store information inside the nodes, so yeah, the correct way now to read information is using source.entityPayload which returns exactly the JSON API response of our CMA with no transformations