Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/flat-buckets-make.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@baseplate-dev/fastify-generators': patch
---

Use Zod schema defined in mutations instead of restrictObjectNulls to allow for cleaner mutations and validation
5 changes: 5 additions & 0 deletions .changeset/twelve-readers-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@baseplate-dev/fastify-generators': patch
---

Add support for validation plugin in Pothos
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
"@baseplate-dev/core-generators#node/node:package-json": "package.json",
"@baseplate-dev/core-generators#node/prettier:prettier-config": ".prettierrc",
"@baseplate-dev/core-generators#node/prettier:prettier-ignore": ".prettierignore",
"@baseplate-dev/core-generators#node/ts-utils:normalize-types": "src/utils/normalize-types.ts",
"@baseplate-dev/core-generators#node/ts-utils:nulls": "src/utils/nulls.ts",
"@baseplate-dev/core-generators#node/ts-utils:string": "src/utils/string.ts",
"@baseplate-dev/core-generators#node/typescript:tsconfig": "tsconfig.json",
"@baseplate-dev/core-generators#node/vitest:global-setup": "src/tests/scripts/global-setup.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@pothos/plugin-relay": "4.6.2",
"@pothos/plugin-simple-objects": "4.1.3",
"@pothos/plugin-tracing": "1.1.0",
"@pothos/plugin-validation": "4.2.0",
"@pothos/tracing-sentry": "1.1.1",
"@prisma/adapter-pg": "6.17.1",
"@prisma/client": "6.17.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { queryFromInfo } from '@pothos/plugin-prisma';

import { builder } from '@src/plugins/graphql/builder.js';
import { restrictObjectNulls } from '@src/utils/nulls.js';

