RStankov / SearchObjectGraphQL

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

Using context to limit scope #10

Closed webdobe closed 5 years ago

webdobe commented 5 years ago

Hello thank you for the taking care of this project.

I am running into issues trying to get context and arguments to modify the scope.

If I use this for my query passing my class as a function arguments and context do not get passed in to the class.

field :people, Types::PeopleConnection, null: true, function: Resolvers::PeopleSearch, connection: true do
      argument :sort, Inputs::SortInput, required: false
      argument :filters, Inputs::PeopleFiltersInput, required: false
end

If I do that and say:

field :people, Types::PeopleConnection, null: true, connection: true do
      argument :sort, Inputs::SortInput, required: false
      argument :filters, Inputs::PeopleFiltersInput, required: false
end

def people(**arguments)
    Resolvers::PeopleSearch.call(nil, arguments, context[:current_user])
end

Only arguments get passed into the people definition and unable to grab the context.

I understand this may have nothing to do with the SearchObject class but curious if I am even going about this correctly.

as trying to modify the scope via the initialize then makes my sort and filter option get bypassed

class Resolvers::PeopleSearch
  # include SearchObject for GraphQL
  include SearchObject.module(:graphql)

  type types[Types::PersonType]

  scope { Person.all }

  # when "sort" is passed "apply_sort" would be called to sort the scope
  option :sort, type: Inputs::SortInput, with: :apply_sort

  # when "filters" is passed "apply_filter" would be called to filter the scope
  option :filters, type: Inputs::PeopleFiltersInput, with: :apply_filter

In the end I am trying to limit the scope based on user permissions. Any feedback would be appreciated.

There is this stack overflow question that fits the build but no one has really made any valuable feedback to it: https://stackoverflow.com/questions/51957480/how-to-pass-current-user-in-graphql-resolver

RStankov commented 5 years ago

Hey, try this:

def people(**arguments)
    Resolvers::PeopleSearch.call(filters: arguments, object: object, context: context)
end

Then you class you can do:

scope { Person.visible_by(context[:current_user] }

I'm working on update that will remove the need for def people. The issue I have to support both old style of GraphQL gem and the new one.

webdobe commented 5 years ago

Hi @RStankov, Really appreciate you providing quick feedback. When I do what you mention I get the error wrong number of arguments (given 1, expected 3). If I do:

def people(**arguments)
    byebug
    Resolvers::PeopleSearch.call(object, arguments, context)
  end

byebug prints my all three of those fine.

require 'search_object'
require 'search_object/plugin/graphql'

class Resolvers::PeopleSearch
  # include SearchObject for GraphQL
  include SearchObject.module(:graphql)

  type types[Types::PersonType]
  byebug
  scope { Person.visible_by(object) }

byebug does not print a thing for object or context both being nil.

RStankov commented 5 years ago

Hm, what do you mean by:

byebug does not print a thing for object or context both being nil.

in people object and context are nil?

webdobe commented 5 years ago

Hi Sorry, I should have replied. I was able to get passed this. I was calling visible_by instead of visible_to which was the name of my method. I am still unsure why those arguments (object & context) are nil when they get to the Class but they appear that way in byebug. But then they work... So I guess I am still just a rails newbie and not sure how to debug. Thank you for your help!

RStankov commented 5 years ago

No worries, glad to help :)