Skip to content

Commit cd90a71

Browse files
authored
Merge pull request #2 from mondaycom/feature/add-policy
Add @Policy directive support
2 parents 51af77b + 5ab53ba commit cd90a71

File tree

13 files changed

+430
-112
lines changed

13 files changed

+430
-112
lines changed

.bundle/config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
BUNDLE_BUILD__JARO_WINKLER: "--with-cflags=-Wno-error=incompatible-function-pointer-types"
3+
BUNDLE_BUILD__DEBASE: "--with-cflags=-Wno-error=incompatible-function-pointer-types"

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ node_modules/
99
.circleci/processed-config.yml
1010

1111
.DS_Store
12+
13+
/.idea

.stignore

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
*.gem
2+
*.rbc
3+
.idea
4+
coverage
5+
InstalledFiles
6+
pkg
7+
spec/reports
8+
spec/examples.txt
9+
test/tmp
10+
test/version_tmp
11+
tmp
12+
13+
# Ignore Byebug command history file.
14+
.byebug_history
15+
16+
## Specific to RubyMotion:
17+
.dat*
18+
.repl_history
19+
build
20+
*.bridgesupport
21+
build-iPhoneOS
22+
build-iPhoneSimulator
23+
24+
## Specific to RubyMotion (use of CocoaPods):
25+
#
26+
# We recommend against adding the Pods directory to your .gitignore. However
27+
# you should judge for yourself, the pros and cons are mentioned at:
28+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29+
#
30+
# vendor/Pods/
31+
32+
## Documentation cache and generated files:
33+
.yardoc
34+
_yardoc
35+
36+
## Environment normalization:
37+
vendor/bundle
38+
lib/bundler/man
39+
40+
# for a library or gem, you might want to ignore these files since the code is
41+
# intended to run in multiple environments; otherwise, check them in:
42+
Gemfile.lock
43+
.ruby-version
44+
.ruby-gemset
45+
46+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
47+
.rvmrc

