Skip to content

Commit 5fefd60

Browse files
authored
add directive definitions (#172)
1 parent 9186c2f commit 5fefd60

File tree

18 files changed

+56
-210
lines changed

18 files changed

+56
-210
lines changed

README.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -358,20 +358,12 @@ type Query {
358358

359359
#### Class-based schemas
360360

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

363363
```ruby
364-
class StitchingResolver < GraphQL::Schema::Directive
365-
graphql_name "stitch"
366-
locations FIELD_DEFINITION
367-
repeatable true
368-
argument :key, String, required: true
369-
argument :arguments, String, required: false
370-
end
371-
372364
class Query < GraphQL::Schema::Object
373365
field :product, Product, null: false do
374-
directive StitchingResolver, key: "id"
366+
directive GraphQL::Stitching::Directives::Stitch, key: "id"
375367
argument :id, ID, required: true
376368
end
377369
end

lib/graphql/stitching.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def stitch_directive
5353
end
5454
end
5555

56+
require_relative "stitching/directives"
5657
require_relative "stitching/supergraph"
5758
require_relative "stitching/client"
5859
require_relative "stitching/composer"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
module GraphQL::Stitching
4+
module Directives
5+
class Stitch < GraphQL::Schema::Directive
6+
graphql_name "stitch"
7+
locations FIELD_DEFINITION
8+
argument :key, String, required: true
9+
argument :arguments, String, required: false
10+
argument :type_name, String, required: false
11+
repeatable true
12+
end
13+
end
14+
end

test/graphql/stitching/executor/shaper_grooming_test.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

33
require "test_helper"
4-
require_relative "../../../schemas/introspection"
54

65
describe "GraphQL::Stitching::Executor::Shaper, grooming" do
76
def test_prunes_stitching_fields
@@ -158,10 +157,10 @@ def test_handles_introspection_types
158157
schema = GraphQL::Schema.from_definition(schema_sdl)
159158
request = GraphQL::Stitching::Request.new(
160159
supergraph_from_schema(schema),
161-
INTROSPECTION_QUERY,
160+
GraphQL::Introspection::INTROSPECTION_QUERY,
162161
)
163162

164-
raw = schema.execute(INTROSPECTION_QUERY).to_h
163+
raw = schema.execute(GraphQL::Introspection::INTROSPECTION_QUERY).to_h
165164
assert GraphQL::Stitching::Executor::Shaper.new(request).perform!(raw)
166165
end
167166
end

test/graphql/stitching/integration/introspection_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
require "test_helper"
44
require_relative "../../../schemas/example"
5-
require_relative "../../../schemas/introspection"
65

76
describe 'GraphQL::Stitching, introspection' do
87
def setup
@@ -14,7 +13,7 @@ def setup
1413
end
1514

1615
def test_performs_full_introspection
17-
result = plan_and_execute(@supergraph, INTROSPECTION_QUERY)
16+
result = plan_and_execute(@supergraph, GraphQL::Introspection::INTROSPECTION_QUERY)
1817

1918
introspection_types = result.dig("data", "__schema", "types").map { _1["name"] }
2019
expected_types = ["Manufacturer", "Product", "Query", "Storefront"]

test/graphql/stitching/planner/plan_introspection_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
require "test_helper"
44
require_relative "../../../schemas/example"
5-
require_relative "../../../schemas/introspection"
65

76
describe "GraphQL::Stitching::Planner, introspection" do
87
def setup
@@ -14,7 +13,7 @@ def setup
1413
def test_plans_full_introspection_query
1514
plan = GraphQL::Stitching::Request.new(
1615
@supergraph,
17-
INTROSPECTION_QUERY,
16+
GraphQL::Introspection::INTROSPECTION_QUERY,
1817
operation_name: "IntrospectionQuery",
1918
).plan
2019

test/schemas/arguments.rb

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22

33
module Schemas
44
module Arguments
5-
class StitchingResolver < GraphQL::Schema::Directive
6-
graphql_name "stitch"
7-
locations FIELD_DEFINITION
8-
argument :key, String, required: true
9-
argument :arguments, String, required: false
10-
repeatable true
11-
end
12-
135
DIRECTORS = [
146
{ id: "1", name: "Steven Spielberg" },
157
{ id: "2", name: "Christopher Nolan" },
@@ -78,7 +70,7 @@ class Movie < GraphQL::Schema::Object
7870

7971
class Query < GraphQL::Schema::Object
8072
field :movies, [Movie, null: true], null: false do
81-
directive StitchingResolver, key: "id"
73+
directive GraphQL::Stitching::Directives::Stitch, key: "id"
8274
argument :ids, [ID], required: true
8375
end
8476

@@ -133,7 +125,7 @@ class ScalarKey < GraphQL::Schema::Scalar
133125

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

146138
field :director, Director, null: false do
147-
directive StitchingResolver, key: "id", arguments: "key: { subkey: { id: $.id } }"
139+
directive GraphQL::Stitching::Directives::Stitch, key: "id", arguments: "key: { subkey: { id: $.id } }"
148140
argument :key, ComplexKey, required: true
149141
end
150142

@@ -153,7 +145,7 @@ def director(key:)
153145
end
154146

155147
field :studios, [Studio, null: true], null: false do
156-
directive StitchingResolver, key: "id", arguments: "keys: { subkey: { id: $.id } }"
148+
directive GraphQL::Stitching::Directives::Stitch, key: "id", arguments: "keys: { subkey: { id: $.id } }"
157149
argument :keys, [ScalarKey], required: true
158150
end
159151

@@ -162,7 +154,7 @@ def studios(keys:)
162154
end
163155

164156
field :genres, [Genre, null: true], null: false do
165-
directive StitchingResolver, key: "id", arguments: "keys: $.id, prefix: 'action'"
157+
directive GraphQL::Stitching::Directives::Stitch, key: "id", arguments: "keys: $.id, prefix: 'action'"
166158
argument :keys, [ID], required: true
167159
argument :prefix, String, required: false
168160
end

test/schemas/composite_keys.rb

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22

33
module Schemas
44
module CompositeKeys
5-
class StitchingResolver < GraphQL::Schema::Directive
6-
graphql_name "stitch"
7-
locations FIELD_DEFINITION
8-
argument :key, String, required: true
9-
argument :arguments, String, required: false
10-
repeatable true
11-
end
12-
135
PAGES = [
146
{
157
id: '1',
@@ -51,7 +43,7 @@ def a
5143

5244
class Query < GraphQL::Schema::Object
5345
field :pages_by_id, [Page, null: true], null: false do
54-
directive StitchingResolver, key: "id"
46+
directive GraphQL::Stitching::Directives::Stitch, key: "id"
5547
argument :ids, [ID], required: true
5648
end
5749

@@ -78,7 +70,7 @@ def b
7870

7971
class Query < GraphQL::Schema::Object
8072
field :pages_by_sku, [Page, null: true], null: false do
81-
directive StitchingResolver, key: "sku"
73+
directive GraphQL::Stitching::Directives::Stitch, key: "sku"
8274
argument :skus, [ID], required: true
8375
end
8476

@@ -115,7 +107,7 @@ class PageHandleKey < GraphQL::Schema::InputObject
115107

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

@@ -153,7 +145,7 @@ class PageOwnerKey < GraphQL::Schema::InputObject
153145

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

test/schemas/conditionals.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22

33
module Schemas
44
module Conditionals
5-
class Resolver < GraphQL::Schema::Directive
6-
graphql_name "stitch"
7-
locations FIELD_DEFINITION
8-
argument :key, String
9-
repeatable true
10-
end
11-
125
FRUITS = [
136
{ id: '1', extension_id: '11', __typename: 'Apple' },
147
{ id: '2', extension_id: '22', __typename: 'Banana' },
@@ -22,7 +15,7 @@ class AppleExtension < GraphQL::Schema::Object
2215

2316
class Query < GraphQL::Schema::Object
2417
field :apple_extension, AppleExtension, null: true do
25-
directive Resolver, key: "id"
18+
directive GraphQL::Stitching::Directives::Stitch, key: "id"
2619
argument :id, ID, required: true
2720
end
2821

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

4336
class Query < GraphQL::Schema::Object
4437
field :banana_extension, BananaExtension, null: true do
45-
directive Resolver, key: "id"
38+
directive GraphQL::Stitching::Directives::Stitch, key: "id"
4639
argument :id, ID, required: true
4740
end
4841

test/schemas/errors.rb

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22

33
module Schemas
44
module Errors
5-
class Resolver < GraphQL::Schema::Directive
6-
graphql_name "stitch"
7-
locations FIELD_DEFINITION
8-
argument :key, String
9-
repeatable true
10-
end
11-
125
ISOTOPES_A = [
136
{ id: '1', name: 'Ne20' },
147
{ id: '2', name: 'Kr79' },
@@ -43,7 +36,7 @@ class Element < GraphQL::Schema::Object
4336

4437
class Query < GraphQL::Schema::Object
4538
field :elements_a, [Element, null: true], null: false do
46-
directive Resolver, key: "id"
39+
directive GraphQL::Stitching::Directives::Stitch, key: "id"
4740
argument :ids, [ID], required: true
4841
end
4942

@@ -62,7 +55,7 @@ def element_a(id:)
6255
end
6356

6457
field :isotope_a, Isotope, null: true do
65-
directive Resolver, key: "id"
58+
directive GraphQL::Stitching::Directives::Stitch, key: "id"
6659
argument :id, ID, required: true
6760
end
6861

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

8982
class Query < GraphQL::Schema::Object
9083
field :elements_b, [Element, null: true], null: false do
91-
directive Resolver, key: "id"
84+
directive GraphQL::Stitching::Directives::Stitch, key: "id"
9285
argument :ids, [ID], required: true
9386
end
9487

@@ -99,7 +92,7 @@ def elements_b(ids:)
9992
end
10093

10194
field :isotope_b, Isotope, null: true do
102-
directive Resolver, key: "id"
95+
directive GraphQL::Stitching::Directives::Stitch, key: "id"
10396
argument :id, ID, required: true
10497
end
10598

0 commit comments

Comments
 (0)