在graphQL查询中传递参数
我刚刚开始学习GraphQL,目前正在尝试创建twitter的克隆。在下面的代码中,是否有一种方法可以将81从id参数(例如user(id:81))自动传递到userId参数(例如tweets(userId:81)) 我复制了下面的代码在graphQL查询中传递参数,graphql,graphql-ruby,Graphql,Graphql Ruby,我刚刚开始学习GraphQL,目前正在尝试创建twitter的克隆。在下面的代码中,是否有一种方法可以将81从id参数(例如user(id:81))自动传递到userId参数(例如tweets(userId:81)) 我复制了下面的代码 { user(id: 81) { username email tweets(userId: 81) { content } } } 用户类型.rb 模块类型 类UserType
{
user(id: 81) {
username
email
tweets(userId: 81) {
content
}
}
}
用户类型.rb
模块类型
类UserType
tweet_type.rb
模块类型
类TweetType
query_type.rb
模块类型
类QueryType
GraphQL有一种一流的方法,可以将动态值从查询中提取出来,并将其作为单独的字典(变量)传递。您可以使用与下面类似的语法,并可以阅读更多有关它的内容
GraphQL有一种一流的方法将动态值从查询中提取出来,并将它们作为单独的字典(变量)传递。您可以使用与下面类似的语法,并可以阅读更多有关它的内容
是的。要调用该查询,您需要将要使用的变量传递给它。类似于:
useQuery(查询,{variables:{id:81}})
yep。要调用该查询,您需要将要使用的变量传递给它。类似于:useQuery(查询,{variables:{id:81}})
module Types
class UserType < Types::BaseObject
field :username, String, null: false
field :email, String, null: false
field :bio, String, null: true
field :tweets, [Types::TweetType], null: true do
argument :user_id, ID, required: true
end
def tweets(user_id:)
Tweet.where(user_id: user_id)
end
end
end
module Types
class TweetType < Types::BaseObject
field :id, ID, null: false
field :content, String, null: false
field :userId, ID, null: false
field :createdAt, GraphQL::Types::ISO8601DateTime, null: false
field :user, Types::UserType, null: false
end
end
module Types
class QueryType < Types::BaseObject
field :tweets,
[Types::TweetType],
null: false,
description: "Returns a list of all tweets"
field :user,
Types::UserType,
null: false,
description: "Returns a list of all users" do
argument :id, ID, required: true
end
def tweets
Tweet.all
end
def user(id:)
User.find(id)
end
end
end
query User($id: Int) {
user(id: $id) {
username
email
tweets(userId: $id) {
content
}
}
}