Using Portable Text with Gatsby and React, encountering issues with data not showing up on frontend
34 replies
Last updated: Apr 7, 2020
U
Has anyone used Block Content — Portable Text with their React or Gatsbyjs apps?
Apr 7, 2020, 9:33 PM
N
haven't deployed yet, but working with Gatsby and Portable Text as we speak
Apr 7, 2020, 9:37 PM
U
user N
I’d like to generate my content inside paragraph tags so I’m using this schema:{ name: 'description', type: 'array', of: [ { type: 'block' } ] },
Apr 7, 2020, 9:38 PM
N
yep, that looks right
Apr 7, 2020, 9:39 PM
U
except, after publishing some content, I don’t get any data on the Frontend. hmmm. If you’re doing anything similiar, I’m curious how you’re doing it. I got content:
Apr 7, 2020, 9:40 PM
N
yeah, let me drop some Gatsby code in here
Apr 7, 2020, 9:41 PM
N
are you using the
@sanity/block-content-to-reactplugin in your React/Gatsby project?
Apr 7, 2020, 9:42 PM
N
you'll need that
Apr 7, 2020, 9:42 PM
U
Oh dang, I need that? I was hoping to get that data through the gatsby graphql layer. ..
Apr 7, 2020, 9:43 PM
N
well, you probably don't NEED it, but it will help
Apr 7, 2020, 9:43 PM
N
in your graphql query, you'll probably be best served by querying for
_rawDescription
Apr 7, 2020, 9:43 PM
U
ah, thank you
Apr 7, 2020, 9:44 PM
N
i have a Portable Text area called "Body Copy"
Apr 7, 2020, 9:44 PM
N
which i query with
_rawBodyCopy(resolveReferences: {maxDepth: 10})
Apr 7, 2020, 9:44 PM
U
you planning on pushing you code to github?
Apr 7, 2020, 10:01 PM
L
user U
I'm not sure, but you might have hit this bug from the gatsby-source-sanity: https://github.com/sanity-io/gatsby-source-sanity/issues/37 Apr 7, 2020, 10:09 PM
U
so I changed it to block right? But My gatsby project seems to still be reading it as a string…
Apr 7, 2020, 10:30 PM
U
String cannot represent value: [{ _key: “cd5ea5ea997c”, _type: “block”, children: [Array], markDefs: [], style: “h1” }, { _key: “185b7c5c4e2f”, _type: “block”, children: [Array], markDefs: [], style: “h1" }, { _key: “7857cb29fcfb”, _type: “block”, children: [Array], markDefs: [], style: “normal” }, { _key: “c1212e400ac9", _type: “block”, children: [Array], markDefs: [], style: “normal” }, { _key: “ebd6d9ccdb93”, _type: “block”, children: [Array], markDefs: [], style: “normal” }, { _key: “c4366c0b5f63", _type: “block”, children: [Array], markDefs: [], style: “normal” }, { _key: “3e7ba4f013ce”, _type: “block”, children: [Array], markDefs: [], style: “normal” }, { _key: “06ac5b869043", _type: “block”, children: [Array], level: 1, listItem: “bullet”, markDefs: [], style: “normal” }, { _key: “77a73e777d31", _type: “block”, children: [Array], level: 1, listItem: “bullet”, markDefs: [], style: “normal” }, { _key: “3172d75cdffc”, _type: “block”, children: [Array], level: 1, listItem: “bullet”, markDefs: [], style: “normal” }, ... 1 more item]
Apr 7, 2020, 10:30 PM
U
user N
I’m not see _rawDescriptionApr 7, 2020, 10:32 PM
U
in my graphql query
Apr 7, 2020, 10:32 PM
L
I think we could help you better if you provided some code, both from the schema (where you are defining your block), and from Gatsby (where you are fetching and trying to render it).
Apr 7, 2020, 10:34 PM
U
{ name: 'description', type: 'array', of: [ { type: 'block' } ] },
Apr 7, 2020, 10:34 PM
U
in localhost:8000/__graphql, i can’t seem to find _rawDescription in the data fields to query, after rebooting the servers
Apr 7, 2020, 10:36 PM
U
Do you know how the Portable Text works with Gatsby/Graphql?
Apr 7, 2020, 10:37 PM
N
yes... i will eventually be putting this up on Github, just being lazy really
Apr 7, 2020, 10:39 PM
N
let me grab some more code for you on how both sides work.
Apr 7, 2020, 10:39 PM
N
ok, this is a snippet from inside a document
as you can see its called
Article
{ title: "Content", name: "content", type: "array", of: [ {type: "block"}, {type: "imageFull"}, {type: "gallery"}, {type: "video"}, {type: "googleMyMap"} ] }
content
Apr 7, 2020, 10:41 PM
N
here's my __graphql
as you can see here, inside my
query MyQuery { allSanityArticle { edges { node { id _rawContent } } } }
allSanityArticle(which matches my document name of
Article) under
edges.nodeyou'll find my
_rawContentwhich matches my Portable Text array of
Content
Apr 7, 2020, 10:43 PM
N
here's the output of that query
{ "data": { "allSanityArticle": { "edges": [ { "node": { "id": "f8c948dd-5749-57ab-90a6-3e27419ed19e", "_rawContent": [ { "_key": "dff03124000f", "_type": "block", "children": [ { "_key": "dff03124000f0", "_type": "span", "marks": [], "text": "When booking this part of the trip, we didn't really know what to expect as far as what things were like down in the extreme south of Thailand. But after three hectic days in Bangkok, anything seemed like it would be a relief." } ], "markDefs": [], "style": "normal" }, { "_key": "63b8ee0e6c68", "_type": "block", "children": [ { "_key": "63b8ee0e6c680", "_type": "span", "marks": [], "text": "We had gone back and forth on when to position the beach and ocean part of our trip, finally deciding on placing it at the end of the trip. Thank goodness for that decision." } ], ...
Apr 7, 2020, 10:45 PM
U
I see it now, it takes awhile before it shows up on the gatsby-graphql side. My bad
Apr 7, 2020, 10:45 PM
N
oh good!
Apr 7, 2020, 10:46 PM
U
so, not the ideal block of data to work with, but I can work with it
Apr 7, 2020, 10:47 PM
N
i've also created a
PortableTextcomponent that then serializes all my different types within the Portable Text ... in my example above that includes
block,
imageFull,
gallery,
videoand
googleMyMaps
Apr 7, 2020, 10:47 PM
N
its actually pretty great to work with because you can get exactly what you want ... its probably a bit more work to set up, but super worth it
Apr 7, 2020, 10:48 PM
U
k, will look into it. Thanks for reaching out you two!
user N
user L
Apr 7, 2020, 10:49 PM
Sanity– build remarkable experiences at scale
The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.