Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

337
Views
graphql-ruby: how to get current user IP inside QueryType?

How to get current user's IP address inside QueryType? For example here:

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
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need to pass in a context to graphql.

Here's an example:

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

Then,

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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!