-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(federation): federation v2.6 support (#1928)
### 📝 Description Adds Federation v2.6 support ```graphql directive @Policy(policies: [[federation__Policy!]!]!) on | FIELD_DEFINITION | OBJECT | INTERFACE | SCALAR | ENUM scalar federation__Policy ``` `@policy` directive indicates to composition that the target element is restricted based on authorization policies that are evaluated in a Rhai script or coprocessor. Refer to the [Apollo Router article](https://www.apollographql.com/docs/router/configuration/authorization#policy) for additional details. ### 🔗 Related Issues N/A
- Loading branch information
1 parent
0aea13e
commit 1172da1
Showing
22 changed files
with
387 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...ration/src/main/kotlin/com/expediagroup/graphql/generator/federation/directives/Policy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2024 Expedia, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.expediagroup.graphql.generator.federation.directives | ||
|
||
import com.expediagroup.graphql.generator.annotations.GraphQLIgnore | ||
|
||
/** | ||
* Annotation representing authorization policy scalar type that is used by the `@policy directive. | ||
* | ||
* @param value required authorization policy | ||
* @see [com.expediagroup.graphql.generator.federation.types.POLICY_SCALAR_TYPE] | ||
*/ | ||
@LinkedSpec(FEDERATION_SPEC) | ||
annotation class Policy(val value: String) | ||
|
||
// this is a workaround for JVM lack of support nested arrays as annotation values | ||
@GraphQLIgnore | ||
annotation class Policies(val value: Array<Policy>) |
111 changes: 111 additions & 0 deletions
111
...c/main/kotlin/com/expediagroup/graphql/generator/federation/directives/PolicyDirective.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright 2024 Expedia, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.expediagroup.graphql.generator.federation.directives | ||
|
||
import com.expediagroup.graphql.generator.annotations.GraphQLDirective | ||
import com.expediagroup.graphql.generator.directives.DirectiveMetaInformation | ||
import graphql.introspection.Introspection | ||
import graphql.schema.GraphQLAppliedDirective | ||
import graphql.schema.GraphQLArgument | ||
import graphql.schema.GraphQLList | ||
import graphql.schema.GraphQLNonNull | ||
import graphql.schema.GraphQLScalarType | ||
import kotlin.reflect.full.memberProperties | ||
|
||
/** | ||
* ```graphql | ||
* directive @policy(scopes: [[Policy!]!]!) on | ||
* ENUM | ||
* | FIELD_DEFINITION | ||
* | INTERFACE | ||
* | OBJECT | ||
* | SCALAR | ||
* ``` | ||
* | ||
* | ||
* Directive that is used to indicate that the target element is restricted based on authorization policies that are evaluated in a Rhai script or coprocessor. | ||
* Refer to the <a href="https://www.apollographql.com/docs/router/configuration/authorization#policy">Apollo Router article</a> for additional details. | ||
* | ||
* @see <a href="https://www.apollographql.com/docs/federation/federated-types/federated-directives#policy">@policy definition</a> | ||
* @see <a href="https://www.apollographql.com/docs/router/configuration/authorization#policy">Apollo Router @policy documentation</a> | ||
*/ | ||
@LinkedSpec(FEDERATION_SPEC) | ||
@Repeatable | ||
@GraphQLDirective( | ||
name = POLICY_DIRECTIVE_NAME, | ||
description = POLICY_DIRECTIVE_DESCRIPTION, | ||
locations = [ | ||
Introspection.DirectiveLocation.ENUM, | ||
Introspection.DirectiveLocation.FIELD_DEFINITION, | ||
Introspection.DirectiveLocation.INTERFACE, | ||
Introspection.DirectiveLocation.OBJECT, | ||
Introspection.DirectiveLocation.SCALAR, | ||
] | ||
) | ||
annotation class PolicyDirective(val policies: Array<Policies>) | ||
|
||
internal const val POLICY_DIRECTIVE_NAME = "policy" | ||
private const val POLICY_DIRECTIVE_DESCRIPTION = "Indicates to composition that the target element is restricted based on authorization policies that are evaluated in a Rhai script or coprocessor" | ||
private const val POLICIES_ARGUMENT = "policies" | ||
|
||
internal fun policyDirectiveDefinition(policies: GraphQLScalarType): graphql.schema.GraphQLDirective = graphql.schema.GraphQLDirective.newDirective() | ||
.name(POLICY_DIRECTIVE_NAME) | ||
.description(POLICY_DIRECTIVE_DESCRIPTION) | ||
.validLocations( | ||
Introspection.DirectiveLocation.ENUM, | ||
Introspection.DirectiveLocation.FIELD_DEFINITION, | ||
Introspection.DirectiveLocation.INTERFACE, | ||
Introspection.DirectiveLocation.OBJECT, | ||
Introspection.DirectiveLocation.SCALAR | ||
) | ||
.argument( | ||
GraphQLArgument.newArgument() | ||
.name(POLICIES_ARGUMENT) | ||
.type( | ||
GraphQLNonNull.nonNull( | ||
GraphQLList.list( | ||
GraphQLNonNull( | ||
GraphQLList.list( | ||
policies | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
.build() | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
internal fun graphql.schema.GraphQLDirective.toAppliedPolicyDirective(directiveInfo: DirectiveMetaInformation): GraphQLAppliedDirective { | ||
// we need to manually transform @policy directive definition as JVM does not support nested array as annotation arguments | ||
val annotationPolicies = directiveInfo.directive.annotationClass.memberProperties | ||
.first { it.name == POLICIES_ARGUMENT } | ||
.call(directiveInfo.directive) as? Array<Policies> ?: emptyArray() | ||
val policies = annotationPolicies.map { policiesAnnotation -> policiesAnnotation.value.toList() } | ||
|
||
return this.toAppliedDirective() | ||
.transform { appliedDirectiveBuilder -> | ||
this.getArgument(POLICIES_ARGUMENT) | ||
.toAppliedArgument() | ||
.transform { argumentBuilder -> | ||
argumentBuilder.valueProgrammatic(policies) | ||
} | ||
.let { | ||
appliedDirectiveBuilder.argument(it) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...-federation/src/main/kotlin/com/expediagroup/graphql/generator/federation/types/Policy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2024 Expedia, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.expediagroup.graphql.generator.federation.types | ||
|
||
import com.expediagroup.graphql.generator.federation.directives.Policy | ||
import com.expediagroup.graphql.generator.federation.exception.CoercingValueToLiteralException | ||
import graphql.GraphQLContext | ||
import graphql.Scalars | ||
import graphql.execution.CoercedVariables | ||
import graphql.language.StringValue | ||
import graphql.language.Value | ||
import graphql.schema.Coercing | ||
import graphql.schema.CoercingParseLiteralException | ||
import graphql.schema.CoercingSerializeException | ||
import graphql.schema.GraphQLScalarType | ||
import java.util.Locale | ||
|
||
internal const val POLICY_SCALAR_NAME = "Policy" | ||
|
||
/** | ||
* Custom scalar type that is used to represent authentication policy which serializes as a String. | ||
*/ | ||
internal val POLICY_SCALAR_TYPE: GraphQLScalarType = GraphQLScalarType.newScalar(Scalars.GraphQLString) | ||
.name(POLICY_SCALAR_NAME) | ||
.description("Federation type representing authorization policy") | ||
.coercing(PolicyCoercing) | ||
.build() | ||
|
||
private object PolicyCoercing : Coercing<Policy, String> { | ||
override fun serialize(dataFetcherResult: Any, graphQLContext: GraphQLContext, locale: Locale): String = | ||
when (dataFetcherResult) { | ||
is Policy -> dataFetcherResult.value | ||
else -> throw CoercingSerializeException( | ||
"Cannot serialize $dataFetcherResult. Expected type 'Policy' but was '${dataFetcherResult.javaClass.simpleName}'." | ||
) | ||
} | ||
|
||
override fun parseValue(input: Any, graphQLContext: GraphQLContext, locale: Locale): Policy = | ||
when (input) { | ||
is Policy -> input | ||
is StringValue -> Policy::class.constructors.first().call(input.value) | ||
else -> throw CoercingParseLiteralException( | ||
"Cannot parse $input to Policy. Expected AST type 'StringValue' but was '${input.javaClass.simpleName}'." | ||
) | ||
} | ||
|
||
override fun parseLiteral(input: Value<*>, variables: CoercedVariables, graphQLContext: GraphQLContext, locale: Locale): Policy = | ||
when (input) { | ||
is StringValue -> Policy::class.constructors.first().call(input.value) | ||
else -> throw CoercingParseLiteralException( | ||
"Cannot parse $input to Policy. Expected AST type 'StringValue' but was '${input.javaClass.simpleName}'." | ||
) | ||
} | ||
|
||
override fun valueToLiteral(input: Any, graphQLContext: GraphQLContext, locale: Locale): Value<*> = | ||
when (input) { | ||
is Policy -> StringValue.newStringValue(input.value).build() | ||
else -> throw CoercingValueToLiteralException(Policy::class, input) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ class ContactDirectiveTest { | |
fun `verify we can import federation spec using custom @link`() { | ||
val expectedSchema = | ||
""" | ||
schema @contact(description : "Send emails to [email protected]", name : "My Team Name", url : "https://myteam.slack.com/room") @link(url : "https://specs.apollo.dev/federation/v2.5"){ | ||
schema @contact(description : "Send emails to [email protected]", name : "My Team Name", url : "https://myteam.slack.com/room") @link(url : "https://specs.apollo.dev/federation/v2.6"){ | ||
query: Query | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.