spec/support/star_wars_schema.rb in graphql-relay-0.11.2 vs spec/support/star_wars_schema.rb in graphql-relay-0.12.0

- old
+ new

@@ -5,16 +5,12 @@ # - global id creation & "decrypting" # - a find-object-by-global ID field # - an interface for Relay ObjectTypes to implement # See global_node_identification.rb for the full API. NodeIdentification = GraphQL::Relay::GlobalNodeIdentification.define do - object_from_id -> (id, ctx) do - # In a normal app, you could call `from_global_id` on your defined object - # type_name, id = NodeIdentification.from_global_id(id) - # - # But to support our testing setup, reach for the global: - type_name, id = GraphQL::Relay::GlobalNodeIdentification.from_global_id(id) + object_from_id -> (node_id, ctx) do + type_name, id = NodeIdentification.from_global_id(node_id) STAR_WARS_DATA[type_name][id] end type_from_object -> (object) do if object == :test_error @@ -82,29 +78,30 @@ type types.Int resolve -> (obj, args, ctx) { obj.object.count * 100 } end end +FactionShipsField = GraphQL::Field.define do + # Resolve field should return an Array, the Connection + # will do the rest! + resolve -> (obj, args, ctx) { + all_ships = obj.ships.map {|ship_id| STAR_WARS_DATA["Ship"][ship_id] } + if args[:nameIncludes] + all_ships = all_ships.select { |ship| ship.name.include?(args[:nameIncludes])} + end + all_ships + } + # You can define arguments here and use them in the connection + argument :nameIncludes, types.String +end Faction = GraphQL::ObjectType.define do name "Faction" interfaces [NodeIdentification.interface] field :id, field: GraphQL::Relay::GlobalIdField.new("Faction") field :name, types.String - connection :ships, Ship.connection_type do - # Resolve field should return an Array, the Connection - # will do the rest! - resolve -> (obj, args, ctx) { - all_ships = obj.ships.map {|ship_id| STAR_WARS_DATA["Ship"][ship_id] } - if args[:nameIncludes] - all_ships = all_ships.select { |ship| ship.name.include?(args[:nameIncludes])} - end - all_ships - } - # You can define arguments here and use them in the connection - argument :nameIncludes, types.String - end + connection :ships, Ship.connection_type, field: FactionShipsField connection :bases, BaseConnectionWithTotalCountType do # Resolve field should return an Array, the Connection # will do the rest! resolve -> (obj, args, ctx) { all_bases = Base.where(id: obj.bases) @@ -204,5 +201,6 @@ # The mutation object exposes a field: field :introduceShip, field: IntroduceShipMutation.field end StarWarsSchema = GraphQL::Schema.new(query: QueryType, mutation: MutationType) +StarWarsSchema.node_identification = NodeIdentification