Skip to content

Instantly share code, notes, and snippets.

@rmosolgo
Created December 4, 2023 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmosolgo/b413051229bd68f70cbc282e54308096 to your computer and use it in GitHub Desktop.
Save rmosolgo/b413051229bd68f70cbc282e54308096 to your computer and use it in GitHub Desktop.
@defer inside fragments
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