Skip to content
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
6 changes: 6 additions & 0 deletions .changeset/tricky-yaks-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-tools/utils': patch
---

Revert https://github.com/ardatan/graphql-tools/pull/7683 which can cause unexpected breaking
changes so as before the schema extension node will always be converted to a schema definition node
2 changes: 1 addition & 1 deletion packages/utils/src/print-schema-with-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export function astFromSchema(
}

const schemaNode: SchemaDefinitionNode | SchemaExtensionNode = {
kind: operationTypes.length ? Kind.SCHEMA_DEFINITION : Kind.SCHEMA_EXTENSION,
kind: operationTypes != null ? Kind.SCHEMA_DEFINITION : Kind.SCHEMA_EXTENSION,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: Condition always evaluates to true, making SCHEMA_EXTENSION unreachable.

The check operationTypes != null will always be true because operationTypes is defined on line 173 as an array (the result of filter(isSome)). Arrays are never null—they can only be empty. This means the code will always produce Kind.SCHEMA_DEFINITION, and the SCHEMA_EXTENSION branch is unreachable.

Apply this diff to fix the condition:

-    kind: operationTypes != null ? Kind.SCHEMA_DEFINITION : Kind.SCHEMA_EXTENSION,
+    kind: operationTypes.length > 0 ? Kind.SCHEMA_DEFINITION : Kind.SCHEMA_EXTENSION,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
kind: operationTypes != null ? Kind.SCHEMA_DEFINITION : Kind.SCHEMA_EXTENSION,
kind: operationTypes.length > 0 ? Kind.SCHEMA_DEFINITION : Kind.SCHEMA_EXTENSION,
🤖 Prompt for AI Agents
In packages/utils/src/print-schema-with-directives.ts around line 182, the
ternary uses `operationTypes != null` which is always true because
`operationTypes` is an array; change the condition to check the array length
(e.g., `operationTypes.length > 0`) so that Kind.SCHEMA_DEFINITION is chosen
only when there are operation types and Kind.SCHEMA_EXTENSION is used when the
array is empty.

operationTypes,
// ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility
directives: directives as any,
Expand Down
14 changes: 0 additions & 14 deletions packages/utils/tests/print-schema-with-directives.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
GraphQLString,
printSchema,
specifiedDirectives,
stripIgnoredCharacters,
} from 'graphql';
import { GraphQLJSON } from 'graphql-scalars';
import { makeExecutableSchema } from '@graphql-tools/schema';
Expand Down Expand Up @@ -403,17 +402,4 @@ describe('printSchemaWithDirectives', () => {
expect(output).toContain('input OneOfInput @oneOf');
}
});
it('prints a federation-style subgraph schema correctly', () => {
const sdl = stripIgnoredCharacters(/* GraphQL */ `
extend schema @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@key"])

type User @key(fields: "id") {
id: ID!
username: String
}
`);
const userSubgraph = buildSchema(sdl, { assumeValid: true, assumeValidSDL: true });
const printedSchema = stripIgnoredCharacters(printSchemaWithDirectives(userSubgraph));
expect(printedSchema).toBe(sdl);
});
});
Loading