将对象数组添加到阿波罗反应中的突变

将对象数组添加到阿波罗反应中的突变

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

问题描述

限时删除!!

我在前端使用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.

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

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.

如果我在调用突变时将tag变量作为参数传递给我,如何在突变中填写标量类型字段(在第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-在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

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

1403页,肝出来的..

09-06 16:58