gatsby-uc / gatsby-source-strapi

Gatsby source plugin for building websites using Strapi as a data source
MIT License
355 stars 183 forks source link

Help with dynamic zones sought after #316

Closed jasewarner closed 1 year ago

jasewarner commented 2 years ago

Does anyone have any pointers on using fragments in graphql queries to retrieve blocks from a dynamic zone when not all of the blocks will necessarily have been included in a single content type?

For example, when editing my page single content type, if I only use two of the three blocks, my query falls over.

Here's my query:

fragment Blocks on STRAPI__COMPONENT_BLOCK_APP_PREVIEWSTRAPI__COMPONENT_BLOCK_HEROSTRAPI__COMPONENT_BLOCK_TEXT_AND_IMAGEUnion {
  __typename
  ... on STRAPI__COMPONENT_BLOCK_APP_PREVIEW {
      internal {
      type
      }
      Title
      Ratio
      Background
      Groups {
      Title
      Paragraph
      Image {
        localFile {
          childImageSharp {
            gatsbyImageData
          }
        }
        alternativeText
      }
      }
      Video {
      url
      mime
      }
    }
    ... on STRAPI__COMPONENT_BLOCK_HERO {
      Background
      Position
      Subtitle
      Title
      Mobile {
    localFile {
      childImageSharp {
        gatsbyImageData
      }
    }
    alternativeText
      }
      Desktop {
      localFile {
        childImageSharp {
          gatsbyImageData
        }
      }
      alternativeText
      }
      internal {
      type
      }
    }
    ... on STRAPI__COMPONENT_BLOCK_TEXT_AND_IMAGE {
      Background
      Paragraph
      Reorder
      Title
      internal {
      type
      }
      Desktop {
      alternativeText
      localFile {
        childImageSharp {
          gatsbyImageData
        }
      }
      }
      Mobile {
      alternativeText
      localFile {
        childImageSharp {
          gatsbyImageData
        }
      }
      }
    }
}
soupette commented 2 years ago

What about only populating the strapi_id in your query and then in your Componenbt make another query to populate the needed field?

Components are available in your GraphiQL you can try it out

jasewarner commented 2 years ago

I'm probably missing something (Strapi noob here), but surely I wouldn't be able to get strapi_id for a component that isn't being used in the dynamic zone on a particular page?

For example, if I'm not using the "Text and Image" component in my page, querying the following would cause the query to fall over:

... on STRAPI__COMPONENT_BLOCK_TEXT_AND_IMAGE {
    strapi_id
}

Right? Or have I got the wrong end of the stick?

remidej commented 2 years ago

I think since strapi_id is common to all components you should be able to query it outside any fragment

Edit: I just tested it and it doesn't work outside fragments. That's not what we want indeed

soupette commented 2 years ago

Yes but since a node is created we should be able to query it anyway, right?

remidej commented 2 years ago

We're able to query it if we know the ID of the component to query, but what if we don't?

jasewarner commented 2 years ago

Yeah, I just get an "Unknown type" error from my GraphQL query.

soupette commented 2 years ago

We're able to query it if we know the ID of the component to query, but what if we don't?

You'll always know the id of the component

moonmeister commented 1 year ago

Thanks for your interest in this project. This plugin is moving into the Gatsby User Collective and this repo will be archived. Please open an issue in that repository, submit a PR if you'd like to see this implemented, or join us on Discord if you have questions!