Gemfile.lock

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ GEM
4747
debase-ruby_core_source (3.2.0)
4848
diff-lcs (1.5.0)
4949
erubi (1.12.0)
50-
google-protobuf (3.22.2-arm64-darwin)
51-
google-protobuf (3.22.2-x86_64-darwin)
52-
google-protobuf (3.22.2-x86_64-linux)
50+
google-protobuf (3.22.2)
5351
graphql (2.0.19)
5452
i18n (1.12.0)
5553
concurrent-ruby (~> 1.0)
@@ -58,7 +56,11 @@ GEM
5856
crass (~> 1.0.2)
5957
nokogiri (>= 1.5.9)
6058
method_source (1.0.0)
59+
mini_portile2 (2.8.7)
6160
minitest (5.18.0)
61+
nokogiri (1.14.2)
62+
mini_portile2 (~> 2.8.0)
63+
racc (~> 1.4)
6264
nokogiri (1.14.2-arm64-darwin)
6365
racc (~> 1.4)
6466
nokogiri (1.14.2-x86_64-darwin)
@@ -119,6 +121,7 @@ GEM
119121
PLATFORMS
120122
arm64-darwin-21
121123
arm64-darwin-22
124+
ruby
122125
x86_64-darwin-20
123126
x86_64-darwin-21
124127
x86_64-darwin-22

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,20 @@ class User < BaseObject
302302
end
303303
```
304304

305+
### The `@policy` directive (Apollo Federation v2)
306+
307+
[Apollo documentation](https://www.apollographql.com/docs/federation/federated-types/federated-directives/#policy)
308+
309+
Call `policy` within your class definition or pass the `policy:` option to your field definition:
310+
311+
```ruby
312+
class Product < BaseObject
313+
policy policies: [["stock:read"]]
314+
field :id, ID, null: false
315+
field :inStock, Boolean, null: false, policy: { policies: [["stock:read"]] }
316+
end
317+
```
318+
305319
### Field set syntax
306320

307321
Field sets can be either strings encoded with the Apollo Field Set [syntax]((https://www.apollographql.com/docs/apollo-server/federation/federation-spec/#scalar-_fieldset)) or arrays, hashes and snake case symbols that follow the graphql-ruby conventions:

lib/apollo-federation/enum.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ def tag(name:)
1818
def inaccessible
1919
add_directive(name: 'inaccessible')
2020
end
21+
22+
def policy(policies)
23+
add_directive(
24+
name: "policy",
25+
arguments: [
26+
name: 'policies',
27+
values: policies,
28+
]
29+
)
30+
end
2131
end
2232
end
2333
end

lib/apollo-federation/field.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Field
88
include HasDirectives
99

1010
VERSION_1_DIRECTIVES = %i[external requires provides].freeze
11-
VERSION_2_DIRECTIVES = %i[shareable inaccessible override tags].freeze
11+
VERSION_2_DIRECTIVES = %i[shareable inaccessible override policy tags].freeze
1212

1313
def initialize(*args, **kwargs, &block)
1414
add_v1_directives(**kwargs)
@@ -59,7 +59,7 @@ def add_v1_directives(external: nil, requires: nil, provides: nil, **_kwargs)
5959
nil
6060
end
6161

62-
def add_v2_directives(shareable: nil, inaccessible: nil, override: nil, tags: [], **_kwargs)
62+
def add_v2_directives(shareable: nil, inaccessible: nil, override: nil, tags: [], policy: nil, **_kwargs)
6363
if shareable
6464
add_directive(name: 'shareable')
6565
end
@@ -78,6 +78,16 @@ def add_v2_directives(shareable: nil, inaccessible: nil, override: nil, tags: []
7878
)
7979
end
8080

81+
if policy
82+
add_directive(
83+
name: "policy",
84+
arguments: [
85+
name: 'policies',
86+
values: policy[:policies]
87+
]
88+
)
89+
end
90+
8191
tags.each do |tag|
8292
add_directive(
8393
name: 'tag',

lib/apollo-federation/interface.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ def tag(name:)
2626
add_directive(name: 'tag', arguments: [name: 'name', values: name])
2727
end
2828

29+
def policy(policies)
30+
add_directive(
31+
name: "policy",
32+
arguments: [
33+
name: 'policies',
34+
values: policies,
35+
]
36+
)
37+
end
38+
2939
def key(fields:, camelize: true)
3040
add_directive(
3141
name: 'key',

lib/apollo-federation/object.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ def tag(name:)
3232
add_directive(name: 'tag', arguments: [name: 'name', values: name])
3333
end
3434

35+
def policy(policies)
36+
add_directive(
37+
name: "policy",
38+
arguments: [
39+
name: 'policies',
40+
values: policies,
41+
]
42+
)
43+
end
44+
3545
def key(fields:, camelize: true, resolvable: true)
3646
arguments = [
3747
name: 'fields',

lib/apollo-federation/scalar.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ def tag(name:)
1818
def inaccessible
1919
add_directive(name: 'inaccessible')
2020
end
21+
22+
def policy(policies)
23+
add_directive(
24+
name: "policy",
25+
arguments: [
26+
name: 'policies',
27+
values: policies,
28+
]
29+
)
30+
end
2131
end
2232
end
2333
end

lib/apollo-federation/schema.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
module ApolloFederation
99
module Schema
10-
IMPORTED_DIRECTIVES = ['inaccessible', 'tag'].freeze
10+
IMPORTED_DIRECTIVES = ['inaccessible', 'policy', 'tag'].freeze
1111

1212
def self.included(klass)
1313
klass.extend(CommonMethods)
@@ -64,7 +64,7 @@ def federation_2_prefix
6464

6565
<<~SCHEMA
6666
extend schema
67-
@link(url: "https://specs.apollo.dev/federation/v2.3"#{federation_namespace}, import: [#{(IMPORTED_DIRECTIVES.map { |directive| "\"@#{directive}\"" }).join(', ')}])
67+
@link(url: "https://specs.apollo.dev/federation/v#{federation_version}"#{federation_namespace}, import: [#{(IMPORTED_DIRECTIVES.map { |directive| "\"@#{directive}\"" }).join(', ')}])
6868
6969
SCHEMA
7070
end

spec/apollo-federation/schema_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
expect(schema.federation_version).to eq('1.0')
1616
end
1717

18-
it 'returns the specified version when set to 2.0' do
18+
it 'returns the specified version when set to 2.6' do
1919
schema = Class.new(GraphQL::Schema) do
2020
include ApolloFederation::Schema
21-
federation version: '2.0'
21+
federation version: '2.6'
2222
end
2323

24-
expect(schema.federation_version).to eq('2.0')
24+
expect(schema.federation_version).to eq('2.6')
2525
end
2626

2727
it 'returns the specified version when set to 2.3' do

0 commit comments

Comments
 (0)