Created
December 4, 2023 15:47
-
-
Save rmosolgo/b413051229bd68f70cbc282e54308096 to your computer and use it in GitHub Desktop.
@defer inside fragments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "bundler/inline" | |
gemfile do | |
gem "graphql", "1.12.24" | |
gem "graphql-pro", "1.24.15" | |
end | |
class Schema < GraphQL::Schema | |
class Thing < GraphQL::Schema::Object | |
field :name, String, null: false | |
end | |
class Query < GraphQL::Schema::Object | |
field :things, [Thing], null: false | |
def things | |
[ | |
{ name: "Hay Bale" }, | |
{ name: "Wheelbarrow" }, | |
{ name: "Fence Post"} | |
] | |
end | |
end | |
query(Query) | |
use GraphQL::Pro::Defer | |
end | |
query_str = <<-GRAPHQL | |
{ | |
things { | |
... ThingFields | |
} | |
} | |
fragment ThingFields on Thing { | |
name @defer | |
} | |
GRAPHQL | |
res = Schema.execute(query_str) | |
pp res.to_h | |
# {"data"=>{"things"=>[{}, {}, {}]}} | |
res.context[:defer].each do |deferral| | |
pp [:deferred, deferral.to_h] | |
end | |
# [:deferred, {:hasNext=>true, :data=>{"things"=>[{}, {}, {}]}}] | |
# [:deferred, {:path=>["things", 0, "name"], :hasNext=>true, :data=>"Hay Bale"}] | |
# [:deferred, {:path=>["things", 1, "name"], :hasNext=>true, :data=>"Wheelbarrow"}] | |
# [:deferred, {:path=>["things", 2, "name"], :hasNext=>false, :data=>"Fence Post"}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment