Skip to content

Commit

Permalink
feat: add support for @composeDirective (#3)
Browse files Browse the repository at this point in the history
Co-authored-by: Yannick Utard <yannickutard@gmail.com>
  • Loading branch information
arutkowski00 and utay authored Nov 21, 2024
1 parent 28d221f commit 6d7ccf2
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 192 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/semantic-pr-title.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: "Semantic PR Title"

on:
pull_request:
branches:
- main
types:
- opened
- edited
- synchronize
- reopened

permissions:
pull-requests: write

jobs:
main:
name: Validate PR title
runs-on: ubuntu-latest
steps:
- uses: amannn/action-semantic-pull-request@v5
id: lint_pr_title
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- uses: marocchino/sticky-pull-request-comment@v2
# When the previous steps fails, the workflow would stop. By adding this
# condition you can continue the execution with the populated error message.
if: always() && (steps.lint_pr_title.outputs.error_message != null)
with:
header: pr-title-lint-error
message: |
Hey there and thank you for opening this pull request! 👋🏼
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.
Details:
```
${{ steps.lint_pr_title.outputs.error_message }}
```
# Delete a previous comment when the issue has been resolved
- if: ${{ steps.lint_pr_title.outputs.error_message == null }}
uses: marocchino/sticky-pull-request-comment@v2
with:
header: pr-title-lint-error
delete: true
116 changes: 0 additions & 116 deletions .github/workflows/semantic.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ def merge_directives(node, type)
end

def directive_name(directive)
if schema.federation_2? && !Schema::IMPORTED_DIRECTIVES.include?(directive[:name])
"#{schema.link_namespace}__#{directive[:name]}"
if schema.federation_2? && schema.all_links.none? { |link| link[:import]&.include?(directive[:name]) }
"#{schema.default_link_namespace}__#{directive[:name]}"
else
directive[:name]
end
Expand Down
55 changes: 45 additions & 10 deletions lib/apollo-federation/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

module ApolloFederation
module Schema
IMPORTED_DIRECTIVES = ['inaccessible', 'policy', 'tag'].freeze
IMPORTED_DIRECTIVES = ['composeDirective', 'inaccessible', 'policy', 'tag'].freeze

def self.included(klass)
klass.extend(CommonMethods)
Expand All @@ -16,9 +16,11 @@ def self.included(klass)
module CommonMethods
DEFAULT_LINK_NAMESPACE = 'federation'

def federation(version: '1.0', link: {})
def federation(version: '1.0', default_link_namespace: nil, links: [], compose_directives: [])
@federation_version = version
@link = { as: DEFAULT_LINK_NAMESPACE }.merge(link)
@default_link_namespace = default_link_namespace
@links = links
@compose_directives = compose_directives
end

def federation_version
Expand All @@ -37,8 +39,8 @@ def federation_sdl(context: nil)
output
end

def link_namespace
@link ? @link[:as] : find_inherited_value(:link_namespace)
def default_link_namespace
@default_link_namespace || find_inherited_value(:default_link_namespace, DEFAULT_LINK_NAMESPACE)
end

def query(new_query_object = nil)
Expand All @@ -53,20 +55,53 @@ def query(new_query_object = nil)
federation_query_object
end

def compose_directives
@compose_directives || find_inherited_value(:compose_directives, [])
end

def links
@links || find_inherited_value(:links, [])
end

def all_links
imported_directives = IMPORTED_DIRECTIVES
default_link = {
url: "https://specs.apollo.dev/federation/v#{federation_version}",
import: imported_directives,
}
default_link[:as] = default_link_namespace if default_link_namespace != DEFAULT_LINK_NAMESPACE
[default_link, *links]
end

private

def original_query
@orig_query_object || find_inherited_value(:original_query)
end

def federation_2_prefix
federation_namespace = ", as: \"#{link_namespace}\"" if link_namespace != DEFAULT_LINK_NAMESPACE
schema = ['extend schema']

all_links.each do |link|
link_str = ' @link('
link_str += "url: \"#{link[:url]}\""
link_str += ", as: \"#{link[:as]}\"" if link[:as]
if link[:import]
imported_directives = link[:import].map { |d| "\"@#{d}\"" }.join(', ')
link_str += ", import: [#{imported_directives}]"
end
link_str += ')'
schema << link_str
end

compose_directives.each do |directive|
schema << " @composeDirective(name: \"@#{directive}\")"
end

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

SCHEMA
schema.join("\n")
end

def schema_entities
Expand Down
Loading

0 comments on commit 6d7ccf2

Please sign in to comment.