Giter VIP home page Giter VIP logo

Comments (6)

jturkel avatar jturkel commented on July 18, 2024 3

@andredantasrocha - We do have that code in an initializer. At some point we had to wrap it in a to_prepare to get things working properly in development with the Rails code reloader though:

Rails.configuration.to_prepare do
  GraphiQL::Rails::EditorsController.class_eval do
    before_action :authenticate_user!
  end 
end

from graphiql-rails.

jturkel avatar jturkel commented on July 18, 2024 2

We're using Devise for authentication and forced GraphiQL to authenticate with the following monkey patch:

GraphiQL::Rails::EditorsController.class_eval do
  before_action :authenticate_user!
end

from graphiql-rails.

connorshea avatar connorshea commented on July 18, 2024

Not quite relevant to this issue, but I can't find a better issue to post it in:

I'm using Doorkeeper to handle API authentication and this is how I made GraphiQL work in development:

# config/initializers/graphiql.rb

# Only enable these in development, GraphiQL isn't enabled in production.
if Rails.env.development?
  GraphiQL::Rails.config.headers['X-GraphiQL-Request'] = ->(_context) { "true" }
end

And then the context looks like this in the execute method:

# app/controllers/graphql_controller.rb

context = {
  current_user: current_user || doorkeeper_user,
  doorkeeper_scopes: doorkeeper_token&.scopes&.to_a,
  graphiql_override: false
}

# Set graphiql_override to true if in development mode and the request
# has the GraphiQL Request header. This is used to allow GraphiQL
# requests to skip the Doorkeeper token checks.
context[:graphiql_override] = true if Rails.env.development? && request.headers['X-GraphiQL-Request'] == 'true'

Then I modify the base object and base mutation classes to not raise if the graphql_override context variable is true.

# app/graphql/types/base_object.rb

def self.authorized?(_object, context)
  raise GraphQL::ExecutionError, "You must be logged in to use the API." if context[:current_user].nil?

  if !context[:doorkeeper_scopes]&.include?('read') && !context[:graphiql_override]
    raise GraphQL::ExecutionError, "Your token must have the 'read' scope to perform a query." 
  end

  return true
end
# app/graphql/mutations/base_mutation.rb

def ready?(**_args)
  raise GraphQL::ExecutionError, "Your token must have the 'write' scope to perform a mutation." if !context[:doorkeeper_scopes]&.include?('write') && !context[:graphiql_override]

  return true
end

There might be better ways to do this, but it's how I'm handling it. :)

from graphiql-rails.

andredantasrocha avatar andredantasrocha commented on July 18, 2024

We're using Devise for authentication and forced GraphiQL to authenticate with the following monkey patch:

GraphiQL::Rails::EditorsController.class_eval do
  before_action :authenticate_user!
end

Hi @jturkel, where did you add this code? in an initializer?

from graphiql-rails.

andredantasrocha avatar andredantasrocha commented on July 18, 2024

Thanks @jturkel, it worked perfectly!

from graphiql-rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.