【问题标题】:Create Connected object in GraphQL via Mutation通过 Mutation 在 GraphQL 中创建 Connected 对象
【发布时间】:2019-07-15 06:51:33
【问题描述】:

通过连接到另一个对象的突变创建对象的最佳实践是什么。

使用以下架构:

type Question @model {
  id: ID!
  text: String!
  answers: [Answer] @connection(name: "QuestionAnswers")
}
type Answer @model {
  id: ID!
  text: String!
  question: Question @connection(name: "QuestionAnswers")
}

以下(及其变体)失败:

mutation CreateAnswer {
  createAnswer(input: {
    text:"Yes",
    question: {
      id: "9d38c759-6b64-4c1f-9e0e-d3b95a72b3a8"
    }
  }) 
    {
        id
    }
  }

服务器端代码:

mutation CreateAnswer($input: CreateAnswerInput!) {
  createAnswer(input: $input) {
    id
    text
    question {
      id
      text
      answers {
        nextToken
      }
    }
  }
}

通过上述,收到以下错误:

"WrongType 类型的验证错误:参数 'input' 的值 'ObjectValue{objectFields=[ObjectField{name='text', value=StringValue{value='3'}}, ObjectField{name='question', value=ObjectValue{objectFields=[ObjectField{name='id', 值=字符串值{值='9d38c759-6b64-4c1f-9e0e-d3b95a72b3a8'}}]}}]}' 包含不在“CreateAnswerInput”中的字段:“问题”@ 'createAnswer'"

【问题讨论】:

  • 该查询在我看来在语法上是有效的。您是否有实际的错误消息、GraphQL 架构或任何与之匹配的服务器端代码?
  • 感谢您的回答。上面添加了架构、服务器端代码和错误。
  • input CreateAnswerInput 是什么样的?
  • 它:{ text:"Yes", question: { id: "9d38c759-6b64-4c1f-9e0e-d3b95a72b3a8" } } 除非我遗漏了什么?

标签: react-native graphql aws-appsync


【解决方案1】:

似乎这可能是 AppSync 中的命名约定。以下对我有用:

mutation createAnswer {
  createAnswer (input: {
    text: "5"
    answerQuestionId: "9d38c759-6b64-4c1f-9e0e-d3b95a72b3a8"
  }) {
    id
  }

answer 添加到 QuestionId 是我们所需要的。

【讨论】:

    猜你喜欢
    • 2021-03-30
    • 2022-01-05
    • 2019-03-13
    • 2011-09-29
    • 2020-11-06
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多