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

Mutation argument loads "No object found for..." error #3869

Closed
bessey opened this issue Jan 27, 2022 · 8 comments
Closed

Mutation argument loads "No object found for..." error #3869

bessey opened this issue Jan 27, 2022 · 8 comments

Comments

@bessey
Copy link
Contributor

bessey commented Jan 27, 2022

Background: I am trying to create a minimum reproduction case for a different issue in the GraphQL Pro Pundit integration, but this is blocking me from building that out!

Describe the bug

I am attempting to follow
https://graphql-ruby.org/mutations/mutation_classes.html#auto-loading-arguments
to create a simple schema that loads a mutation argument. I can see that the code that loads the argument is reached, however this object is discarded it seems.

I am probably missing something really stupid, but until I get past this I can't validate my much more interesting bug report, so here I am.

Demo

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'graphql', '1.13.6'
  gem 'graphql-pro',  '1.21.1', source: 'https://gems.graphql.pro/'
end

class Impact
  def initialize(id)
    @id = id
  end

  attr_reader :id
end

class SimpleMutation < GraphQL::Schema::Mutation
end
class ImpactType < GraphQL::Schema::Object
  field :impact, Float, null: true
end

class SimpleOptionUpdate < SimpleMutation
  argument :id, ID, required: false, loads: ImpactType, as: :impact #, pundit_role: :manage


  field :thing, String

  def resolve(impact: nil)
    "hi"
  end

end

class MutationType < GraphQL::Schema::Object
  field :simple_option_update, mutation: SimpleOptionUpdate
end


class MySchema < GraphQL::Schema
  mutation(MutationType)

  def self.object_from_id(id, _ctx)
    puts "Object ran #{id}"
    Impact.new(id)
  end

  def self.id_from_object(object, _type_definition, _ctx)
    puts "ID FROM OBJECT RAN"
    object.id
  end
end

query = <<-GRAPHQL
  mutation {
    simpleOptionUpdate(id: "123") {
      thing
    }
  }
GRAPHQL

result = MySchema.execute(query, variables: {}, context: {})
puts result.to_h

I also tried including this in the schema but it did not change the outcome

  def self.resolve_type(type, obj, context)
    puts "RESOLVED TYPE #{obj.class}"
    case obj
    when Impact
      ImpactType
    else
      raise("Unexpected object: #{obj}")
    end.tap do |type|
      puts "we chose #{type}"
    end
  end

Expected behavior

Loads the object, enters the resolver code.

Actual behavior

Resolver is never reached because argument load fails:

$ ruby ./pundit-authz.rb
Object ran 123
{"data"=>{"simpleOptionUpdate"=>nil}, "errors"=>[{"message"=>"No object found for `id: \"123\"`", "locations"=>[{"line"=>2, "column"=>5}], "path"=>["simpleOptionUpdate"]}]}
@rmosolgo
Copy link
Owner

Hey -- I'll read the content of this issue in a few minutes, but I noticed that the original body included your graphql pro bundler credential.

I've created a new credential and added it to https://billing.graphql.pro. Could you please log on there, get the new credential, and update your application to to use it? Then let me know and I'll disable the old one. Thanks!

@bessey
Copy link
Contributor Author

bessey commented Jan 27, 2022

Ah bugger sorry Robert! Shall do

@bessey
Copy link
Contributor Author

bessey commented Jan 27, 2022

I've launched the new credentials to production, and I have deleted my revision from the history in GH. It will take a few days to naturally progress to actively developed branches. If you can stomach it I would appreciate delaying the revoke a week, but if not I can expedite on our side.

Feel free to delete these comments too.

@rmosolgo
Copy link
Owner

The problem is, you can't loads: ... a type that isn't part of the schema. Since impact type isn't the return type of any fields, GraphQL-Ruby thinks it's hidden from the current viewer. (Sadly, it doesn't tell you, the developer, this, TODO: #2286 .)

To make it work again:

    field :thing, String 
+   # add `ImpactType` to the schema somehow
+   field :impact, ImpactType

    def resolve(impact: nil)
-     "hi"
+     # just to make sure it's really getting through:
+     { thing: impact.id.to_s }
    end

For me, that outputs:

{"data"=>{"simpleOptionUpdate"=>{"thing"=>"123"}}}

Does that unblock you?

@bessey
Copy link
Contributor Author

bessey commented Jan 31, 2022

It does, thank you!

@bessey bessey closed this as completed Jan 31, 2022
@bessey
Copy link
Contributor Author

bessey commented Jul 29, 2022

I just got bitten by this again in a new context in a greenfield project. Luckily, I found my own ticket when Googling for answers 😆 but just wanted to note that its common enough that I ran into it twice in 6 months. Could be me being stupid too though 😋

@rmosolgo
Copy link
Owner

No, it should really be better. I think logging some graphql info would be a good start, see #2286 for a suggestion

@jsierles
Copy link
Contributor

jsierles commented Aug 4, 2022

The solution posted here still didn't work for me - adding the unexpected type as a field. Also the type appears to be in the schema already.

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

3 participants