From 7af624d1260bcc9688b12016d228300d22d108a8 Mon Sep 17 00:00:00 2001 From: Michael Gagliardo Date: Wed, 8 Jun 2022 16:53:27 -0400 Subject: [PATCH] feat: skip table types that do not have primary key constraints --- src/__tests__/main.test.ts | 29 +++++++++++++++++++++++++++++ src/postgraphile-upsert.ts | 1 + 2 files changed, 30 insertions(+) diff --git a/src/__tests__/main.test.ts b/src/__tests__/main.test.ts index 4163b533..31cf9f0e 100644 --- a/src/__tests__/main.test.ts +++ b/src/__tests__/main.test.ts @@ -45,6 +45,11 @@ test.beforeEach(async (t) => { unique (project_name, title) ) `); + await t.context.client.query(` + create table no_primary_keys( + name text + ) + `) const middleware = postgraphile(t.context.client, "public", { graphiql: true, appendPlugins: [PgMutationUpsertPlugin], @@ -80,6 +85,20 @@ const execGqlOp = (t: PluginExecutionContext, query: () => string) => return json; }); +const fetchMutationTypes = async (t: PluginExecutionContext) => { + const query = nanographql` + query { + __type(name: "Mutation") { + name + fields { + name + } + } + } + ` + return execGqlOp(t, query) +} + const fetchAllBikes = async (t: PluginExecutionContext) => { const query = nanographql` query { @@ -133,6 +152,16 @@ const create = async (t: PluginExecutionContext) => ` ); +test("ignores tables without primary keys", async (t) => { + await create(t) + const res = await fetchMutationTypes(t) + const upsertMutations = new Set(res.data.__type.fields.map(({ name }) => name).filter((name) => name.startsWith('upsert'))) + t.assert(upsertMutations.size === 2) + t.assert(upsertMutations.has('upsertBike')) + t.assert(upsertMutations.has('upsertRole')) + +}) + test("upsert crud", async (t) => { await create(t); const res = await fetchAllBikes(t); diff --git a/src/postgraphile-upsert.ts b/src/postgraphile-upsert.ts index 1df2ed37..cc69ec3d 100644 --- a/src/postgraphile-upsert.ts +++ b/src/postgraphile-upsert.ts @@ -32,6 +32,7 @@ export const PgMutationUpsertPlugin: Plugin = (builder) => { fields, pgIntrospectionResultsByKind.class .filter((table: any) => !!table.namespace) + .filter((table: any) => !!table.primaryKeyConstraint) .filter((table: any) => !omit(table, "upsert")) .filter((table: any) => table.isSelectable) .filter((table: any) => table.isInsertable)