Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not expose Scope and Policy scalars to supergraph #62

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/old-berries-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@theguild/federation-composition": patch
---

Fix: do not expose `federation__Scope` and `federation__Policy` scalar definitions to a supergraph
97 changes: 97 additions & 0 deletions __tests__/supergraph-composition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,4 +674,101 @@ testImplementations(api => {
}
`);
});

test('removes federation__Policy and federation__Scope scalars', () => {
const result = api.composeServices(
[
{
name: 'feed',
typeDefs: graphql`
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.6"
import: ["@requiresScopes", "@policy", "@key", "@shareable"]
)

scalar federation__Scope
scalar federation__Policy

directive @federation__requiresScopes(
scopes: [[federation__Scope!]!]!
) on FIELD_DEFINITION | OBJECT | INTERFACE | SCALAR | ENUM
directive @federation__policy(
policies: [[federation__Policy!]!]!
) on FIELD_DEFINITION | OBJECT | INTERFACE | SCALAR | ENUM

type Query {
feed: [Post!]!
@shareable
@requiresScopes(scopes: [["read:posts"]])
@policy(policies: [["read_posts"]])
}

type Post @key(fields: "id") {
id: ID!
title: String!
}
`,
},
{
name: 'comments',
typeDefs: graphql`
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.6"
import: [
"@authenticated"
"@requiresScopes"
"@policy"
"@key"
"@external"
"@shareable"
]
)

scalar federation__Scope
scalar federation__Policy

directive @federation__requiresScopes(
scopes: [[federation__Scope!]!]!
) on FIELD_DEFINITION | OBJECT | INTERFACE | SCALAR | ENUM
directive @federation__policy(
policies: [[federation__Policy!]!]!
) on FIELD_DEFINITION | OBJECT | INTERFACE | SCALAR | ENUM

extend type Post @key(fields: "id") {
id: ID! @external
comments: [Comment!]! @policy(policies: [["read_post_comments"]])
}

extend type Query {
feed: [Post!]!
@shareable
@policy(policies: [["read_post_comments"], ["read_post_comments"]])
@requiresScopes(scopes: [["read:posts"]])
}

type Mutation {
commentPost(input: CommentPostInput!): Comment! @authenticated
}

input CommentPostInput {
postId: ID!
comment: String!
}

type Comment {
id: ID!
body: String!
}
`,
},
],
);

assertCompositionSuccess(result);

expect(result.supergraphSdl).not.toMatch('federation__Policy');
expect(result.supergraphSdl).not.toMatch('federation__Scope');
});
});
13 changes: 11 additions & 2 deletions src/specifications/federation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ const federationSpecFactory = {
) on FIELD_DEFINITION | OBJECT | INTERFACE | SCALAR | ENUM
directive @authenticated on FIELD_DEFINITION | OBJECT | INTERFACE | SCALAR | ENUM
directive @requiresScopes(
scopes: [[Scope!]!]!
scopes: [[federation__Scope!]!]!
) on FIELD_DEFINITION | OBJECT | INTERFACE | SCALAR | ENUM
directive @composeDirective(name: String!) repeatable on SCHEMA
directive @extends on OBJECT | INTERFACE
Expand All @@ -247,7 +247,8 @@ const federationSpecFactory = {
name: String!
) repeatable on FIELD_DEFINITION | INTERFACE | OBJECT | UNION | ARGUMENT_DEFINITION | SCALAR | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION
scalar FieldSet
scalar Policy
scalar federation__Policy
scalar federation__Scope
`,
prefix,
imports,
Expand All @@ -274,6 +275,14 @@ function createTypeDefinitions(doc: string, prefix: string, imports?: readonly L
toInclude.add('federation__FieldSet');
}

if (toInclude.has('requiresScopes')) {
toInclude.add('federation__Scope');
}

if (toInclude.has('policy')) {
toInclude.add('federation__Policy');
}

const directives: DirectiveDefinitionNode[] = [];
const types: TypeDefinitionNode[] = [];

Expand Down
2 changes: 2 additions & 0 deletions src/subgraph/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,8 @@ function directiveFactory(state: SubgraphState) {
export function cleanSubgraphStateFromFederationSpec(state: SubgraphState): SubgraphState {
state.types.delete('_FieldSet');
state.types.delete('federation__FieldSet');
state.types.delete('federation__Policy');
state.types.delete('federation__Scope');

return state;
}
Expand Down
2 changes: 2 additions & 0 deletions src/subgraph/validation/validation-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ export function createSubgraphValidationContext(
}

setOfNames.add(alias ? alias.replace(/^@/, '') : specDirective.name.value);
setOfNames.add(`federation__${specDirective.name.value}`);
}
}

Expand Down Expand Up @@ -251,6 +252,7 @@ export function createSubgraphValidationContext(
}

setOfNames.add(alias ? alias : specType.name.value);
setOfNames.add(`federation__${specType.name.value}`);
}
}

Expand Down
Loading