Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,20 +358,12 @@ type Query {

#### Class-based schemas

The `@stitch` directive can be added to class-based schemas with a directive class:
The `@stitch` directive can be added to class-based schemas with a directive definition provided by the library:

```ruby
class StitchingResolver < GraphQL::Schema::Directive
graphql_name "stitch"
locations FIELD_DEFINITION
repeatable true
argument :key, String, required: true
argument :arguments, String, required: false
end

class Query < GraphQL::Schema::Object
field :product, Product, null: false do
directive StitchingResolver, key: "id"
directive GraphQL::Stitching::Directives::Stitch, key: "id"
argument :id, ID, required: true
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/graphql/stitching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def stitch_directive
end
end

require_relative "stitching/directives"
require_relative "stitching/supergraph"
require_relative "stitching/client"
require_relative "stitching/composer"
Expand Down
14 changes: 14 additions & 0 deletions lib/graphql/stitching/directives.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module GraphQL::Stitching
module Directives
class Stitch < GraphQL::Schema::Directive
graphql_name "stitch"
locations FIELD_DEFINITION
argument :key, String, required: true
argument :arguments, String, required: false
argument :type_name, String, required: false
repeatable true
end
end
end
5 changes: 2 additions & 3 deletions test/graphql/stitching/executor/shaper_grooming_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "test_helper"
require_relative "../../../schemas/introspection"

describe "GraphQL::Stitching::Executor::Shaper, grooming" do
def test_prunes_stitching_fields
Expand Down Expand Up @@ -158,10 +157,10 @@ def test_handles_introspection_types
schema = GraphQL::Schema.from_definition(schema_sdl)
request = GraphQL::Stitching::Request.new(
supergraph_from_schema(schema),
INTROSPECTION_QUERY,
GraphQL::Introspection::INTROSPECTION_QUERY,
)

raw = schema.execute(INTROSPECTION_QUERY).to_h
raw = schema.execute(GraphQL::Introspection::INTROSPECTION_QUERY).to_h
assert GraphQL::Stitching::Executor::Shaper.new(request).perform!(raw)
end
end
3 changes: 1 addition & 2 deletions test/graphql/stitching/integration/introspection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require "test_helper"
require_relative "../../../schemas/example"
require_relative "../../../schemas/introspection"

describe 'GraphQL::Stitching, introspection' do
def setup
Expand All @@ -14,7 +13,7 @@ def setup
end

def test_performs_full_introspection
result = plan_and_execute(@supergraph, INTROSPECTION_QUERY)
result = plan_and_execute(@supergraph, GraphQL::Introspection::INTROSPECTION_QUERY)

introspection_types = result.dig("data", "__schema", "types").map { _1["name"] }
expected_types = ["Manufacturer", "Product", "Query", "Storefront"]
Expand Down
3 changes: 1 addition & 2 deletions test/graphql/stitching/planner/plan_introspection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require "test_helper"
require_relative "../../../schemas/example"
require_relative "../../../schemas/introspection"

describe "GraphQL::Stitching::Planner, introspection" do
def setup
Expand All @@ -14,7 +13,7 @@ def setup
def test_plans_full_introspection_query
plan = GraphQL::Stitching::Request.new(
@supergraph,
INTROSPECTION_QUERY,
GraphQL::Introspection::INTROSPECTION_QUERY,
operation_name: "IntrospectionQuery",
).plan

Expand Down
18 changes: 5 additions & 13 deletions test/schemas/arguments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@

module Schemas
module Arguments
class StitchingResolver < GraphQL::Schema::Directive
graphql_name "stitch"
locations FIELD_DEFINITION
argument :key, String, required: true
argument :arguments, String, required: false
repeatable true
end

DIRECTORS = [
{ id: "1", name: "Steven Spielberg" },
{ id: "2", name: "Christopher Nolan" },
Expand Down Expand Up @@ -78,7 +70,7 @@ class Movie < GraphQL::Schema::Object

class Query < GraphQL::Schema::Object
field :movies, [Movie, null: true], null: false do
directive StitchingResolver, key: "id"
directive GraphQL::Stitching::Directives::Stitch, key: "id"
argument :ids, [ID], required: true
end

Expand Down Expand Up @@ -133,7 +125,7 @@ class ScalarKey < GraphQL::Schema::Scalar

class Query < GraphQL::Schema::Object
field :movies2, [Movie, null: true], null: false do
directive StitchingResolver, key: "id", arguments: "ids: $.id, status: STREAMING"
directive GraphQL::Stitching::Directives::Stitch, key: "id", arguments: "ids: $.id, status: STREAMING"
argument :ids, [ID], required: true
argument :status, MovieStatus, required: true
end
Expand All @@ -144,7 +136,7 @@ def movies2(ids:, status: nil)
end

field :director, Director, null: false do
directive StitchingResolver, key: "id", arguments: "key: { subkey: { id: $.id } }"
directive GraphQL::Stitching::Directives::Stitch, key: "id", arguments: "key: { subkey: { id: $.id } }"
argument :key, ComplexKey, required: true
end

Expand All @@ -153,7 +145,7 @@ def director(key:)
end

field :studios, [Studio, null: true], null: false do
directive StitchingResolver, key: "id", arguments: "keys: { subkey: { id: $.id } }"
directive GraphQL::Stitching::Directives::Stitch, key: "id", arguments: "keys: { subkey: { id: $.id } }"
argument :keys, [ScalarKey], required: true
end

Expand All @@ -162,7 +154,7 @@ def studios(keys:)
end

field :genres, [Genre, null: true], null: false do
directive StitchingResolver, key: "id", arguments: "keys: $.id, prefix: 'action'"
directive GraphQL::Stitching::Directives::Stitch, key: "id", arguments: "keys: $.id, prefix: 'action'"
argument :keys, [ID], required: true
argument :prefix, String, required: false
end
Expand Down
16 changes: 4 additions & 12 deletions test/schemas/composite_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@

module Schemas
module CompositeKeys
class StitchingResolver < GraphQL::Schema::Directive
graphql_name "stitch"
locations FIELD_DEFINITION
argument :key, String, required: true
argument :arguments, String, required: false
repeatable true
end

PAGES = [
{
id: '1',
Expand Down Expand Up @@ -51,7 +43,7 @@ def a

class Query < GraphQL::Schema::Object
field :pages_by_id, [Page, null: true], null: false do
directive StitchingResolver, key: "id"
directive GraphQL::Stitching::Directives::Stitch, key: "id"
argument :ids, [ID], required: true
end

Expand All @@ -78,7 +70,7 @@ def b

class Query < GraphQL::Schema::Object
field :pages_by_sku, [Page, null: true], null: false do
directive StitchingResolver, key: "sku"
directive GraphQL::Stitching::Directives::Stitch, key: "sku"
argument :skus, [ID], required: true
end

Expand Down Expand Up @@ -115,7 +107,7 @@ class PageHandleKey < GraphQL::Schema::InputObject

class Query < GraphQL::Schema::Object
field :pages_by_handle, [Page, null: true], null: false do
directive StitchingResolver, key: "handle scope", arguments: "keys: { handle: $.handle, scope: $.scope }"
directive GraphQL::Stitching::Directives::Stitch, key: "handle scope", arguments: "keys: { handle: $.handle, scope: $.scope }"
argument :keys, [PageHandleKey], required: true
end

Expand Down Expand Up @@ -153,7 +145,7 @@ class PageOwnerKey < GraphQL::Schema::InputObject

class Query < GraphQL::Schema::Object
field :pages_by_owner, [Page, null: true], null: false do
directive StitchingResolver, key: "owner { id type }", arguments: "keys: { id: $.owner.id, type: $.owner.type }"
directive GraphQL::Stitching::Directives::Stitch, key: "owner { id type }", arguments: "keys: { id: $.owner.id, type: $.owner.type }"
argument :keys, [PageOwnerKey], required: true
end

Expand Down
11 changes: 2 additions & 9 deletions test/schemas/conditionals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@

module Schemas
module Conditionals
class Resolver < GraphQL::Schema::Directive
graphql_name "stitch"
locations FIELD_DEFINITION
argument :key, String
repeatable true
end

FRUITS = [
{ id: '1', extension_id: '11', __typename: 'Apple' },
{ id: '2', extension_id: '22', __typename: 'Banana' },
Expand All @@ -22,7 +15,7 @@ class AppleExtension < GraphQL::Schema::Object

class Query < GraphQL::Schema::Object
field :apple_extension, AppleExtension, null: true do
directive Resolver, key: "id"
directive GraphQL::Stitching::Directives::Stitch, key: "id"
argument :id, ID, required: true
end

Expand All @@ -42,7 +35,7 @@ class BananaExtension < GraphQL::Schema::Object

class Query < GraphQL::Schema::Object
field :banana_extension, BananaExtension, null: true do
directive Resolver, key: "id"
directive GraphQL::Stitching::Directives::Stitch, key: "id"
argument :id, ID, required: true
end

Expand Down
15 changes: 4 additions & 11 deletions test/schemas/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@

module Schemas
module Errors
class Resolver < GraphQL::Schema::Directive
graphql_name "stitch"
locations FIELD_DEFINITION
argument :key, String
repeatable true
end

ISOTOPES_A = [
{ id: '1', name: 'Ne20' },
{ id: '2', name: 'Kr79' },
Expand Down Expand Up @@ -43,7 +36,7 @@ class Element < GraphQL::Schema::Object

class Query < GraphQL::Schema::Object
field :elements_a, [Element, null: true], null: false do
directive Resolver, key: "id"
directive GraphQL::Stitching::Directives::Stitch, key: "id"
argument :ids, [ID], required: true
end

Expand All @@ -62,7 +55,7 @@ def element_a(id:)
end

field :isotope_a, Isotope, null: true do
directive Resolver, key: "id"
directive GraphQL::Stitching::Directives::Stitch, key: "id"
argument :id, ID, required: true
end

Expand All @@ -88,7 +81,7 @@ class Element < GraphQL::Schema::Object

class Query < GraphQL::Schema::Object
field :elements_b, [Element, null: true], null: false do
directive Resolver, key: "id"
directive GraphQL::Stitching::Directives::Stitch, key: "id"
argument :ids, [ID], required: true
end

Expand All @@ -99,7 +92,7 @@ def elements_b(ids:)
end

field :isotope_b, Isotope, null: true do
directive Resolver, key: "id"
directive GraphQL::Stitching::Directives::Stitch, key: "id"
argument :id, ID, required: true
end

Expand Down
13 changes: 3 additions & 10 deletions test/schemas/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@

module Schemas
module Example
class Resolver < GraphQL::Schema::Directive
graphql_name "stitch"
locations FIELD_DEFINITION
argument :key, String
repeatable true
end

PRODUCTS = [
{ upc: '1', name: 'iPhone', price: 699.99, manufacturer_id: '1' },
{ upc: '2', name: 'Apple Watch', price: 399.99, manufacturer_id: '1' },
Expand Down Expand Up @@ -56,7 +49,7 @@ def products

class RootQuery < GraphQL::Schema::Object
field :product, Product, null: false do
directive Resolver, key: "upc"
directive GraphQL::Stitching::Directives::Stitch, key: "upc"
argument :upc, ID, required: true
end

Expand All @@ -65,7 +58,7 @@ def product(upc:)
end

field :manufacturer, Manufacturer, null: false do
directive Resolver, key: "id"
directive GraphQL::Stitching::Directives::Stitch, key: "id"
argument :id, ID, required: true
end

Expand Down Expand Up @@ -122,7 +115,7 @@ class Manufacturer < GraphQL::Schema::Object

class Query < GraphQL::Schema::Object
field :manufacturer, Manufacturer, null: true do
directive Resolver, key: "id"
directive GraphQL::Stitching::Directives::Stitch, key: "id"
argument :id, ID, required: true
end

Expand Down
13 changes: 3 additions & 10 deletions test/schemas/federation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@

module Schemas
module Federation
class StitchField < GraphQL::Schema::Directive
graphql_name "stitch"
locations FIELD_DEFINITION
argument :key, String
repeatable true
end

class FederationKey < GraphQL::Schema::Directive
graphql_name "key"
locations OBJECT
Expand Down Expand Up @@ -180,17 +173,17 @@ class Widget < GraphQL::Schema::Object

class Query < GraphQL::Schema::Object
field :gadgets, [Gadget, null: true], null: false do
directive StitchField, key: "id"
directive GraphQL::Stitching::Directives::Stitch, key: "id"
argument :ids, [ID], required: true
end

field :sprockets, [Sprocket, null: true], null: false do
directive StitchField, key: "id"
directive GraphQL::Stitching::Directives::Stitch, key: "id"
argument :ids, [ID], required: true
end

field :widgets, [Widget, null: true], null: false do
directive StitchField, key: "upc"
directive GraphQL::Stitching::Directives::Stitch, key: "upc"
argument :upcs, [ID], required: true
end

Expand Down
Loading