👀 See Sanity in action: Watch product demo now →

Generating Data Inside Paragraph Tags - Block Content and Portable Text with React or GatsbyJS Apps

32 replies
Last updated: Apr 7, 2020
Has anyone used Block Content — Portable Text with their React or Gatsbyjs apps?
Apr 7, 2020, 9:33 PM
haven't deployed yet, but working with Gatsby and Portable Text as we speak
Apr 7, 2020, 9:37 PM
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
yep, that looks right
Apr 7, 2020, 9:39 PM
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
yeah, let me drop some Gatsby code in here
Apr 7, 2020, 9:41 PM
are you using the
@sanity/block-content-to-react
plugin in your React/Gatsby project?
Apr 7, 2020, 9:42 PM
you'll need that
Apr 7, 2020, 9:42 PM
Oh dang, I need that? I was hoping to get that data through the gatsby graphql layer. ..
Apr 7, 2020, 9:43 PM
well, you probably don't NEED it, but it will help
Apr 7, 2020, 9:43 PM
in your graphql query, you'll probably be best served by querying for
_rawDescription
Apr 7, 2020, 9:43 PM
ah, thank you
Apr 7, 2020, 9:44 PM
i have a Portable Text area called "Body Copy"
Apr 7, 2020, 9:44 PM
which i query with

_rawBodyCopy(resolveReferences: {maxDepth: 10})
Apr 7, 2020, 9:44 PM
you planning on pushing you code to github?
Apr 7, 2020, 10:01 PM
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
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
user N
I’m not see _rawDescription
Apr 7, 2020, 10:32 PM
in my graphql query
Apr 7, 2020, 10:32 PM
    {
      name: 'description',
      type: 'array',
      of: [
        {
          type: 'block'
        }
      ]
    },
Apr 7, 2020, 10:34 PM
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
Do you know how the Portable Text works with Gatsby/Graphql?
Apr 7, 2020, 10:37 PM
yes... i will eventually be putting this up on Github, just being lazy really
Apr 7, 2020, 10:39 PM
let me grab some more code for you on how both sides work.
Apr 7, 2020, 10:39 PM
ok, this is a snippet from inside a document
Article


{
      title: "Content",
      name: "content",
      type: "array",
      of: [
        {type: "block"},
        {type: "imageFull"},
        {type: "gallery"},
        {type: "video"},
        {type: "googleMyMap"}
      ]
    }
as you can see its called
content
Apr 7, 2020, 10:41 PM
here's my __graphql

query MyQuery {
  allSanityArticle {
    edges {
      node {
        id
        _rawContent
      }
    }
  }
}
as you can see here, inside my
allSanityArticle
(which matches my document name of
Article
) under
edges.node
you'll find my
_rawContent
which matches my Portable Text array of
Content
Apr 7, 2020, 10:43 PM
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
I see it now, it takes awhile before it shows up on the gatsby-graphql side. My bad
Apr 7, 2020, 10:45 PM
oh good!
Apr 7, 2020, 10:46 PM
so, not the ideal block of data to work with, but I can work with it
Apr 7, 2020, 10:47 PM
i've also created a
PortableText
component that then serializes all my different types within the Portable Text ... in my example above that includes
block
,
imageFull
,
gallery
,
video
and
googleMyMaps
Apr 7, 2020, 10:47 PM
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
k, will look into it. Thanks for reaching out you two!
user N
user L
Apr 7, 2020, 10:49 PM

Sanity.io – build remarkable experiences at scale

Sanity is a customizable solution that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.