RStankov / SearchObjectGraphQL

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

Nested input question #4

Closed hughevans closed 6 years ago

hughevans commented 6 years ago

đź‘‹

Is there an easy way to nest the options (arguments) inside another input?

For example, I want the nest the options under filter::

    customLocationsConnection(filter: {active: true, name_contains: "a"}) {
      edges {
        node {
          …
        }
      }
    }

I can do this by defining an GraphQL::InputObjectType for the option type, but that means I just end up with a single resolver function where I need to tediously apply the scope changes (negating much of the benefits of SearchObject).

Cheers!

RStankov commented 6 years ago

Hi,

You can nest arguments by defining an InputObjectType:

class PostSearch 
  include SearchObject.module(:graphql)

  NestedInput = GraphQL::InputObjectType.define do
    name 'PostSearchNestedInput'

    argument :type, types.String
    argument :value, types.String
  end

  option :filters, type: NestedInput, with: :apply_filters
end

If you gave me concrete example, I can help more.

hughevans commented 6 years ago

Thanks @RStankov. Then you’d have to apply all the filters “manually” in one large method though wouldn’t you. This would be nicer I think:

class PostSearch 
  include SearchObject.module(:graphql)

  option :filter do
    name 'PostSearchFilters'

    option(:name, type: types.String)       { |scope, value| scope.where name: value }
    option(:published, type: types.Boolean) { |scope, value| value ? scope.published : scope.unpublished }
  end
end
RStankov commented 6 years ago

Yes you have to apply the filter manually.

I really like your idea of nested filters. It would be a bit tricky to implement. But I try to see how, I can do it.