RStankov / SearchObjectGraphQL

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

Testing Resolvers #23

Closed jdpaterson closed 4 years ago

jdpaterson commented 4 years ago

I really like this functionality and I think it is helpful to have for the majority of query-types in the apps I'm writing. One thing I noticed though is that in the Ruby-Graphql documentation it lists some warnings about using resolvers when they might not be necessary. Is there any possiblity of pulling some of the logic out of the resolver and moving it into a separate class, so that we can test it without having to test the resolver? Or maybe even calling SearchObject from a regular query definition could be more ideal than having to use a resolver?

For instance when using the standalone SearchObject gem I could do this:

field posts, [Types::PostType], null: false do 
  argument filters, Input::PostInput, required: true
end
def posts(filters: {})
  PostSearch.new(filters: filters).results
end

And I could easily test the PostSearch separately from any GQL tests. But then I wouldn't have access to the added features of this plugin in the PostSearch.

Happy to help if any of the above is feasible. Thanks

RStankov commented 4 years ago

Hey,

You can do this. Having just a method works for simple cases. Usually, I do it for resolvers without arguments.

In the case of search object, arguments and search object are related. Most of the time you will want to add argument when adding a new filter. So having a resolver makes it easier.

You can use those helpers -> https://gist.github.com/RStankov/23831f5d2df26a2a91feaaf18b7816ab to test resolvers.