spec/support/star_wars_schema.rb in graphql-relay-0.3.0 vs spec/support/star_wars_schema.rb in graphql-relay-0.3.1

- old
+ new

@@ -29,10 +29,20 @@ global_id_field :id field :name, types.String field :planet, types.String end +# Define a connection which will wrap an ActiveRecord::Relation. +# We use an optional block to add fields to the connection type: +BaseConnection = GraphQL::Relay::RelationConnection.create_type(BaseType) do + field :totalCount do + type types.Int + resolve -> (obj, args, ctx) { obj.object.count } + end +end + + Faction = GraphQL::ObjectType.define do name "Faction" interfaces [NodeInterface] field :id, field: GraphQL::Relay::GlobalIdField.new("Faction") field :name, types.String @@ -47,15 +57,20 @@ all_ships } # You can define arguments here and use them in the connection argument :nameIncludes, types.String end - connection :bases, BaseType.connection_type do + connection :bases, BaseConnection do # Resolve field should return an Array, the Connection # will do the rest! resolve -> (obj, args, ctx) { - Base.where(id: obj.bases) + all_bases = Base.where(id: obj.bases) + if args[:nameIncludes] + all_bases = all_bases.where("name LIKE ?", "%#{args[:nameIncludes]}%") + end + all_bases } + argument :nameIncludes, types.String end end # Define a mutation. It will also: # - define a derived InputObjectType