RStankov / SearchObjectGraphQL

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

Doc: How to make it work with resolver #17

Closed vfonic closed 4 years ago

vfonic commented 4 years ago

I've had some issues making SearchObject work with resolver.

For some unexplicable reason neither SearchObject nor SearchObjectGraphQL were loaded.

$ rails console
> SearchObject
NameError: uninitialized constant SearchObject
from /Users/viktor/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/active_support.rb:76:in `block in load_missing_constant'
> require 'search_object'
true
> SearchObject
SearchObject
> require 'search_object/plugin/graphql'
true

After adding these two requires on top of my resolver, the search finally worked.

Here's the full source code for resolver:

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

module Resolvers
  class EventResolver
    include SearchObject.module(:graphql)

    type Types::EventType.connection_type, null: false

    scope { object.respond_to?(:subscribed_events) ? object.subscribed_events : object.events }

    option(:from_date, type: Types::DateType) { |scope, value| scope.after(value) }
  end
end

One thing to note: from_date is visible as fromDate argument in GraphQL schema. This is what I want, but just something to keep in mind.

@RStankov do you think anything from here should be added to the README.md? Or perhaps the gem should autoload the missing constants/modules?

I'm using rails (5.2.3) and graphql (1.9.9).

PS I don't use 'spring' gem in my app.

RStankov commented 4 years ago

I think this should be added to README. Because I don't want to force autoloading things which won't be used from search object to the users.

vfonic commented 4 years ago

I find it very rare that gems don't autoload main files/constants. Isn't the whole idea of gem 'gem_name', require: false to opt-in for not autoloading things?

RStankov commented 4 years ago

Yes, the but most people don't think about this and get a lot of unneeded coded. I know it is a bit inconvenient. Will update README to explain this.

Thanks for mentioning it.

vfonic commented 4 years ago

You're right. Most people probably do that. Me included. :)

I think the expectation of a ruby gem would be for it to get required when specified without require: false. That would be the most common solution. I'd make it so that the gem does autoload, and add to README instructions how to disable autoloading.

Either way, thanks for looking into this!

RStankov commented 4 years ago

✅Updated the README

ItsRhen commented 3 years ago

The naming convention for option declaration needs some love. I just killed 3 hours because the example has them camelCase and they'd only run when snake_case. That said, love the plugin! Well done.

RStankov commented 3 years ago

@ItsRhen sorry that this happened. Which example you are referring to and how do you think we can improve on this example?