import {
createUser,
Expand All @@ -10,13 +9,15 @@ import {
} from '../services/user.data-service.js';
import { userObjectType } from './user.object-type.js';

const createUserDataInputType = builder.inputType('CreateUserData', {
fields: (t) => ({
email: t.string(),
name: t.string(),
emailVerified: t.boolean(),
}),
});
const createUserDataInputType = builder
.inputType('CreateUserData', {
fields: (t) => ({
email: t.string(),
name: t.string(),
emailVerified: t.boolean(),
}),
})
.validate(createUser.$dataSchema);

builder.mutationField('createUser', (t) =>
t.fieldWithInputPayload({
Expand All @@ -27,22 +28,25 @@ builder.mutationField('createUser', (t) =>
authorize: ['admin'],
resolve: async (root, { input: { data } }, context, info) => {
const user = await createUser({
data: restrictObjectNulls(data, ['emailVerified']),
data,
context,
query: queryFromInfo({ context, info, path: ['user'] }),
skipValidation: true,
});
return { user };
},
}),
);

const updateUserDataInputType = builder.inputType('UpdateUserData', {
fields: (t) => ({
email: t.string(),
name: t.string(),
emailVerified: t.boolean(),
}),
});
const updateUserDataInputType = builder
.inputType('UpdateUserData', {
fields: (t) => ({
email: t.string(),
name: t.string(),
emailVerified: t.boolean(),
}),
})
.validate(updateUser.$dataSchema);

builder.mutationField('updateUser', (t) =>
t.fieldWithInputPayload({
Expand All @@ -55,9 +59,10 @@ builder.mutationField('updateUser', (t) =>
resolve: async (root, { input: { id, data } }, context, info) => {
const user = await updateUser({
where: { id },
data: restrictObjectNulls(data, ['emailVerified']),
data,
context,
query: queryFromInfo({ context, info, path: ['user'] }),
skipValidation: true,
});
return { user };
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { PothosFieldWithInputPayloadPlugin } from './index.js';
import type {
MutationWithInputPayloadOptions,
OutputShapeFromFields,
PayloadFieldRef,
} from './types.js';

declare global {
Expand Down Expand Up @@ -56,10 +57,7 @@ declare global {
payload: RootFieldBuilder<Types, unknown, 'PayloadObject'>;
fieldWithInputPayload: <
InputFields extends InputFieldMap,
PayloadFields extends Record<
string,
FieldRef<Types, unknown, 'PayloadObject'>
>,
PayloadFields extends Record<string, PayloadFieldRef<Types, unknown>>,
ResolveShape,
ResolveReturnShape,
Args extends InputFieldMap = Record<never, never>,
Expand All @@ -79,7 +77,7 @@ declare global {
ShapeFromTypeParam<
Types,
ObjectRef<Types, OutputShapeFromFields<PayloadFields>>,
false
Types['DefaultFieldNullability']
>
>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {

import { capitalizeString } from '@src/utils/string.js';

import type { PayloadFieldRef } from './types.js';

const rootBuilderProto =
RootFieldBuilder.prototype as PothosSchemaTypes.RootFieldBuilder<
SchemaTypes,
Expand All @@ -27,11 +29,11 @@ rootBuilderProto.fieldWithInputPayload = function fieldWithInputPayload({
// expose all fields of payload by default
const payloadFields = (): Record<
string,
FieldRef<SchemaTypes, unknown, 'PayloadObject'>
PayloadFieldRef<SchemaTypes, unknown>
> => {
for (const key of Object.keys(payload)) {
payload[key].onFirstUse((cfg) => {
if (cfg.kind === 'Object') {
if (cfg.kind === 'Object' && !cfg.resolve) {
cfg.resolve = (parent) =>
(parent as Record<string, unknown>)[key] as Readonly<unknown>;
}
Expand All @@ -56,16 +58,19 @@ rootBuilderProto.fieldWithInputPayload = function fieldWithInputPayload({
type: payloadRef,
nullable: false,
...fieldOptions,
} as never);
} as never) as FieldRef<SchemaTypes, never, 'Mutation'>;

fieldRef.onFirstUse((config) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fieldRef.onFirstUse((config: any) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
const capitalizedName = capitalizeString(config.name);
const inputName = `${capitalizedName}Input`;
const payloadName = `${capitalizedName}Payload`;

if (inputRef) {
inputRef.name = inputName;
this.builder.inputType(inputRef, {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
description: `Input type for ${config.name} mutation`,
fields: () => input,
});
Expand All @@ -74,6 +79,7 @@ rootBuilderProto.fieldWithInputPayload = function fieldWithInputPayload({
payloadRef.name = payloadName;
this.builder.objectType(payloadRef, {
name: payloadName,
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
description: `Payload type for ${config.name} mutation`,
fields: payloadFields,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import type {
SchemaTypes,
} from '@pothos/core';

export type PayloadFieldRef<Types extends SchemaTypes, T> = Omit<
FieldRef<Types, T, 'PayloadObject'>,
'validate'
>;

export type OutputShapeFromFields<Fields extends FieldMap> =
NullableToOptional<{
[K in keyof Fields]: Fields[K] extends GenericFieldRef<infer T> ? T : never;
Expand All @@ -23,7 +28,7 @@ export type MutationWithInputPayloadOptions<
Kind extends FieldKind,
Args extends InputFieldMap,
InputFields extends InputFieldMap,
PayloadFields extends Record<string, FieldRef<Types, unknown, 'Object'>>,
PayloadFields extends Record<string, PayloadFieldRef<Types, unknown>>,
ResolveShape,
ResolveReturnShape,
> = Omit<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PrismaPlugin from '@pothos/plugin-prisma';
import RelayPlugin from '@pothos/plugin-relay';
import SimpleObjectsPlugin from '@pothos/plugin-simple-objects';
import TracingPlugin, { isRootField } from '@pothos/plugin-tracing';
import ValidationPlugin from '@pothos/plugin-validation';
import { createSentryWrapper } from '@pothos/tracing-sentry';

import type PrismaTypes from '@src/generated/prisma/pothos-prisma-types.js';
Expand Down Expand Up @@ -57,6 +58,7 @@ export const builder = new SchemaBuilder<{
pothosStripQueryMutationPlugin,
RelayPlugin,
SimpleObjectsPlugin,
ValidationPlugin,
],
prisma: {
client: prisma,
Expand Down
Loading