Jos*_*man 6 apollo reactjs graphql graphcool
我在前端使用react-apollo,在后端使用graphcool.我有一个突变,创建一个这样的教程:
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
}
}
`
Run Code Online (Sandbox Code Playgroud)
它在提交处理程序中调用,如此...
this.props.createTutorialMutation({
variables: {
author,
link,
title,
completed: false,
postedById
}
})
Run Code Online (Sandbox Code Playgroud)
一切都很美妙.
现在我想在创建新教程时添加一组标记.我创建了输入字段并将其连接起来,以便tags变量是一个对象数组,每个对象都有一个标记id和标记文本.
如果我尝试将标记字段添加到变异中,则需要标量类型.但是对于一组对象似乎没有标量类型.
如果我在调用变异时将tag变量作为参数传递,我如何填充变异中的Scalar类型字段(在148行上这里https://github.com/joshpitzalis/path/blob/graphQL/src/ components/Add.js)和架构?
我是graphQL的新手,我明白我可能会以错误的方式接近这一点.如果是这种情况,我如何在graphQL中添加一个对象数组?
您应该Tag
在模式文件中添加一个新类型,并将其Tutorial
与新关系连接:
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")
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用嵌套的创建突变创建新教程和新标记,如下所示:
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
}
}
}
`
Run Code Online (Sandbox Code Playgroud)
这篇文章提供了更多关于其他方法及其权衡的背景知识:https://www.graph.cool/forum/t/how-do-i-add-an-array-of-objects-to-a-mutation-在-阿波罗反应/ 365/6→U = nilan
归档时间: |
|
查看次数: |
5081 次 |
最近记录: |