Skip to content

Commit 8a345d2

Browse files
authored
Allow null to be passed to withPgClient/withPgClientTransaction (#462)
2 parents ecbd109 + e4b310d commit 8a345d2

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@dataplan/pg": patch
3+
"postgraphile": patch
4+
---
5+
6+
Allow 'null' to be passed to `withPgClient`/`withPgClientTransaction`

grafast/dataplan-pg/src/steps/withPgClient.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { GrafastResultsList, GrafastValuesList } from "grafast";
2-
import { ExecutableStep } from "grafast";
2+
import { constant, ExecutableStep } from "grafast";
33

44
import type { PgClient, PgExecutor, WithPgClient } from "../executor";
55

@@ -70,18 +70,29 @@ export class WithPgClientStep<
7070

7171
export function withPgClient<TData, TResult>(
7272
executor: PgExecutor,
73-
$data: ExecutableStep<TData>,
73+
$data:
74+
| ExecutableStep<TData>
75+
| (TData extends null | undefined ? null | undefined : never),
7476
callback: WithPgClientStepCallback<TData, TResult>,
7577
) {
76-
return new WithPgClientStep(executor, $data, callback);
78+
return new WithPgClientStep(
79+
executor,
80+
$data ?? constant($data as TData),
81+
callback,
82+
);
7783
}
7884

7985
export function withPgClientTransaction<TData, TResult>(
8086
executor: PgExecutor,
81-
$data: ExecutableStep<TData>,
87+
$data:
88+
| ExecutableStep<TData>
89+
| (TData extends null | undefined ? null | undefined : never),
8290
callback: WithPgClientStepCallback<TData, TResult>,
8391
) {
84-
return withPgClient(executor, $data, (client, data) =>
85-
client.withTransaction((txClient) => callback(txClient, data)),
92+
return withPgClient(
93+
executor,
94+
$data ?? constant($data as TData),
95+
(client, data) =>
96+
client.withTransaction((txClient) => callback(txClient, data)),
8697
);
8798
}

postgraphile/website/postgraphile/make-extend-schema-plugin.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -519,11 +519,12 @@ export const MyRegisterUserMutationPlugin = makeExtendSchemaPlugin((build) => {
519519
`,
520520
plans: {
521521
Mutation: {
522-
registerUser() {
522+
registerUser(_, fieldArgs) {
523+
const $input = fieldArgs.getRaw("input");
523524
const $user = withPgClientTransaction(
524525
executor,
525-
null,
526-
async (pgClient) => {
526+
$input,
527+
async (pgClient, input) => {
527528
// Our custom logic to register the user:
528529
const {
529530
rows: [user],
@@ -532,14 +533,14 @@ export const MyRegisterUserMutationPlugin = makeExtendSchemaPlugin((build) => {
532533
INSERT INTO app_public.users (name, email, bio)
533534
VALUES ($1, $2, $3)
534535
RETURNING *`,
535-
values: [args.input.name, args.input.email, args.input.bio],
536+
values: [input.name, input.email, input.bio],
536537
});
537538

538539
// Send the email. If this fails then the error will be caught
539540
// and the transaction rolled back; it will be as if the user
540541
// never registered
541542
await mockSendEmail(
542-
args.input.email,
543+
input.email,
543544
"Welcome to my site",
544545
`You're user ${user.id} - thanks for being awesome`,
545546
);

0 commit comments

Comments
 (0)