diff --git a/packages/fields-document/src/structure.ts b/packages/fields-document/src/structure.ts index 1d51f332a4f..fc2545e7fbf 100644 --- a/packages/fields-document/src/structure.ts +++ b/packages/fields-document/src/structure.ts @@ -52,46 +52,47 @@ export const structure = const defaultValue = getInitialPropsValue(schema); const unreferencedConcreteInterfaceImplementations: graphql.ObjectType[] = []; - if (!globalStructureTypes[name]) { - globalStructureTypes[name] = { - output: graphql.object<{ value: JSONValue }>()({ - name: `${name}Output`, - fields: { - structure: getOutputGraphQLField( - name, - schema, - unreferencedConcreteInterfaceImplementations, - new Map(), - meta - ), - json: graphql.field({ - type: graphql.JSON, - args: { - hydrateRelationships: graphql.arg({ - type: graphql.nonNull(graphql.Boolean), - defaultValue: false, - }), - }, - resolve({ value }, args, context) { - if (args.hydrateRelationships) { - return addRelationshipDataToComponentProps(schema, value, (schema, value) => - fetchRelationshipData( - context, - schema.listKey, - schema.many, - schema.selection || '', - value - ) - ); - } - return value; - }, - }), - }, - }), - update: getGraphQLInputType(name, schema, 'update', new Map(), meta), - create: getGraphQLInputType(name, schema, 'create', new Map(), meta), - }; + const graphqlTypes = globalStructureTypes[name] || { + output: graphql.object<{ value: JSONValue }>()({ + name: `${name}Output`, + fields: { + structure: getOutputGraphQLField( + name, + schema, + unreferencedConcreteInterfaceImplementations, + new Map(), + meta + ), + json: graphql.field({ + type: graphql.JSON, + args: { + hydrateRelationships: graphql.arg({ + type: graphql.nonNull(graphql.Boolean), + defaultValue: false, + }), + }, + resolve({ value }, args, context) { + if (args.hydrateRelationships) { + return addRelationshipDataToComponentProps(schema, value, (schema, value) => + fetchRelationshipData( + context, + schema.listKey, + schema.many, + schema.selection || '', + value + ) + ); + } + return value; + }, + }), + }, + }), + update: getGraphQLInputType(name, schema, 'update', new Map(), meta), + create: getGraphQLInputType(name, schema, 'create', new Map(), meta), + }; + if (config.graphql?.typeName) { + globalStructureTypes[name] = graphqlTypes; } return jsonFieldTypePolyfilledForSQLite( @@ -125,7 +126,7 @@ export const structure = input: { create: { arg: graphql.arg({ - type: globalStructureTypes[name].create, + type: graphqlTypes.create, }), async resolve(val, context) { return await getValueForCreate(schema, val, context, []); @@ -133,12 +134,12 @@ export const structure = }, update: { arg: graphql.arg({ - type: globalStructureTypes[name].update, + type: graphqlTypes.update, }), }, }, output: graphql.field({ - type: globalStructureTypes[name].output, + type: graphqlTypes.output, resolve(source) { return source; }, diff --git a/tests/sandbox/schema.graphql b/tests/sandbox/schema.graphql index 918be5ef242..ef385bb5226 100644 --- a/tests/sandbox/schema.graphql +++ b/tests/sandbox/schema.graphql @@ -6,9 +6,9 @@ type Thing { text: String timestamp: DateTime structure: ThingStructureOutput - structureRelationships: ThingStructureRelationshipsOutput structureNested: StructureNestedOutput structureNested2: StructureNestedOutput + structureRelationships: ThingStructureRelationshipsOutput checkbox: Boolean password: PasswordState toOneRelationship: User @@ -46,8 +46,8 @@ type ThingStructure { array: [Int] } -type ThingStructureNestedOutput { - structure: [ThingStructureNested] +type StructureNestedOutput { + structure: [StructureNested] json(hydrateRelationships: Boolean! = false): JSON } @@ -303,10 +303,9 @@ input ThingUpdateInput { text: String timestamp: DateTime structure: ThingStructureUpdateInput - structureNested: [ThingStructureNestedUpdateInput] - structureRelationships: [ThingRelateToOneForUpdateInput] structureNested: [StructureNestedUpdateInput] structureNested2: [StructureNestedUpdateInput] + structureRelationships: [ThingRelateToOneForUpdateInput] checkbox: Boolean password: String toOneRelationship: UserRelateToOneForUpdateInput @@ -334,9 +333,9 @@ input ThingStructureUpdateInput { array: [Int] } -input ThingStructureNestedUpdateInput { - leaf: ThingStructureNestedLeafUpdateInput - group: ThingStructureNestedGroupUpdateInput +input StructureNestedUpdateInput { + leaf: StructureNestedLeafUpdateInput + group: StructureNestedGroupUpdateInput } input StructureNestedLeafUpdateInput { @@ -388,10 +387,9 @@ input ThingCreateInput { text: String timestamp: DateTime structure: ThingStructureCreateInput - structureNested: [ThingStructureNestedCreateInput] - structureRelationships: [ThingRelateToOneForCreateInput] structureNested: [StructureNestedCreateInput] structureNested2: [StructureNestedCreateInput] + structureRelationships: [ThingRelateToOneForCreateInput] checkbox: Boolean password: String toOneRelationship: UserRelateToOneForCreateInput @@ -419,9 +417,9 @@ input ThingStructureCreateInput { array: [Int] } -input ThingStructureNestedCreateInput { - leaf: ThingStructureNestedLeafCreateInput - group: ThingStructureNestedGroupCreateInput +input StructureNestedCreateInput { + leaf: StructureNestedLeafCreateInput + group: StructureNestedGroupCreateInput } input StructureNestedLeafCreateInput { diff --git a/tests/sandbox/schema.prisma b/tests/sandbox/schema.prisma index 991d7bb1268..55f7b22ef74 100644 --- a/tests/sandbox/schema.prisma +++ b/tests/sandbox/schema.prisma @@ -18,8 +18,8 @@ model Thing { timestamp DateTime? structure String @default("{\"integer\":0,\"array\":[]}") structureNested String @default("[]") - structureRelationships String @default("[]") structureNested2 String @default("[]") + structureRelationships String @default("[]") checkbox Boolean @default(false) password String? toOneRelationship User? @relation("Thing_toOneRelationship", fields: [toOneRelationshipId], references: [id])