Skip to content

Commit 6d7ccf2

Browse files
arutkowski00utay
andauthored
feat: add support for @composeDirective (#3)
Co-authored-by: Yannick Utard <[email protected]>
1 parent 28d221f commit 6d7ccf2

File tree

5 files changed

+159
-192
lines changed

5 files changed

+159
-192
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: "Semantic PR Title"
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
types:
8+
- opened
9+
- edited
10+
- synchronize
11+
- reopened
12+
13+
permissions:
14+
pull-requests: write
15+
16+
jobs:
17+
main:
18+
name: Validate PR title
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: amannn/action-semantic-pull-request@v5
22+
id: lint_pr_title
23+
env:
24+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
25+
26+
- uses: marocchino/sticky-pull-request-comment@v2
27+
# When the previous steps fails, the workflow would stop. By adding this
28+
# condition you can continue the execution with the populated error message.
29+
if: always() && (steps.lint_pr_title.outputs.error_message != null)
30+
with:
31+
header: pr-title-lint-error
32+
message: |
33+
Hey there and thank you for opening this pull request! 👋🏼
34+
35+
We require pull request titles to follow the [Conventional Commits specification](https://www.conventionalcommits.org/en/v1.0.0/) and it looks like your proposed title needs to be adjusted.
36+
37+
Details:
38+
39+
```
40+
${{ steps.lint_pr_title.outputs.error_message }}
41+
```
42+
43+
# Delete a previous comment when the issue has been resolved
44+
- if: ${{ steps.lint_pr_title.outputs.error_message == null }}
45+
uses: marocchino/sticky-pull-request-comment@v2
46+
with:
47+
header: pr-title-lint-error
48+
delete: true

.github/workflows/semantic.yml

Lines changed: 0 additions & 116 deletions
This file was deleted.

lib/apollo-federation/federated_document_from_schema_definition.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ def merge_directives(node, type)
100100
end
101101

102102
def directive_name(directive)
103-
if schema.federation_2? && !Schema::IMPORTED_DIRECTIVES.include?(directive[:name])
104-
"#{schema.link_namespace}__#{directive[:name]}"
103+
if schema.federation_2? && schema.all_links.none? { |link| link[:import]&.include?(directive[:name]) }
104+
"#{schema.default_link_namespace}__#{directive[:name]}"
105105
else
106106
directive[:name]
107107
end

lib/apollo-federation/schema.rb

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

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

1212
def self.included(klass)
1313
klass.extend(CommonMethods)
@@ -16,9 +16,11 @@ def self.included(klass)
1616
module CommonMethods
1717
DEFAULT_LINK_NAMESPACE = 'federation'
1818

19-
def federation(version: '1.0', link: {})
19+
def federation(version: '1.0', default_link_namespace: nil, links: [], compose_directives: [])
2020
@federation_version = version
21-
@link = { as: DEFAULT_LINK_NAMESPACE }.merge(link)
21+
@default_link_namespace = default_link_namespace
22+
@links = links
23+
@compose_directives = compose_directives
2224
end
2325

2426
def federation_version
@@ -37,8 +39,8 @@ def federation_sdl(context: nil)
3739
output
3840
end
3941

40-
def link_namespace
41-
@link ? @link[:as] : find_inherited_value(:link_namespace)
42+
def default_link_namespace
43+
@default_link_namespace || find_inherited_value(:default_link_namespace, DEFAULT_LINK_NAMESPACE)
4244
end
4345

4446
def query(new_query_object = nil)
@@ -53,20 +55,53 @@ def query(new_query_object = nil)
5355
federation_query_object
5456
end
5557

58+
def compose_directives
59+
@compose_directives || find_inherited_value(:compose_directives, [])
60+
end
61+
62+
def links
63+
@links || find_inherited_value(:links, [])
64+
end
65+
66+
def all_links
67+
imported_directives = IMPORTED_DIRECTIVES
68+
default_link = {
69+
url: "https://specs.apollo.dev/federation/v#{federation_version}",
70+
import: imported_directives,
71+
}
72+
default_link[:as] = default_link_namespace if default_link_namespace != DEFAULT_LINK_NAMESPACE
73+
[default_link, *links]
74+
end
75+
5676
private
5777

5878
def original_query
5979
@orig_query_object || find_inherited_value(:original_query)
6080
end
6181

6282
def federation_2_prefix
63-
federation_namespace = ", as: \"#{link_namespace}\"" if link_namespace != DEFAULT_LINK_NAMESPACE
83+
schema = ['extend schema']
84+
85+
all_links.each do |link|
86+
link_str = ' @link('
87+
link_str += "url: \"#{link[:url]}\""
88+
link_str += ", as: \"#{link[:as]}\"" if link[:as]
89+
if link[:import]
90+
imported_directives = link[:import].map { |d| "\"@#{d}\"" }.join(', ')
91+
link_str += ", import: [#{imported_directives}]"
92+
end
93+
link_str += ')'
94+
schema << link_str
95+
end
96+
97+
compose_directives.each do |directive|
98+
schema << " @composeDirective(name: \"@#{directive}\")"
99+
end
64100

65-
<<~SCHEMA
66-
extend schema
67-
@link(url: "https://specs.apollo.dev/federation/v#{federation_version}"#{federation_namespace}, import: [#{(IMPORTED_DIRECTIVES.map { |directive| "\"@#{directive}\"" }).join(', ')}])
101+
schema << ''
102+
schema << ''
68103

69-
SCHEMA
104+
schema.join("\n")
70105
end
71106

72107
def schema_entities

0 commit comments

Comments
 (0)