gpt4 book ai didi

graphql - Lighthouse GraphQL - 如何将数组作为变量传递?

转载 作者:行者123 更新时间:2023-12-05 03:47:11 40 4
gpt4 key购买 nike

我正在努力构建一个简单的 Facebook 克隆来积累我对 GraphQL 的了解。我正在使用 lighthouse-php在 Laravel 之上为我的数据提供服务。本质上,一个用户有很多 friend ,我知道这是可行的,因为在 Tinker 控制台中,我可以使用 $user->friends()->get() 之类的东西返回正确的数据。我正在努力弄清楚如何传递一组数据。

在我的前端,我有一个 Vuetify 组合框(基本上是一个选择下拉列表),它构建了一个电子邮件数组,这就是我试图传递给我的后端突变的内容。

我如何构造类型和变异以使其接受电子邮件数组?

user.graphql:

extend type Mutation @guard(with: ["api"]) {
addFriends(input: [AddFriendsInput]! @spread): [User]! @field(resolver: "UserMutator@addFriends")
}

type User {
id: ID!
email: String!
password: String!
first_name: String
last_name: String
created_at: DateTime!
updated_at: DateTime!

friends: [User]! @belongsToMany(relation: "friends")
}

input AddFriendsInput {
email: String!
}

addFriend.gql:

mutation AddFriends($friends: [AddFriendsInput]!) {
addFriends(input: { email:[$friends] }){
id
email
}
}

最佳答案

好吧,终于明白了。

将输入修改为:

input AddFriendsInput {
email: String!
# friends: [User]
}

并将我的 Mutation 更改为:

mutation AddFriends($friends: [AddFriendsInput]!) {
addFriends(input: $friends) {
id
email
}
}

这让我现在可以将一系列电子邮件传递给我的解析器。

关于graphql - Lighthouse GraphQL - 如何将数组作为变量传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64977845/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com