File tree 2 files changed +18
-10
lines changed
2 files changed +18
-10
lines changed Original file line number Diff line number Diff line change @@ -81,6 +81,12 @@ test.beforeEach(async (t) => {
81
81
name text
82
82
)
83
83
` ) ;
84
+ await t . context . client . query ( `
85
+ create table just_unique_constraints (
86
+ name text,
87
+ unique (name)
88
+ )
89
+ ` ) ;
84
90
await initializePostgraphile ( t ) ;
85
91
} ) ;
86
92
@@ -221,17 +227,18 @@ const create = async (
221
227
return execGqlOp ( t , nanographql ( mutation ) ) ;
222
228
} ;
223
229
224
- test ( "ignores tables without primary keys" , async ( t ) => {
230
+ test ( "ignores tables without primary keys or unique constraints " , async ( t ) => {
225
231
await create ( t ) ;
226
232
const res = await fetchMutationTypes ( t ) ;
227
233
const upsertMutations = new Set (
228
234
res . data . __type . fields
229
235
. map ( ( { name } ) => name )
230
236
. filter ( ( name ) => name . startsWith ( "upsert" ) )
231
237
) ;
232
- t . assert ( upsertMutations . size === 2 ) ;
238
+ t . assert ( upsertMutations . size === 3 ) ;
233
239
t . assert ( upsertMutations . has ( "upsertBike" ) ) ;
234
240
t . assert ( upsertMutations . has ( "upsertRole" ) ) ;
241
+ t . assert ( upsertMutations . has ( "upsertJustUniqueConstraint" ) ) ;
235
242
} ) ;
236
243
237
244
test ( "upsert crud - match primary key constraint" , async ( t ) => {
Original file line number Diff line number Diff line change @@ -27,14 +27,15 @@ export const PgMutationUpsertPlugin: Plugin = (builder) => {
27
27
) . filter ( ( con ) => con . type === "u" || con . type === "p" ) ;
28
28
const upsertFieldsByName = ( pgIntrospectionResultsByKind . class as PgTable [ ] )
29
29
. filter (
30
- ( table ) =>
31
- ! ! table . namespace &&
32
- ! ! table . primaryKeyConstraint &&
33
- ! omit ( table , "upsert" ) &&
34
- table . isSelectable &&
35
- table . isInsertable &&
36
- table . isUpdatable
37
- )
30
+ ( table ) => {
31
+ const hasUniqueConstraint = allUniqueConstraints . some ( ( c ) => c . classId === table . id ) ;
32
+ return ! ! table . namespace &&
33
+ ( ! ! table . primaryKeyConstraint || hasUniqueConstraint ) &&
34
+ ! omit ( table , "upsert" ) &&
35
+ table . isSelectable &&
36
+ table . isInsertable &&
37
+ table . isUpdatable
38
+ } )
38
39
. reduce < GraphQLFieldConfigMap < unknown , unknown > > ( ( fnsByName , table ) => {
39
40
const gqlTable = pgGetGqlTypeByTypeIdAndModifier ( table . type . id , null ) ;
40
41
if ( ! gqlTable ) return fnsByName ;
You can’t perform that action at this time.
0 commit comments