Skip to content

Commit

Permalink
feat: skip table types that do not have primary key constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
mgagliardo91 authored and cdaringe committed Jul 1, 2022
1 parent a3899d5 commit 4d9c10a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/postgraphile-upsert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4d9c10a

Please sign in to comment.