4

I am trying to create a mutation for Strapi that creates/edits dynamic zones. Dynamic zones in Strapi are union types. How does one do a GraphQL mutation with dynamic zones?

To be exact. What should be the content here?

input: {data: {inhalt: [{text: "hallo"}]}}

In the example below there is a single component named impressum. Inhalt is the dynamic zone. It contains different components: ComponentPageText, ComponentPageInformation and ComponentPageArticle.

This mutation

mutation {
  updateImpressum(input: {data: {inhalt: [{text: "hallo"}]}}) {
    impressum {
      inhalt {
        __typename
      }
    }
  }
}

returns

Expected type ImpressumInhaltDynamicZoneInput!, found {text: \"hallo\"}; Component not found. expected one of: ComponentPageText, ComponentPageInformation, ComponentPageArticle

This returns the same error

mutation {
  updateImpressum(input: {data: {inhalt: [{ComponentPageText: {text: "hallo"}}]}}) {
    impressum {
      inhalt {
        __typename
      }
    }
  }
}

Schema introspection returns

{
  "name": "ComponentPageText",
  "kind": "OBJECT"
}

STRUCTURE (added after comment)

impressum => inhalt => [page.text, page.information, page.article]

corresponds to

single type => dynamic zone => [components]

Fields in components

page.text: text
page.information: title, text, image
page.article: relation to collection type - article

SCHEMA INTROSPECTION

{
  "name": "updateImpressum",
  "__typename": "__Field",
  "description": "",
  "args": [
    {
      "name": "input",
      "description": "",
      "__typename": "__InputValue",
      "type": {
        "kind": "INPUT_OBJECT",
        "name": "updateImpressumInput",
        "possibleTypes": null,
        "interfaces": null,
        "inputFields": [
          {
            "name": "data",
            "description": "",
            "__typename": "__InputValue"
          }
        ]
      }
    }
  ]
}
2
  • Hello, can you please share your compo and content type structure.
    – Jim LAURIE
    Mar 14, 2020 at 15:23
  • @JimLAURIE I added the structure above in the question. Was this what you meant? Do you need any more information? Mar 16, 2020 at 11:25

2 Answers 2

4

Here is an example of how to mutate a dynamic zone in Strapi. In my case, I have a collection called Tests and a dynamic zone called New Question that is under a group called questions-group:

mutation {
 createTest(input:{ data:{
  title:"Test Title Here"
  questions: [
     {
      __typename: "ComponentQuestionsGroupNewQuestion"
      __component: "questions-group.new-question"
      title:"What is 4 + 20?"
      correct_answer: "24"
      wrong_answer: "420"
    }
  ]
  }}) {
    test {
      id
    }
  }
}

You have to provide __typename: and __component: inside the dynamic zone clause.

3
  • This works. Thanks a lot for your answer! I am sorry for the late reply. Jun 28, 2020 at 14:51
  • Now (I'm usign Strapi 3.6.2 with MongoDB) you don't have to include __component anymore. You can just provide __typename and it will work correctly. May 27, 2021 at 7:10
  • Life saver! I don't understand why there's no docu about this on the main site Apr 6, 2022 at 15:33
1

You need something like this to work with components:

mutation($text: String!) {
  updateImpressum {
    impressum {
      inhalt {
        __typename
        ... on MyComponentName {
            text: $text
        }
      }
    }
  }
}

Tip: use the /graphql client to autocomplete component types by typing "... on "

Tip2: Use fragments

1
  • Thank you for your comment, but what I cannot figure out is not how to query after doing the mutation but the mutation itself. Meaning this string "updateImpressum(input: {data: {inhalt: [{text: "hallo"}]}})". I am sorry if I did not express this clearly in my question. Mar 17, 2020 at 19:04

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.