Gatsby: [gatsby-source-contentful] Contentful JSON Object field - I'd like to query for the entire JSON object, but instead I have to query for each key.

Created on 13 Jun 2019  路  2Comments  路  Source: gatsbyjs/gatsby

Summary

I have a JSON Object field in my Contentful content Page model where I am adding a JSON object (for Google structured data). I want to query for that entire JSON object back in my GraphQL response, to then pass through to my SEO component to a <script type="application/ld+json">.

It seems I have to explicitly query for each key in my JSON object. This is very cumbersome, as we have several types of structured data (social profiles, business, breadcrumbs) which will all have different keys.

For example, my JSON object is:
{ "hello": "hi", "goodbye": "bye" }

Can there be some way to query it like this:
contentfulEntry { jsonObject { jsonObject } }

which gives me the entire object, rather than what I am currently limited to, which is:

contentfulEntry { jsonObject { hello goodbye } }

I see this as possibly being similar to the Contentful Markdown field, in that I can get the entire Markdown back in one response.

Is there a way to get back the entire JSON object? If not, could this be something to be added to the gatsby-source-contentful plugin?

gatsby info --clipboard

System:
OS: macOS 10.14.2
CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 8.9.1 - /usr/local/bin/node
Yarn: 1.3.2 - /usr/local/bin/yarn
npm: 6.1.0 - /usr/local/bin/npm
Languages:
Python: 2.7.15 - /usr/local/bin/python
Browsers:
Chrome: 75.0.3770.80
Firefox: 64.0.2
Safari: 12.0.2
npmPackages:
gatsby: ^1.9.273 => 1.9.273
gatsby-link: ^1.6.40 => 1.6.45
gatsby-plugin-algolia: ^0.0.3 => 0.0.3
gatsby-plugin-create-client-paths: ^1.0.8 => 1.0.8
gatsby-plugin-google-tagmanager: ^1.0.19 => 1.0.19
gatsby-plugin-lodash: ^1.0.11 => 1.0.11
gatsby-plugin-postcss-sass: 1.0.19 => 1.0.19
gatsby-plugin-react-helmet: ^2.0.10 => 2.0.11
gatsby-plugin-sass: 1.0.19 => 1.0.19
gatsby-source-contentful: ^1.3.48 => 1.3.54
gatsby-transformer-remark: ^1.7.40 => 1.7.44
npmGlobalPackages:
gatsby-cli: 2.6.7
gatsby-starter-default: 1.0.0

File contents (if changed)

N/A

question or discussion

Most helpful comment

A bit of an ugly solution but you could query for { internal:{ content }} to get an escaped string of the whole situation then parse it yourself.

All 2 comments

Hey @drake-smith

jsonObject in your Contentful model is being parsed. This is by design. Since all fields (that a client wants in a response) need to be explicitly included in GraphQL, there's no way to get back the _entire_ object without mentioning the fields.

If you want a JSON string back (as opposed to it parsed JSON), you could set the field type in the model to be a String, request the field and pass it in a <script type="application/ld+json">

Closing this for now but please feel free to reopen if there is anything else here that we can help with. Thank you for using Gatsby! 馃挏

A bit of an ugly solution but you could query for { internal:{ content }} to get an escaped string of the whole situation then parse it yourself.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

magicly picture magicly  路  3Comments

andykais picture andykais  路  3Comments

ghost picture ghost  路  3Comments

timbrandin picture timbrandin  路  3Comments

3CordGuy picture 3CordGuy  路  3Comments