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
Circular dependencies in Class Based API without rails autoloading #1929
Comments
|
Hi, good question! It's not well documented but you can also use strings in cases like this, for example: field :author, "Types::User", null: falseHere's a little runable example: require "graphql"
class Query < GraphQL::Schema::Object
field :a, "TypeA", null: false
end
class TypeA < GraphQL::Schema::Object
field :b, "TypeB", null: false
end
class TypeB < GraphQL::Schema::Object
field :a, "TypeA", null: false
end
class Schema < GraphQL::Schema
query(Query)
end
root = OpenStruct.new(a: OpenStruct.new(b: OpenStruct.new(a: 1)))
puts Schema.execute("{ a { b { a { __typename } } } }", root_value: root ).to_h
{"data"=>{"a"=>{"b"=>{"a"=>{"__typename"=>"TypeA"}}}}}Would using strings work in your case? |
|
Hey, that works perfectly! Thanks |
|
I'm encountering this issue with circular dependencies, but my field returns an array instead of a single object: class TypeA < GraphQL::Schema::Object
field :b, [TypeB], null: false
endSurrounding the type class in quotes does not resolve the issue, e.g.: class TypeA < GraphQL::Schema::Object
field :b, ["TypeB"], null: false
endor class TypeA < GraphQL::Schema::Object
field :b, "[TypeB]", null: false
endAre there any recommendations on how to overcome this issue in this particular case? |
|
I would expect wrapping the whole thing in a string ( require "graphql"
class Query < GraphQL::Schema::Object
field :a, "[TypeA]", null: false
end
class TypeA < GraphQL::Schema::Object
field :b, "[TypeB]", null: false
end
class TypeB < GraphQL::Schema::Object
field :a, "[TypeA]", null: false
end
class Schema < GraphQL::Schema
query(Query)
end
root = OpenStruct.new(a: [OpenStruct.new(b: [OpenStruct.new(a: [1])])])
puts Schema.execute("{ a { b { a { __typename } } } }", root_value: root ).to_h
# {"data"=>{"a"=>[{"b"=>[{"a"=>[{"__typename"=>"TypeA"}]}]}]}}If that's not working for you, please open a new issue with more details about your situation and the error you encountered. |
I am either missing something or it does not appear to be possible to set up circularly referencing objects with the class based API if you're not using the rails autoloader. The example in the introductory blog post
http://rmosolgo.github.io/blog/2018/03/25/why-a-new-schema-definition-api/
only works because the rails autoloader kicks in on
Module#const_missingat step 3. If we were to manually require in the files, we would have the following execution order.This can be seen in the stack trace you get running the above code
(Ruby 2.4.3, grapqhl 1.8.11)
Is the rails autoloader a hard dependency of the new schema definition API or is there a different way to solve this problem?
The text was updated successfully, but these errors were encountered: