Skip to content

Commit ed75ce2

Browse files
authored
fix(types): fix enum tables when used in custom mutations (#680)
1 parent c474ed8 commit ed75ce2

File tree

9 files changed

+82
-17
lines changed

9 files changed

+82
-17
lines changed

packages/graphile-build-pg/src/plugins/PgIntrospectionPlugin.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export interface PgType {
9898
enumDescriptions: string[] | void;
9999
rangeSubTypeId: string | void;
100100
tags: { [tag: string]: true | string | Array<string> };
101+
isFake?: boolean;
101102
}
102103

103104
export interface PgAttribute {

packages/graphile-build-pg/src/plugins/PgIntrospectionPlugin.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export type PgType = {
109109
enumDescriptions: ?(string[]),
110110
rangeSubTypeId: ?string,
111111
tags: { [string]: string },
112+
isFake: ?boolean,
112113
};
113114

114115
export type PgAttribute = {
@@ -714,9 +715,11 @@ Original error: ${e.message}
714715

715716
// Create fake enum type
716717
const constraintIdent =
717-
constraint.type === "p" ? "" : `_${constraint.name}`;
718+
(constraint.type === "p" ? "" : `_${constraint.name}`) +
719+
"_fake_enum";
718720
const enumTypeArray = {
719721
kind: "type",
722+
isFake: true,
720723
id: `FAKE_ENUM_${klass.namespaceName}_${klass.name}${constraintIdent}_list`,
721724
name: `_${klass.name}${constraintIdent}`,
722725
description: null,
@@ -739,6 +742,7 @@ Original error: ${e.message}
739742
};
740743
const enumType = {
741744
kind: "type",
745+
isFake: true,
742746
id: `FAKE_ENUM_${klass.namespaceName}_${klass.name}${constraintIdent}`,
743747
name: `${klass.name}${constraintIdent}`,
744748
description: klass.description,

packages/graphile-build-pg/src/plugins/PgTablesPlugin.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,11 @@ export default (function PgTablesPlugin(
323323
const v = obj[fieldName];
324324
if (inputField && v != null) {
325325
const { type, typeModifier } = inputField;
326-
return sql.fragment`${gql2pg(
327-
v,
328-
type,
329-
typeModifier
330-
)}::${sql.identifier(type.namespaceName, type.name)}`;
326+
return sql.fragment`${gql2pg(v, type, typeModifier)}::${
327+
type.isFake
328+
? sql.identifier("unknown")
329+
: sql.identifier(type.namespaceName, type.name)
330+
}`;
331331
} else {
332332
return sql.null; // TODO: return default instead.
333333
}
@@ -336,10 +336,14 @@ export default (function PgTablesPlugin(
336336
return sql.fragment`row(${sql.join(
337337
attributes.map(attr2sql),
338338
","
339-
)})::${sql.identifier(
340-
tablePgType.namespaceName,
341-
tablePgType.name
342-
)}`;
339+
)})::${
340+
tablePgType.isFake
341+
? sql.identifier("unknown")
342+
: sql.identifier(
343+
tablePgType.namespaceName,
344+
tablePgType.name
345+
)
346+
}`;
343347
},
344348
};
345349

packages/graphile-build-pg/src/plugins/PgTypesPlugin.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,11 @@ export default (function PgTypesPlugin(
149149
return sql.fragment`array[${sql.join(
150150
val.map(v => gql2pg(v, type.arrayItemType, modifier)),
151151
", "
152-
)}]::${sql.identifier(type.namespaceName)}.${sql.identifier(
153-
type.name
154-
)}`;
152+
)}]::${
153+
type.isFake
154+
? sql.identifier("unknown")
155+
: sql.identifier(type.namespaceName, type.name)
156+
}`;
155157
} else {
156158
return sql.value(val);
157159
}

packages/graphile-build-pg/src/plugins/viaTemporaryTable.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,14 @@ select ${sql.join(
124124
(outputArgName, idx) =>
125125
sql.query`(${sqlValuesAlias}.output_value_list)[${sql.literal(
126126
idx + 1
127-
)}]::${sql.identifier(
128-
outputArgTypes[idx].namespaceName,
129-
outputArgTypes[idx].name
130-
)} as ${sql.identifier(
127+
)}]::${
128+
outputArgTypes[idx].isFake
129+
? sql.identifier("unknown")
130+
: sql.identifier(
131+
outputArgTypes[idx].namespaceName,
132+
outputArgTypes[idx].name
133+
)
134+
} as ${sql.identifier(
131135
// According to https://www.postgresql.org/docs/10/static/sql-createfunction.html,
132136
// "If you omit the name for an output argument, the system will choose a default column name."
133137
// In PG 9.x and 10, the column names appear to be assigned with a `column` prefix.

packages/postgraphile-core/__tests__/fixtures/mutations/enum_tables.mutations.graphql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ mutation {
1919
description
2020
}
2121
}
22+
23+
referencingTableMutation(input: { t: { enum1: A1, enum2: B2, enum3: C4 } }) {
24+
integer
25+
}
2226
}

packages/postgraphile-core/__tests__/integration/__snapshots__/mutations.test.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ Object {
7676
"letter": "C",
7777
},
7878
},
79+
"referencingTableMutation": Object {
80+
"integer": 435,
81+
},
7982
},
8083
}
8184
`;

packages/postgraphile-core/__tests__/integration/schema/__snapshots__/enum_tables.test.js.snap

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,12 @@ type Mutation {
378378
"""
379379
input: DeleteReferencingTableByIdInput!
380380
): DeleteReferencingTablePayload
381+
referencingTableMutation(
382+
"""
383+
The exclusive input argument for this mutation. An object type, make sure to see documentation for this object’s fields.
384+
"""
385+
input: ReferencingTableMutationInput!
386+
): ReferencingTableMutationPayload
381387
382388
"""
383389
Updates a single \`LetterDescription\` using its globally unique id and a patch.
@@ -582,6 +588,31 @@ input ReferencingTableInput {
582588
id: Int
583589
}
584590
591+
"""All input for the \`referencingTableMutation\` mutation."""
592+
input ReferencingTableMutationInput {
593+
"""
594+
An arbitrary string value with no semantic meaning. Will be included in the
595+
payload verbatim. May be used to track mutations by the client.
596+
"""
597+
clientMutationId: String
598+
t: ReferencingTableInput
599+
}
600+
601+
"""The output of our \`referencingTableMutation\` mutation."""
602+
type ReferencingTableMutationPayload {
603+
"""
604+
The exact same \`clientMutationId\` that was provided in the mutation input,
605+
unchanged and unused. May be used by a client to track mutations.
606+
"""
607+
clientMutationId: String
608+
integer: Int
609+
610+
"""
611+
Our root query field type. Allows us to run any query from our mutation payload.
612+
"""
613+
query: Query
614+
}
615+
585616
"""
586617
Represents an update to a \`ReferencingTable\`. Fields that are set will be updated.
587618
"""

packages/postgraphile-core/__tests__/kitchen-sink-schema.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,18 @@ create table enum_tables.referencing_table(
12031203
enum_3 char(2) references enum_tables.lots_of_enums(enum_3)
12041204
);
12051205

1206+
-- Relates to https://github.com/graphile/postgraphile/issues/1365
1207+
create function enum_tables.referencing_table_mutation(t enum_tables.referencing_table)
1208+
returns int as $$
1209+
declare
1210+
v_out int;
1211+
begin
1212+
insert into enum_tables.referencing_table (enum_1, enum_2, enum_3) values (t.enum_1, t.enum_2, t.enum_3)
1213+
returning id into v_out;
1214+
return v_out;
1215+
end;
1216+
$$ language plpgsql volatile;
1217+
12061218

12071219
--------------------------------------------------------------------------------
12081220

0 commit comments

Comments
 (0)