|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | module Schemas |
4 | | - module VisibilityDirective |
5 | | - class StitchDirective < GraphQL::Schema::Directive |
6 | | - graphql_name "stitch" |
7 | | - locations FIELD_DEFINITION |
8 | | - argument :key, String |
9 | | - repeatable true |
10 | | - end |
11 | | - |
12 | | - class VisibilityDirective < GraphQL::Schema::Directive |
13 | | - graphql_name "visibility" |
14 | | - locations FIELD_DEFINITION, ARGUMENT_DEFINITION, ENUM_VALUE, OBJECT |
15 | | - argument :profiles, [String] |
16 | | - end |
| 4 | + module Visibility |
| 5 | + RECORDS = [ |
| 6 | + { id: "1", price: 20.99, msrp: 10.99, quantity_available: 23, quantity_in_stock: 35 }, |
| 7 | + { id: "2", price: 99.99, msrp: 69.99, quantity_available: 77, quantity_in_stock: 100 }, |
| 8 | + ].freeze |
17 | 9 |
|
18 | | - class Alpha < GraphQL::Schema |
| 10 | + class PriceSchema < GraphQL::Schema |
19 | 11 | class Sprocket < GraphQL::Schema::Object |
20 | 12 | field :id, ID, null: false do |f| |
21 | | - f.directive(VisibilityDirective, profiles: ["private"]) |
22 | | - end |
23 | | - |
24 | | - field :color, String, null: false do |f| |
25 | | - f.directive(VisibilityDirective, profiles: ["public"]) |
26 | | - end |
27 | | - |
28 | | - field :size, Integer, null: false do |f| |
29 | | - f.directive(VisibilityDirective, profiles: ["public", "private"]) |
30 | | - end |
31 | | - end |
32 | | - |
33 | | - class Widget < GraphQL::Schema::Object |
34 | | - directive(VisibilityDirective, profiles: ["public"]) |
35 | | - field :id, ID, null: false |
36 | | - end |
37 | | - |
38 | | - class Toggle < GraphQL::Schema::Enum |
39 | | - value "YES" do |v| |
40 | | - v.directive(VisibilityDirective, profiles: ["public"]) |
| 13 | + f.directive(GraphQL::Stitching::Directives::Visibility, profiles: []) |
41 | 14 | end |
42 | 15 |
|
43 | | - value "NO" do |v| |
44 | | - v.directive(VisibilityDirective, profiles: ["private"]) |
45 | | - end |
| 16 | + field :price, Float, null: false |
46 | 17 |
|
47 | | - value "MAYBE" do |v| |
48 | | - v.directive(VisibilityDirective, profiles: ["public", "private"]) |
| 18 | + field :msrp, Float, null: false do |f| |
| 19 | + f.directive(GraphQL::Stitching::Directives::Visibility, profiles: ["private"]) |
49 | 20 | end |
50 | | - |
51 | | - value "ANY" |
52 | 21 | end |
53 | 22 |
|
54 | 23 | class Query < GraphQL::Schema::Object |
55 | | - field :sprocket_a, Sprocket, null: false do |f| |
56 | | - f.directive(StitchDirective, key: "id") |
| 24 | + field :sprocket, Sprocket, null: true do |f| |
| 25 | + f.directive(GraphQL::Stitching::Directives::Stitch, key: "id") |
57 | 26 | f.argument(:id, ID, required: true) |
58 | 27 | end |
59 | 28 |
|
60 | | - def sprocket_a(id:) |
61 | | - { id: id, color: "red", size: 2 } |
62 | | - end |
63 | | - |
64 | | - field :widget_a, Widget, null: false |
65 | | - |
66 | | - def widget_a |
67 | | - { id: 1 } |
68 | | - end |
69 | | - |
70 | | - field :args, String, null: false do |f| |
71 | | - f.argument(:id, ID, required: false) do |a| |
72 | | - a.directive(VisibilityDirective, profiles: ["public"]) |
73 | | - end |
74 | | - f.argument(:enum, Toggle, required: false) do |a| |
75 | | - a.directive(VisibilityDirective, profiles: ["private"]) |
76 | | - end |
77 | | - end |
78 | | - |
79 | | - def args(id:, enum:) |
80 | | - id.to_s |
| 29 | + def sprocket(id:) |
| 30 | + RECORDS.find { _1[:id] == id } |
81 | 31 | end |
82 | 32 | end |
83 | 33 |
|
84 | 34 | query Query |
85 | 35 | end |
86 | 36 |
|
87 | | - class Bravo < GraphQL::Schema |
| 37 | + class InventorySchema < GraphQL::Schema |
88 | 38 | class Sprocket < GraphQL::Schema::Object |
89 | 39 | field :id, ID, null: false |
90 | | - field :color, String, null: false |
91 | | - field :weight, Integer, null: false |
92 | | - end |
| 40 | + |
| 41 | + field :quantity_available, Integer, null: false |
93 | 42 |
|
94 | | - class Widget < GraphQL::Schema::Object |
95 | | - directive(VisibilityDirective, profiles: ["private"]) |
96 | | - field :id, ID, null: false |
97 | | - end |
98 | | - |
99 | | - class Toggle < GraphQL::Schema::Enum |
100 | | - value "YES" |
101 | | - value "NO" |
102 | | - value "MAYBE" |
103 | | - value "ANY" |
| 43 | + field :quantity_in_stock, Integer, null: false do |f| |
| 44 | + f.directive(GraphQL::Stitching::Directives::Visibility, profiles: ["private"]) |
| 45 | + end |
104 | 46 | end |
105 | 47 |
|
106 | 48 | class Query < GraphQL::Schema::Object |
107 | | - field :sprocket_b, Sprocket, null: false do |f| |
108 | | - f.directive(StitchDirective, key: "id") |
109 | | - f.argument(:id, ID, required: true) |
110 | | - end |
111 | | - |
112 | | - def sprocket_b(id:) |
113 | | - { id: id, color: "red", weight: 3 } |
114 | | - end |
115 | | - |
116 | | - field :widget_b, Widget, null: false |
117 | | - |
118 | | - def widget_b |
119 | | - { id: 1 } |
120 | | - end |
121 | | - |
122 | | - field :args, String, null: false do |f| |
123 | | - f.argument(:id, ID, required: false) |
124 | | - f.argument(:enum, Toggle, required: false) |
| 49 | + field :sprockets, [Sprocket], null: false do |f| |
| 50 | + f.directive(GraphQL::Stitching::Directives::Stitch, key: "id") |
| 51 | + f.directive(GraphQL::Stitching::Directives::Visibility, profiles: ["private"]) |
| 52 | + f.argument(:ids, [ID, null: false], required: true) |
125 | 53 | end |
126 | 54 |
|
127 | | - def args(id:, enum:) |
128 | | - id.to_s |
| 55 | + def sprockets(ids:) |
| 56 | + ids.map { |id| RECORDS.find { _1[:id] == id } } |
129 | 57 | end |
130 | 58 | end |
131 | 59 |
|
|
0 commit comments