gatsby-source-strapi icon indicating copy to clipboard operation
gatsby-source-strapi copied to clipboard

Help with dynamic zones sought after

Open jasewarner opened this issue 3 years ago • 7 comments
trafficstars

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
	    }
	  }
      }
    }
}

jasewarner avatar Apr 05 '22 11:04 jasewarner

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

soupette avatar Apr 07 '22 14:04 soupette

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?

jasewarner avatar Apr 07 '22 15:04 jasewarner

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

remidej avatar Apr 07 '22 15:04 remidej

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

soupette avatar Apr 08 '22 08:04 soupette

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

remidej avatar Apr 08 '22 13:04 remidej

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

jasewarner avatar Apr 11 '22 14:04 jasewarner

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

soupette avatar Apr 11 '22 16:04 soupette

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!

moonmeister avatar Dec 27 '22 18:12 moonmeister