简体   繁体   English

如何:带有动态区域/组件的 Strapi GraphQL 突变

[英]How to: Strapi GraphQL mutation with dynamic zones/component

I am trying to create a mutation for Strapi that creates/edits dynamic zones.我正在尝试为 Strapi 创建一个用于创建/编辑动态区域的突变。 Dynamic zones in Strapi are union types. Strapi 中的动态区域是联合类型。 How does one do a GraphQL mutation with dynamic zones ?如何使用动态区域进行 GraphQL突变

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 .在下面的示例中,有一个名为impressum 的组件。 Inhalt is the dynamic zone.吸入是动态区域。 It contains different components: ComponentPageText, ComponentPageInformation and ComponentPageArticle .它包含不同的组件: ComponentPageText、ComponentPageInformationComponentPageArticle

This mutation这种突变

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

returns回报

Expected type ImpressumInhaltDynamicZoneInput!, found {text: \\"hallo\\"};预期类型 ImpressumInhaltDynamicZoneInput!,找到 {text: \\"hallo\\"}; Component not found.未找到组件。 expected one of: ComponentPageText, ComponentPageInformation, ComponentPageArticle预期之一: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] impressum => 吸入 => [page.text, page.information, page.article]

corresponds to对应于

single type => dynamic zone => [components]单一类型 => 动态区域 => [组件]

Fields in components组件中的字段

page.text : text page.text : 文本
page.information : title, text, image page.information : 标题、文字、图片
page.article : relation to collection type - article page.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"
          }
        ]
      }
    }
  ]
}

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 "提示:使用 /graphql 客户端通过键入“... on”来自动完成组件类型

Tip2: Use fragments Tip2:使用片段

Here is an example of how to mutate a dynamic zone in Strapi.这是如何在 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:就我而言,我有一个名为 Tests 的集合和一个名为 New Question 的动态区域,它位于名为 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.您必须在动态区域子句中提供__typename:__component:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM