Created
December 9, 2024 14:45
-
-
Save rmosolgo/87b273189a894d6263efe3e9303dc3b8 to your computer and use it in GitHub Desktop.
GraphQL-Ruby: using the same resolver with different return types (list vs. connection)
This file contains hidden or 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", "2.4.7" | |
end | |
class MySchema < GraphQL::Schema | |
class GetItems < GraphQL::Schema::Resolver | |
def resolve | |
[ | |
{ name: "Piano" }, | |
{ name: "Coffee Mug" }, | |
{ name: "Blueberry" } | |
] | |
end | |
end | |
class Item < GraphQL::Schema::Object | |
field :name, String | |
end | |
class Query < GraphQL::Schema::Object | |
field :legacy_items, [Item], resolver: GetItems | |
field :items, Item.connection_type, resolver: GetItems | |
end | |
query(Query) | |
end | |
puts MySchema.to_definition | |
# `Query.items` gets connection arguments: | |
# type Item { | |
# name: String | |
# } | |
# | |
# """ | |
# The connection type for Item. | |
# """ | |
# type ItemConnection { | |
# """ | |
# A list of edges. | |
# """ | |
# edges: [ItemEdge] | |
# | |
# """ | |
# A list of nodes. | |
# """ | |
# nodes: [Item] | |
# | |
# """ | |
# Information to aid in pagination. | |
# """ | |
# pageInfo: PageInfo! | |
# } | |
# | |
# """ | |
# An edge in a connection. | |
# """ | |
# type ItemEdge { | |
# """ | |
# A cursor for use in pagination. | |
# """ | |
# cursor: String! | |
# | |
# """ | |
# The item at the end of the edge. | |
# """ | |
# node: Item | |
# } | |
# | |
# """ | |
# Information about pagination in a connection. | |
# """ | |
# type PageInfo { | |
# """ | |
# When paginating forwards, the cursor to continue. | |
# """ | |
# endCursor: String | |
# | |
# """ | |
# When paginating forwards, are there more items? | |
# """ | |
# hasNextPage: Boolean! | |
# | |
# """ | |
# When paginating backwards, are there more items? | |
# """ | |
# hasPreviousPage: Boolean! | |
# | |
# """ | |
# When paginating backwards, the cursor to continue. | |
# """ | |
# startCursor: String | |
# } | |
# | |
# type Query { | |
# items( | |
# """ | |
# Returns the elements in the list that come after the specified cursor. | |
# """ | |
# after: String | |
# | |
# """ | |
# Returns the elements in the list that come before the specified cursor. | |
# """ | |
# before: String | |
# | |
# """ | |
# Returns the first _n_ elements from the list. | |
# """ | |
# first: Int | |
# | |
# """ | |
# Returns the last _n_ elements from the list. | |
# """ | |
# last: Int | |
# ): ItemConnection | |
# legacyItems: [Item!] | |
# } | |
pp MySchema.execute("{ legacyItems { name } }").to_h | |
# Plain list: | |
# {"data"=>{"legacyItems"=>[{"name"=>"Piano"}, {"name"=>"Coffee Mug"}, {"name"=>"Blueberry"}]}} | |
pp MySchema.execute("{ items { nodes { name } } }").to_h | |
# Connection: | |
# {"data"=>{"items"=>{"nodes"=>[{"name"=>"Piano"}, {"name"=>"Coffee Mug"}, {"name"=>"Blueberry"}]}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment