Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a pattern for testing mutations directly? #2322

Closed
jjb opened this issue Jun 12, 2019 · 2 comments
Closed

Is there a pattern for testing mutations directly? #2322

jjb opened this issue Jun 12, 2019 · 2 comments

Comments

@jjb
Copy link

jjb commented Jun 12, 2019

Is there a pattern for testing mutations directly, without going through the HTTP or controller layer? This blog post proposes one:

https://www.codementor.io/karanjaeddy/build-a-to-do-list-api-with-graphql-api-rails-5-part-1-irjt1e7jm#mutations

But I haven't been able to get it to work in my app for an endpoint of this form

  DoThing = GraphQL::Relay::Mutation.define do
    name "doThing"
    resolve (obj, inputs, ctx) {

I want to invoke resolve on something but I can't figure out how to get to it in UserMutations::CompleteCheckout. There's a @resolve_proc instance variable but I would have to use metaprogramming to access it.

Let me know any ideas and I'll try to contribute to the docs with what I come up with!

@rmosolgo
Copy link
Owner

without going through the HTTP or controller layer?

You can call MySchema.execute(...) directly, with a GraphQL query string, but that's the only other option that I know of.

I recommend writing an "integration test" like that ☝️ , but then extracting all the mutation's logic into plain ol' Ruby code, and unit testing that separately.

For example, if you have a complicated AddUserToTeam mutation, add

# app/models/user/add_to_team.rb
class User::AddToTeam 
  def initialize(graphql_inputs) 
    # ...
  end 

  def perform 
    # ... do the stuff 
  end 
end 

Then test that class on its own. (Call it a "Service object" if you like :P)

Then the mutation's resolve is "just":

User::AddToTeam.new(args).perform 

@jjb
Copy link
Author

jjb commented Jul 7, 2019

Thanks! Here's what I came up with: https://github.com/rmosolgo/graphql-ruby/wiki/Writing-tests-for-mutations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants