From ae100bc5328fd4c46305ed92dd8fe46d609dba08 Mon Sep 17 00:00:00 2001 From: Mark Probst Date: Mon, 20 Jul 2026 18:30:12 -0400 Subject: [PATCH] fix(graphql): keep distinct GraphQL types with identical shape separate (#1072) Distinct GraphQL object types (e.g. GridConfig and GridProps) that happen to share the same fields were unified into a single output class, since quicktype's class deduplication only compared structural shape. This is correct for JSON/JSON Schema input, but GraphQL input should always keep schema-distinct types distinct since they are known-unrelated by the schema itself, regardless of --combine-classes (which only governs combining structurally-similar-but-not-identical JSON-derived classes). Tag each class built from GraphQL input with an identity type attribute carrying its GraphQL schema type name, and make both class type identity (TypeBuilder.getClassType) and the CombineClasses rewrite respect it, so two GraphQL classes are only unified when they come from the same named GraphQL type. Repeated uses of the same GraphQL type still dedupe as before. Added a GraphQL fixture (query + introspection schema) with two structurally-identical sibling types, and a focused unit test asserting the generated TypeScript keeps them as separate interfaces with both combineClasses on and off. Co-Authored-By: gpt-5.6-sol via pi --- packages/quicktype-core/src/Type/Type.ts | 7 + .../src/rewrites/CombineClasses.ts | 11 +- packages/quicktype-graphql-input/src/index.ts | 22 +- test/inputs/graphql/separate-types.gqlschema | 1233 +++++++++++++++++ test/inputs/graphql/separate-types1.1.json | 16 + test/inputs/graphql/separate-types1.graphql | 14 + test/unit/graphql-separate-types.test.ts | 45 + 7 files changed, 1344 insertions(+), 4 deletions(-) create mode 100644 test/inputs/graphql/separate-types.gqlschema create mode 100644 test/inputs/graphql/separate-types1.1.json create mode 100644 test/inputs/graphql/separate-types1.graphql create mode 100644 test/unit/graphql-separate-types.test.ts diff --git a/packages/quicktype-core/src/Type/Type.ts b/packages/quicktype-core/src/Type/Type.ts index f888f4033f..7af7a93c57 100644 --- a/packages/quicktype-core/src/Type/Type.ts +++ b/packages/quicktype-core/src/Type/Type.ts @@ -266,6 +266,13 @@ function identityAttributes(attributes: TypeAttributes): TypeAttributes { return mapFilter(attributes, (_, kind) => kind.inIdentity); } +export function haveSameIdentityAttributes(a: Type, b: Type): boolean { + return areEqual( + identityAttributes(a.getAttributes()), + identityAttributes(b.getAttributes()), + ); +} + export function primitiveTypeIdentity( kind: PrimitiveTypeKind, attributes: TypeAttributes, diff --git a/packages/quicktype-core/src/rewrites/CombineClasses.ts b/packages/quicktype-core/src/rewrites/CombineClasses.ts index df8540a2e0..62516f001a 100644 --- a/packages/quicktype-core/src/rewrites/CombineClasses.ts +++ b/packages/quicktype-core/src/rewrites/CombineClasses.ts @@ -5,6 +5,7 @@ import { type ClassProperty, ClassType, type Type, + haveSameIdentityAttributes, setOperationCasesEqual, } from "../Type/Type.js"; import type { TypeGraph } from "../Type/TypeGraph.js"; @@ -110,14 +111,20 @@ function tryAddToClique( onlyWithSameProperties: boolean, ): boolean { for (const prototype of clique.prototypes) { - if (prototype.structurallyCompatible(c)) { + if ( + haveSameIdentityAttributes(prototype, c) && + prototype.structurallyCompatible(c) + ) { clique.members.push(c); return true; } } for (const prototype of clique.prototypes) { - if (canBeCombined(prototype, c, onlyWithSameProperties)) { + if ( + haveSameIdentityAttributes(prototype, c) && + canBeCombined(prototype, c, onlyWithSameProperties) + ) { clique.prototypes.push(c); clique.members.push(c); return true; diff --git a/packages/quicktype-graphql-input/src/index.ts b/packages/quicktype-graphql-input/src/index.ts index 2ac6fcbad8..35fbe61f42 100644 --- a/packages/quicktype-graphql-input/src/index.ts +++ b/packages/quicktype-graphql-input/src/index.ts @@ -15,6 +15,7 @@ import { type Input, type RunContext, StringTypes, + TypeAttributeKind, type TypeAttributes, type TypeBuilder, TypeNames, @@ -77,6 +78,22 @@ function getField(t: GQLType, name: string): Field { return panic(`Required field ${name} not defined on type ${t.name}.`); } +class GraphQLTypeNameTypeAttributeKind extends TypeAttributeKind { + public constructor() { + super("graphqlTypeName"); + } + + public get inIdentity(): boolean { + return true; + } + + public combine(names: string[]): string { + return names[0]; + } +} + +const graphQLTypeNameTypeAttributeKind = new GraphQLTypeNameTypeAttributeKind(); + function makeNames( name: string, fieldName: string | null, @@ -422,10 +439,11 @@ class GQLQuery { } } - return builder.getClassType( + const attributes = new Map( makeNames(nameOrOverride, containingFieldName, containingTypeName), - properties, ); + attributes.set(graphQLTypeNameTypeAttributeKind, gqlType.name); + return builder.getClassType(attributes, properties); }; public makeType( diff --git a/test/inputs/graphql/separate-types.gqlschema b/test/inputs/graphql/separate-types.gqlschema new file mode 100644 index 0000000000..4fee44be66 --- /dev/null +++ b/test/inputs/graphql/separate-types.gqlschema @@ -0,0 +1,1233 @@ +{ + "data": { + "__schema": { + "queryType": { + "name": "Query", + "kind": "OBJECT" + }, + "mutationType": null, + "subscriptionType": null, + "types": [ + { + "kind": "OBJECT", + "name": "Query", + "description": null, + "fields": [ + { + "name": "grid", + "description": null, + "args": [], + "type": { + "kind": "OBJECT", + "name": "Grid", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "Grid", + "description": null, + "fields": [ + { + "name": "config", + "description": null, + "args": [], + "type": { + "kind": "OBJECT", + "name": "GridConfig", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "props", + "description": null, + "args": [], + "type": { + "kind": "OBJECT", + "name": "GridProps", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "GridConfig", + "description": null, + "fields": [ + { + "name": "items", + "description": null, + "args": [], + "type": { + "kind": "LIST", + "name": null, + "ofType": { + "name": "ComponentRef", + "kind": "OBJECT", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "GridProps", + "description": null, + "fields": [ + { + "name": "items", + "description": null, + "args": [], + "type": { + "kind": "LIST", + "name": null, + "ofType": { + "name": "ComponentRef", + "kind": "OBJECT", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "ComponentRef", + "description": null, + "fields": [ + { + "name": "id", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "SCALAR", + "name": "String", + "description": "The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.", + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "SCALAR", + "name": "Boolean", + "description": "The `Boolean` scalar type represents `true` or `false`.", + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "__Schema", + "description": "A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations.", + "fields": [ + { + "name": "description", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "types", + "description": "A list of all types supported by this server.", + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": null, + "kind": "LIST", + "ofType": { + "name": null, + "kind": "NON_NULL", + "ofType": { + "name": "__Type", + "kind": "OBJECT", + "ofType": null + } + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "queryType", + "description": "The type that query operations will be rooted at.", + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": "__Type", + "kind": "OBJECT", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "mutationType", + "description": "If this server supports mutation, the type that mutation operations will be rooted at.", + "args": [], + "type": { + "kind": "OBJECT", + "name": "__Type", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "subscriptionType", + "description": "If this server support subscription, the type that subscription operations will be rooted at.", + "args": [], + "type": { + "kind": "OBJECT", + "name": "__Type", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "directives", + "description": "A list of all directives supported by this server.", + "args": [ + { + "name": "includeDeprecated", + "description": null, + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": "Boolean", + "kind": "SCALAR", + "ofType": null + } + }, + "defaultValue": "false" + } + ], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": null, + "kind": "LIST", + "ofType": { + "name": null, + "kind": "NON_NULL", + "ofType": { + "name": "__Directive", + "kind": "OBJECT", + "ofType": null + } + } + } + }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "__Type", + "description": "The fundamental unit of any GraphQL Schema is the type. There are many kinds of types in GraphQL as represented by the `__TypeKind` enum.\n\nDepending on the kind of a type, certain fields describe information about that type. Scalar types provide no information beyond a name, description and optional `specifiedByURL`, while Enum types provide their values. Object and Interface types provide the fields they describe. Abstract types, Union and Interface, provide the Object types possible at runtime. List and NonNull types compose other types.", + "fields": [ + { + "name": "kind", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": "__TypeKind", + "kind": "ENUM", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "name", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "description", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "specifiedByURL", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "fields", + "description": null, + "args": [ + { + "name": "includeDeprecated", + "description": null, + "type": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null + }, + "defaultValue": "false" + } + ], + "type": { + "kind": "LIST", + "name": null, + "ofType": { + "name": null, + "kind": "NON_NULL", + "ofType": { + "name": "__Field", + "kind": "OBJECT", + "ofType": null + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "interfaces", + "description": null, + "args": [], + "type": { + "kind": "LIST", + "name": null, + "ofType": { + "name": null, + "kind": "NON_NULL", + "ofType": { + "name": "__Type", + "kind": "OBJECT", + "ofType": null + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "possibleTypes", + "description": null, + "args": [], + "type": { + "kind": "LIST", + "name": null, + "ofType": { + "name": null, + "kind": "NON_NULL", + "ofType": { + "name": "__Type", + "kind": "OBJECT", + "ofType": null + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "enumValues", + "description": null, + "args": [ + { + "name": "includeDeprecated", + "description": null, + "type": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null + }, + "defaultValue": "false" + } + ], + "type": { + "kind": "LIST", + "name": null, + "ofType": { + "name": null, + "kind": "NON_NULL", + "ofType": { + "name": "__EnumValue", + "kind": "OBJECT", + "ofType": null + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "inputFields", + "description": null, + "args": [ + { + "name": "includeDeprecated", + "description": null, + "type": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null + }, + "defaultValue": "false" + } + ], + "type": { + "kind": "LIST", + "name": null, + "ofType": { + "name": null, + "kind": "NON_NULL", + "ofType": { + "name": "__InputValue", + "kind": "OBJECT", + "ofType": null + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "ofType", + "description": null, + "args": [], + "type": { + "kind": "OBJECT", + "name": "__Type", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "isOneOf", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "ENUM", + "name": "__TypeKind", + "description": "An enum describing what kind of type a given `__Type` is.", + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": [ + { + "name": "SCALAR", + "description": "Indicates this type is a scalar.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "OBJECT", + "description": "Indicates this type is an object. `fields` and `interfaces` are valid fields.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "INTERFACE", + "description": "Indicates this type is an interface. `fields`, `interfaces`, and `possibleTypes` are valid fields.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "UNION", + "description": "Indicates this type is a union. `possibleTypes` is a valid field.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "ENUM", + "description": "Indicates this type is an enum. `enumValues` is a valid field.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "INPUT_OBJECT", + "description": "Indicates this type is an input object. `inputFields` is a valid field.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "LIST", + "description": "Indicates this type is a list. `ofType` is a valid field.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "NON_NULL", + "description": "Indicates this type is a non-null. `ofType` is a valid field.", + "isDeprecated": false, + "deprecationReason": null + } + ], + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "__Field", + "description": "Object and Interface types are described by a list of Fields, each of which has a name, potentially a list of arguments, and a return type.", + "fields": [ + { + "name": "name", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": "String", + "kind": "SCALAR", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "description", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "args", + "description": null, + "args": [ + { + "name": "includeDeprecated", + "description": null, + "type": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null + }, + "defaultValue": "false" + } + ], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": null, + "kind": "LIST", + "ofType": { + "name": null, + "kind": "NON_NULL", + "ofType": { + "name": "__InputValue", + "kind": "OBJECT", + "ofType": null + } + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "type", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": "__Type", + "kind": "OBJECT", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "isDeprecated", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": "Boolean", + "kind": "SCALAR", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "deprecationReason", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "__InputValue", + "description": "Arguments provided to Fields or Directives and the input fields of an InputObject are represented as Input Values which describe their type and optionally a default value.", + "fields": [ + { + "name": "name", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": "String", + "kind": "SCALAR", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "description", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "type", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": "__Type", + "kind": "OBJECT", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "defaultValue", + "description": "A GraphQL-formatted string representing the default value for this input value.", + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "isDeprecated", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": "Boolean", + "kind": "SCALAR", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "deprecationReason", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "__EnumValue", + "description": "One possible value for a given Enum. Enum values are unique values, not a placeholder for a string or numeric value. However an Enum value is returned in a JSON response as a string.", + "fields": [ + { + "name": "name", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": "String", + "kind": "SCALAR", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "description", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "isDeprecated", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": "Boolean", + "kind": "SCALAR", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "deprecationReason", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "__Directive", + "description": "A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document.\n\nIn some cases, you need to provide options to alter GraphQL's execution behavior in ways field arguments will not suffice, such as conditionally including or skipping a field. Directives provide this by describing additional information to the executor.", + "fields": [ + { + "name": "name", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": "String", + "kind": "SCALAR", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "description", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "isRepeatable", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": "Boolean", + "kind": "SCALAR", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "locations", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": null, + "kind": "LIST", + "ofType": { + "name": null, + "kind": "NON_NULL", + "ofType": { + "name": "__DirectiveLocation", + "kind": "ENUM", + "ofType": null + } + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "args", + "description": null, + "args": [ + { + "name": "includeDeprecated", + "description": null, + "type": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null + }, + "defaultValue": "false" + } + ], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": null, + "kind": "LIST", + "ofType": { + "name": null, + "kind": "NON_NULL", + "ofType": { + "name": "__InputValue", + "kind": "OBJECT", + "ofType": null + } + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "isDeprecated", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": "Boolean", + "kind": "SCALAR", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "deprecationReason", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "ENUM", + "name": "__DirectiveLocation", + "description": "A Directive can be adjacent to many parts of the GraphQL language, a __DirectiveLocation describes one such possible adjacencies.", + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": [ + { + "name": "QUERY", + "description": "Location adjacent to a query operation.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "MUTATION", + "description": "Location adjacent to a mutation operation.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "SUBSCRIPTION", + "description": "Location adjacent to a subscription operation.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "FIELD", + "description": "Location adjacent to a field.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "FRAGMENT_DEFINITION", + "description": "Location adjacent to a fragment definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "FRAGMENT_SPREAD", + "description": "Location adjacent to a fragment spread.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "INLINE_FRAGMENT", + "description": "Location adjacent to an inline fragment.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "VARIABLE_DEFINITION", + "description": "Location adjacent to a variable definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "SCHEMA", + "description": "Location adjacent to a schema definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "SCALAR", + "description": "Location adjacent to a scalar definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "OBJECT", + "description": "Location adjacent to an object type definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "FIELD_DEFINITION", + "description": "Location adjacent to a field definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "ARGUMENT_DEFINITION", + "description": "Location adjacent to an argument definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "INTERFACE", + "description": "Location adjacent to an interface definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "UNION", + "description": "Location adjacent to a union definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "ENUM", + "description": "Location adjacent to an enum definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "ENUM_VALUE", + "description": "Location adjacent to an enum value definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "INPUT_OBJECT", + "description": "Location adjacent to an input object type definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "INPUT_FIELD_DEFINITION", + "description": "Location adjacent to an input object field definition.", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "DIRECTIVE_DEFINITION", + "description": "Location adjacent to a directive definition.", + "isDeprecated": false, + "deprecationReason": null + } + ], + "possibleTypes": null + } + ], + "directives": [ + { + "name": "include", + "description": "Directs the executor to include this field or fragment only when the `if` argument is true.", + "locations": [ + "FIELD", + "FRAGMENT_SPREAD", + "INLINE_FRAGMENT" + ], + "args": [ + { + "name": "if", + "description": "Included when true.", + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": "Boolean", + "kind": "SCALAR", + "ofType": null + } + }, + "defaultValue": null + } + ] + }, + { + "name": "skip", + "description": "Directs the executor to skip this field or fragment when the `if` argument is true.", + "locations": [ + "FIELD", + "FRAGMENT_SPREAD", + "INLINE_FRAGMENT" + ], + "args": [ + { + "name": "if", + "description": "Skipped when true.", + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": "Boolean", + "kind": "SCALAR", + "ofType": null + } + }, + "defaultValue": null + } + ] + }, + { + "name": "deprecated", + "description": "Marks an element of a GraphQL schema as no longer supported.", + "locations": [ + "FIELD_DEFINITION", + "ARGUMENT_DEFINITION", + "INPUT_FIELD_DEFINITION", + "ENUM_VALUE", + "DIRECTIVE_DEFINITION" + ], + "args": [ + { + "name": "reason", + "description": "Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted using the Markdown syntax, as specified by [CommonMark](https://commonmark.org/).", + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "defaultValue": "\"No longer supported\"" + } + ] + }, + { + "name": "specifiedBy", + "description": "Exposes a URL that specifies the behavior of this scalar.", + "locations": [ + "SCALAR" + ], + "args": [ + { + "name": "url", + "description": "The URL that specifies the behavior of this scalar.", + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "name": "String", + "kind": "SCALAR", + "ofType": null + } + }, + "defaultValue": null + } + ] + }, + { + "name": "oneOf", + "description": "Indicates exactly one field must be supplied and this field must not be `null`.", + "locations": [ + "INPUT_OBJECT" + ], + "args": [] + } + ] + } + } +} diff --git a/test/inputs/graphql/separate-types1.1.json b/test/inputs/graphql/separate-types1.1.json new file mode 100644 index 0000000000..b9c398d10c --- /dev/null +++ b/test/inputs/graphql/separate-types1.1.json @@ -0,0 +1,16 @@ +{ + "data": { + "grid": { + "config": { + "items": [ + { "id": "config-component" } + ] + }, + "props": { + "items": [ + { "id": "props-component" } + ] + } + } + } +} diff --git a/test/inputs/graphql/separate-types1.graphql b/test/inputs/graphql/separate-types1.graphql new file mode 100644 index 0000000000..ef79e9484b --- /dev/null +++ b/test/inputs/graphql/separate-types1.graphql @@ -0,0 +1,14 @@ +query GridQuery { + grid { + config { + items { + id + } + } + props { + items { + id + } + } + } +} diff --git a/test/unit/graphql-separate-types.test.ts b/test/unit/graphql-separate-types.test.ts new file mode 100644 index 0000000000..5a34d779dc --- /dev/null +++ b/test/unit/graphql-separate-types.test.ts @@ -0,0 +1,45 @@ +import fs from "node:fs"; + +import { describe, expect, test } from "vitest"; + +import { GraphQLInput } from "quicktype-graphql-input"; +import { InputData, quicktype } from "quicktype-core"; + +const schema = JSON.parse( + fs.readFileSync("test/inputs/graphql/separate-types.gqlschema", "utf8"), +); +const query = fs.readFileSync( + "test/inputs/graphql/separate-types1.graphql", + "utf8", +); + +async function renderTypeScript(combineClasses: boolean): Promise { + const input = new GraphQLInput(); + await input.addSource({ name: "SeparateTypes1", schema, query }); + + const inputData = new InputData(); + inputData.addInput(input); + + const result = await quicktype({ + inputData, + lang: "typescript", + combineClasses, + rendererOptions: { "just-types": true }, + }); + return result.lines.join("\n"); +} + +describe("GraphQL object types", () => { + test.each([ + false, + true, + ])("keeps structurally identical schema types separate when combineClasses=%s", async (combineClasses) => { + const output = await renderTypeScript(combineClasses); + + expect(output).toContain("export interface Config {"); + expect(output).toContain("export interface Props {"); + expect(output).toMatch(/config: Config \| null;/); + expect(output).toMatch(/props:\s+Props \| null;/); + expect(output.match(/export interface Item \{/g)).toHaveLength(1); + }); +});