From 3c96c110f3601153b2f97d5d567f5fefbba77b52 Mon Sep 17 00:00:00 2001 From: Martin Bonnin Date: Thu, 6 Mar 2025 18:37:04 +0100 Subject: [PATCH 1/2] add suport for @disableErrorPropagation --- .../kotlin/com/apollographql/apollo/ast/Schema.kt | 2 ++ .../com/apollographql/apollo/ast/gqldocument.kt | 1 + .../apollo/ast/internal/SchemaValidationScope.kt | 3 ++- .../apollo/ast/internal/definitions.kt | 6 ++++++ .../apollo/compiler/ApolloCompiler.kt | 14 ++++++++++++-- .../catch/src/main/graphql/shared/schema.graphqls | 4 +++- 6 files changed, 26 insertions(+), 4 deletions(-) diff --git a/libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/Schema.kt b/libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/Schema.kt index 2c94d17ef30..bd509bffee7 100644 --- a/libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/Schema.kt +++ b/libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/Schema.kt @@ -203,6 +203,8 @@ class Schema internal constructor( const val ONE_OF = "oneOf" @ApolloExperimental const val DEFER = "defer" + @ApolloExperimental + const val DISABLE_ERROR_PROPAGATION = "experimental_disableErrorPropagation" @ApolloExperimental const val LINK = "link" diff --git a/libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/gqldocument.kt b/libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/gqldocument.kt index 4340ef2a4e8..708aa07229a 100644 --- a/libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/gqldocument.kt +++ b/libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/gqldocument.kt @@ -6,6 +6,7 @@ import com.apollographql.apollo.annotations.ApolloInternal import com.apollographql.apollo.ast.internal.ExtensionsMerger import com.apollographql.apollo.ast.internal.builtinsDefinitionsStr import com.apollographql.apollo.ast.internal.compilerOptions_0_0 +import com.apollographql.apollo.ast.internal.disableErrorPropagationStr import com.apollographql.apollo.ast.internal.kotlinLabsDefinitions_0_3 import com.apollographql.apollo.ast.internal.kotlinLabsDefinitions_0_4 import com.apollographql.apollo.ast.internal.kotlinLabsDefinitions_0_5 diff --git a/libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/internal/SchemaValidationScope.kt b/libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/internal/SchemaValidationScope.kt index 1d6fd4f6322..27ac0cac8fa 100644 --- a/libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/internal/SchemaValidationScope.kt +++ b/libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/internal/SchemaValidationScope.kt @@ -261,7 +261,8 @@ internal fun validateSchema(definitions: List, options: SchemaVal deferDefinitionsStr, nonNullDefinitionStr, kotlinLabsDefinitions_0_5, - nullabilityDefinitionsStr + nullabilityDefinitionsStr, + disableErrorPropagationStr ).flatMap { it.parseAsGQLDocument().getOrThrow().definitions } diff --git a/libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/internal/definitions.kt b/libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/internal/definitions.kt index c1483689e8a..911d3f39c2a 100644 --- a/libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/internal/definitions.kt +++ b/libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/internal/definitions.kt @@ -1,5 +1,7 @@ package com.apollographql.apollo.ast.internal +import com.apollographql.apollo.annotations.ApolloInternal + /** * This file contains several groups of GraphQL definitions we use during codegen: * @@ -544,4 +546,8 @@ directive @defer( internal val nonNullDefinitionStr = """ directive @nonnull(fields: String! = "") on OBJECT | FIELD +""".trimIndent() + +internal val disableErrorPropagationStr = """ +directive @experimental_disableErrorPropagation on QUERY | MUTATION | SUBSCRIPTION """.trimIndent() \ No newline at end of file diff --git a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompiler.kt b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompiler.kt index 11e1cec978e..e0a54d54a5e 100644 --- a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompiler.kt +++ b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompiler.kt @@ -5,6 +5,7 @@ import com.apollographql.apollo.ast.DifferentShape import com.apollographql.apollo.ast.DirectiveRedefinition import com.apollographql.apollo.ast.ForeignSchema import com.apollographql.apollo.ast.GQLDefinition +import com.apollographql.apollo.ast.GQLDirective import com.apollographql.apollo.ast.GQLDocument import com.apollographql.apollo.ast.GQLFragmentDefinition import com.apollographql.apollo.ast.GQLOperationDefinition @@ -14,6 +15,7 @@ import com.apollographql.apollo.ast.IncompatibleDefinition import com.apollographql.apollo.ast.Issue import com.apollographql.apollo.ast.ParserOptions import com.apollographql.apollo.ast.QueryDocumentMinifier +import com.apollographql.apollo.ast.Schema import com.apollographql.apollo.ast.UnknownDirective import com.apollographql.apollo.ast.UnusedFragment import com.apollographql.apollo.ast.UnusedVariable @@ -269,10 +271,18 @@ object ApolloCompiler { } } + val operations = definitions.filterIsInstance().map { - addRequiredFields(it, addTypename, schema, fragmentDefinitions).let { - documentTransform?.transform(schema, it) ?: it + var operation = addRequiredFields(it, addTypename, schema, fragmentDefinitions) + if (documentTransform != null) { + operation = documentTransform.transform(schema, it) + } + if (schema.directiveDefinitions.containsKey(Schema.DISABLE_ERROR_PROPAGATION)) { + operation = operation.copy( + directives = operation.directives + GQLDirective(null, Schema.DISABLE_ERROR_PROPAGATION, emptyList()) + ) } + operation } // Remember the fragments with the possibly updated fragments diff --git a/tests/catch/src/main/graphql/shared/schema.graphqls b/tests/catch/src/main/graphql/shared/schema.graphqls index 2f1e80ecf95..65646be9626 100644 --- a/tests/catch/src/main/graphql/shared/schema.graphqls +++ b/tests/catch/src/main/graphql/shared/schema.graphqls @@ -20,4 +20,6 @@ type User { type Product { price: String -} \ No newline at end of file +} + +directive @experimental_disableErrorPropagation on QUERY | MUTATION | SUBSCRIPTION \ No newline at end of file From 08eaeb6e54dc1d2f4f07a2fc5721d66297f87e3e Mon Sep 17 00:00:00 2001 From: Martin Bonnin Date: Fri, 7 Mar 2025 13:52:51 +0100 Subject: [PATCH 2/2] Condition enabling @disableErrorPropagation on @catchByDefault --- .../kotlin/com/apollographql/apollo/compiler/ApolloCompiler.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompiler.kt b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompiler.kt index e0a54d54a5e..97f371e4b65 100644 --- a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompiler.kt +++ b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompiler.kt @@ -277,7 +277,8 @@ object ApolloCompiler { if (documentTransform != null) { operation = documentTransform.transform(schema, it) } - if (schema.directiveDefinitions.containsKey(Schema.DISABLE_ERROR_PROPAGATION)) { + if (schema.directiveDefinitions.containsKey(Schema.DISABLE_ERROR_PROPAGATION) + && schema.schemaDefinition?.directives?.any { schema.originalDirectiveName(it.name) == Schema.CATCH_BY_DEFAULT } == true) { operation = operation.copy( directives = operation.directives + GQLDirective(null, Schema.DISABLE_ERROR_PROPAGATION, emptyList()) )