¿Cómo obtener la dirección IP del usuario actual dentro de QueryType? Por ejemplo aquí:
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 endDebe pasar en un context a graphql.
Aquí hay un ejemplo:
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) endEntonces,
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