From 3d6ca58499b00d11acef97edb5330697b390cfaa Mon Sep 17 00:00:00 2001 From: Laurin Quast Date: Fri, 26 Jan 2024 12:31:01 +0100 Subject: [PATCH] relax rules for inaccessible --- .changeset/bright-pants-accept.md | 5 ++ .../errors/REQUIRED_INACCESSIBLE.spec.ts | 49 +++++++++++++++++-- src/compose.ts | 2 + ...ument-or-field-is-not-inaccessible-rule.ts | 8 ++- 4 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 .changeset/bright-pants-accept.md diff --git a/.changeset/bright-pants-accept.md b/.changeset/bright-pants-accept.md new file mode 100644 index 0000000..e816d4c --- /dev/null +++ b/.changeset/bright-pants-accept.md @@ -0,0 +1,5 @@ +--- +'@theguild/federation-composition': patch +--- + +Fix REQUIRED_INACCESSIBLE occurring on inaccessible fields/input types diff --git a/__tests__/supergraph/errors/REQUIRED_INACCESSIBLE.spec.ts b/__tests__/supergraph/errors/REQUIRED_INACCESSIBLE.spec.ts index 9f61555..efa3cec 100644 --- a/__tests__/supergraph/errors/REQUIRED_INACCESSIBLE.spec.ts +++ b/__tests__/supergraph/errors/REQUIRED_INACCESSIBLE.spec.ts @@ -13,7 +13,6 @@ testVersions((api, version) => { url: "https://specs.apollo.dev/federation/${version}" import: ["@key", "@inaccessible"] ) - type Query { a(id: ID! @inaccessible): Int! } @@ -33,7 +32,6 @@ testVersions((api, version) => { ]), }), ); - expect( api.composeServices([ { @@ -44,12 +42,11 @@ testVersions((api, version) => { url: "https://specs.apollo.dev/federation/${version}" import: ["@key", "@inaccessible"] ) - + input A { id: ID! @inaccessible b: Int } - type Query { a(a: A): Int! } @@ -69,5 +66,49 @@ testVersions((api, version) => { ]), }), ); + + expect( + api.composeServices([ + { + name: 'users', + typeDefs: graphql` + extend schema + @link( + url: "https://specs.apollo.dev/federation/${version}" + import: ["@inaccessible"] + ) + + type Query { + a(id: ID! @inaccessible): Int!@inaccessible + b: Int! + } + `, + }, + ])?.errors, + ).toBeUndefined(); + + expect( + api.composeServices([ + { + name: 'users', + typeDefs: graphql` + extend schema + @link( + url: "https://specs.apollo.dev/federation/${version}" + import: ["@inaccessible"] + ) + + input A @inaccessible { + a: Int! @inaccessible + } + + type Query { + a(id: A! @inaccessible): Int! @inaccessible + b: Int! + } + `, + }, + ])?.errors, + ).toBeUndefined(); }); }); diff --git a/src/compose.ts b/src/compose.ts index bb00e5e..293832f 100644 --- a/src/compose.ts +++ b/src/compose.ts @@ -107,11 +107,13 @@ ${print({ export type CompositionResult = CompositionFailure | CompositionSuccess; export interface CompositionFailure { + supergraphSdl?: undefined; errors: GraphQLError[]; } export interface CompositionSuccess { supergraphSdl: string; + errors?: undefined; } export function assertCompositionSuccess( diff --git a/src/supergraph/validation/rules/required-argument-or-field-is-not-inaccessible-rule.ts b/src/supergraph/validation/rules/required-argument-or-field-is-not-inaccessible-rule.ts index 91588e8..bb42ac1 100644 --- a/src/supergraph/validation/rules/required-argument-or-field-is-not-inaccessible-rule.ts +++ b/src/supergraph/validation/rules/required-argument-or-field-is-not-inaccessible-rule.ts @@ -7,7 +7,11 @@ export function RequiredArgumentOrFieldIsNotInaccessibleRule( ): SupergraphVisitorMap { return { InputObjectTypeField(inputObjectState, fieldState) { - if (fieldState.type.endsWith('!') && fieldState.inaccessible) { + if ( + !inputObjectState.inaccessible && + fieldState.inaccessible && + fieldState.type.endsWith('!') + ) { context.reportError( new GraphQLError( `Input field "${inputObjectState.name}.${fieldState.name}" is @inaccessible but is a required input field of its type.`, @@ -21,7 +25,7 @@ export function RequiredArgumentOrFieldIsNotInaccessibleRule( } }, ObjectTypeFieldArg(objectState, fieldState, argState) { - if (argState.type.endsWith('!') && argState.inaccessible) { + if (!fieldState.inaccessible && argState.inaccessible && argState.type.endsWith('!')) { context.reportError( new GraphQLError( `Argument "${objectState.name}.${fieldState.name}(${argState.name}:)" is @inaccessible but is a required argument of its field.`,