RStankov / SearchObjectGraphQL

GraphQL plugin for SearchObject gem
MIT License
159 stars 25 forks source link

accessing GraphQL context from within Resolver class #1

Closed woahstorkdork closed 6 years ago

woahstorkdork commented 6 years ago

I've implemented the filtering example from how-to-graphql and it is working fine; however, I want to access the context object in order to add authentication/authorization to the search object resolver.

I've unsuccessfully tried setting binding.pry inside of the Resolvers::LinksSearch class in order to see if I can access the context object.

class Resolvers::LinksSearch
    include SearchObject.module(:graphql)
    ...
    binding.pry
    (tried to access context from within the pry session here)
    ...   
end

I'm wondering how I can pass/access context within the Resolvers::DispensariesSearch class: field :allDispensaries, function: Resolvers::LinksSearch like I can in a simple field:

    resolve ->(obj, args, ctx) {
      ...
       (can access ctx here)
      ...
    }
  end

Is this possible using the SearchObject plugin ? Should I define a initialize method for the Resolvers::LinksSearch and pass in context as argument when creating a new instance ?

Any and all help would be greatly appreciated !

woahstorkdork commented 6 years ago

I just figured out what you meant in the documentation, that you can access the context from within scope:

scope { context[:current_user] }

perhaps it would benefit other users to have an explicit example in the documentation ? or maybe I'm the only slow one ;)

RStankov commented 6 years ago

@woahstorkdork Adding an example if a good idea. Will try to do it :)