[英]graphql-ruby: how to get current user IP inside QueryType?
如何在QueryType中獲取當前用戶的IP地址? 例如這里:
class QueryType < GraphQL::Schema::Object
description "The query root of this schema"
field :post, PostType, "Find a post by ID" do
argument :id, ID
end
def post(id:)
# I need to get user's IP here
Post.find(id)
end
end
您需要將context
傳遞給 graphql。
這是一個例子:
class GraphqlController < ApplicationController
def execute
variables = prepare_variables(params[:variables])
query = params[:query]
operation_name = params[:operationName]
context = {
current_user: current_user,
ip: request.remote_ip
}
result = YourSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
render json: result
rescue StandardError => e
raise e unless Rails.env.development?
handle_error_in_development(e)
end
然后,
class QueryType < GraphQL::Schema::Object
description "The query root of this schema"
field :post, PostType, "Find a post by ID" do
argument :id, ID
end
def post(id:)
# I need to get user's IP here
# => context[:ip]
Post.find(id)
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.