将对象数组添加到 apollo-react 中的突变 [英] Add an array of Objects to a mutation in apollo-react

查看:31
本文介绍了将对象数组添加到 apollo-react 中的突变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在前端使用 react-apollo,在后端使用 graphcool.我有一个创建这样的教程的突变:

I am using react-apollo on the front-end and graphcool on the backend. I have a mutation that creates a tutorial like so:

const CREATE_TUTORIAL_MUTATION = gql`
  mutation CreateTutorialMutation(
    $author: String
    $link: String
    $title: String!
    $postedById: ID!
    $completed: Boolean!
  ) {
    createTutorial(
      author: $author
      link: $link
      title: $title
      postedById: $postedById
      completed: $completed
    ) {
      author
      link
      title
      postedBy {
        id
        name
      }
      completed
    }
  }
`

它在像这样的提交处理程序中被调用......

It gets called in a submit handler like so...

this.props.createTutorialMutation({
      variables: {
        author,
        link,
        title,
        completed: false,
        postedById
      }
    })

一切都很好.

现在我想在创建新教程时添加一组标签.我创建了输入字段并将其连接起来,以便 tags 变量是一个对象数组,每个对象都有一个标签 id 和标签 text.

Now I want to add a set of tags to when I create a new tutorial. I created the input field and connected it so that the tags variable is an array of objects, each with a tag id and the tag text.

如果我尝试将 tags 字段添加到突变中,它需要一个标量类型.但是对象数组似乎没有标量类型.

If I try and add the tags field to the mutation it needs a scalar type. But there is doesn't seem to be a scalar type for an array of objects.

如果我在调用突变时将标签变量作为参数传入,我该如何填写突变中的标量类型字段(此处为第 148 行 https://github.com/joshpitzalis/path/blob/graphQL/src/components/Add.js) 和架构?

If I pass the tag variable in as a parameter when I call the mutation how do I fill in the Scalar type field in the mutation ( on line 148 here https://github.com/joshpitzalis/path/blob/graphQL/src/components/Add.js) and in the schema?

我是 graphQL 的新手,我知道我可能完全以错误的方式来处理这个问题.如果是这种情况,我如何将对象数组添加到 graphQL 中的突变?

I am new to graphQL and I understand that I might be approaching this completely the wrong way. If that is the case, how do I add an array of objects to a mutation in graphQL?

推荐答案

您应该将新的 Tag 类型添加到您的架构文件中,并使用新的将其连接到 Tutorial关系:

You should add a new Tag type to your schema file and connect it to Tutorial with a new relation:

type Tutorial {
  author: String
  completed: Boolean
  link: String
  title: String!
  id: ID! @isUnique
  createdAt: DateTime!
  updatedAt: DateTime!
  postedBy: User @relation(name: "UsersTutorials")
  tags: [Tag!]! @relation(name: "TutorialTags")
}

type Tag {
  id: ID!
  tag: String!
  number: Int!
  tutorials: [Tutorial!]! @relation(name: "TutorialTags")
}

然后,您可以使用如下嵌套的 create 突变创建新教程和新标签:

Then you can create a new tutorial and new tags using a nested create mutation like this:

const CREATE_TUTORIAL_MUTATION = gql`
  mutation CreateTutorialMutation(
    $author: String
    $link: String
    $title: String!
    $tags: [TutorialtagsTag!]!
    $completed: Boolean!
    $postedById: ID!
  ) {
    createTutorial(
      author: $author
      link: $link
      title: $title
      tags: $tags
      completed: $completed
      postedById: $postedById
    ) {
      author
      link
      title
      postedBy {
        id
        name
      }
      completed
      tags {
        id
        text
      }
    }
  }
`

这篇文章提供了有关其他方法及其权衡的更多背景:https://www.graph.cool/forum/t/how-do-i-add-an-array-of-objects-to-a-mutation-in-apollo-react/365/6?u=nilan

This post gives more background about other approaches and their trade-offs: https://www.graph.cool/forum/t/how-do-i-add-an-array-of-objects-to-a-mutation-in-apollo-react/365/6?u=nilan

这篇关于将对象数组添加到 apollo-react 中的突变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