Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
Add t.effectWithInput
Browse files Browse the repository at this point in the history
  • Loading branch information
iamchanii committed May 30, 2024
1 parent 9ba507f commit 030a664
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-squids-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pothos-plugin-effect": minor
---

Add `t.effectWithInput`
1 change: 1 addition & 0 deletions packages/pothos-plugin-effect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ No friendly documentation is currently available, but you can check out the test
- [Adding and configuring plugins to `SchemaBuilder`](https://github.com/iamchanii/pothos-plugin-effect/blob/main/packages/pothos-plugin-effect/tests/schema/builder.ts#L9-L23)
- [Adding a field using `t.effect`](https://github.com/iamchanii/pothos-plugin-effect/blob/main/packages/pothos-plugin-effect/tests/schema/base.ts)
- [Using with the Errors plugin](https://github.com/iamchanii/pothos-plugin-effect/blob/main/packages/pothos-plugin-effect/tests/schema/error.ts)
- [Using with the With-Input plugin](https://github.com/iamchanii/pothos-plugin-effect/blob/main/packages/pothos-plugin-effect/tests/schema/withInput.ts)
- [Using with the Prisma](https://github.com/iamchanii/pothos-plugin-effect/blob/main/packages/pothos-plugin-effect/tests/schema/prisma.ts)
- [Using with the Drizzle](https://github.com/iamchanii/pothos-plugin-effect/blob/main/packages/pothos-plugin-effect/tests/schema/drizzle.ts)
- [Adding a subscription field using `Stream`](https://github.com/iamchanii/pothos-plugin-effect/blob/main/packages/pothos-plugin-effect/tests/schema/stream.ts#L11)
Expand Down
1 change: 1 addition & 0 deletions packages/pothos-plugin-effect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@pothos/plugin-errors": "3.11.1",
"@pothos/plugin-prisma": "3.63.1",
"@pothos/plugin-relay": "3.46.0",
"@pothos/plugin-with-input": "^3.10.1",
"@prisma/client": "5.9.1",
"@types/better-sqlite3": "^7.6.9",
"@types/node": "20.11.13",
Expand Down
26 changes: 25 additions & 1 deletion packages/pothos-plugin-effect/src/field-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,34 @@ fieldBuilderProto.effect = function (options) {
},
// @ts-ignore
subscribe: async (root, args, context, info) => {
if ('subscribe' in options) {
if ('subscribe' in options && typeof options.subscribe === 'function') {
const effectFieldResult = options.subscribe(root, args, context, info);

return this.executeStream(effectFieldResult);
}
},
});
};

// @ts-ignore
fieldBuilderProto.effectWithInput = function (options) {
// @ts-ignore
return this.fieldWithInput({
...options,
// @ts-ignore
resolve: async (root, args, context, info) => {
if ('resolve' in options) {
const effectFieldResult = options.resolve(root, args, context, info);

// @ts-ignore
return this.executeEffect(effectFieldResult);
}
},
// @ts-ignore
subscribe: async (root, args, context, info) => {
if ('subscribe' in options && typeof options.subscribe === 'function') {
const effectFieldResult = options.subscribe(root, args, context, info);

return this.executeStream(effectFieldResult);
}
},
Expand Down
32 changes: 32 additions & 0 deletions packages/pothos-plugin-effect/src/global-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type {
FieldNullability,
FieldRef,
InputFieldMap,
InputFieldRef,
PluginName,
SchemaTypes,
ShapeFromTypeParam,
TypeParam,
Expand Down Expand Up @@ -57,6 +59,36 @@ declare global {
ResolveReturnShape
>,
) => FieldRef<ShapeFromTypeParam<Types, Type, Nullable>>;

effectWithInput: 'withInput' extends PluginName
? <
Type extends TypeParam<Types>,
Nullable extends FieldNullability<Type>,
Args extends Record<string, InputFieldRef<unknown, 'Arg'>>,
ResolveShape,
ResolveReturnShape,
Fields extends Record<
string,
InputFieldRef<unknown, 'InputObject'>
>,
InputName extends string,
ArgRequired extends boolean,
>(
options: PluginTypes.EffectFieldWithInputOptions<
Types,
ParentShape,
Type,
Nullable,
Args,
Kind,
ResolveShape,
ResolveReturnShape,
Fields,
InputName,
ArgRequired
>,
) => FieldRef<ShapeFromTypeParam<Types, Type, Nullable>>
: '@pothos/plugin-with-input is required to use this method';
}
}
}
46 changes: 46 additions & 0 deletions packages/pothos-plugin-effect/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import type {
FieldKind,
FieldNullability,
InputFieldMap,
InputFieldRef,
InputShapeFromFields,
MaybePromise,
PluginName,
SchemaTypes,
ShapeFromTypeParam,
TypeParam,
Expand Down Expand Up @@ -218,6 +220,50 @@ export type EffectFieldOptions<
ResolveReturnShape
>[Kind];

export type EffectFieldWithInputOptions<
Types extends SchemaTypes,
ParentShape,
Type extends TypeParam<Types>,
Nullable extends FieldNullability<Type>,
Args extends Record<string, InputFieldRef<unknown, 'Arg'>>,
Kind extends FieldKind,
ResolveShape,
ResolveReturnShape,
Fields extends Record<string, InputFieldRef<unknown, 'InputObject'>>,
InputName extends string,
ArgRequired extends boolean,
> = Omit<
EffectFieldOptions<
Types,
ParentShape,
Type,
Nullable,
Args & {
[K in InputName]: InputFieldRef<
| InputShapeFromFields<Fields>
| (true extends ArgRequired ? never : null | undefined)
>;
},
Kind,
ResolveShape,
ResolveReturnShape
>,
'args'
> &
// @ts-ignore
PothosSchemaTypes.FieldWithInputBaseOptions<
Types,
Args & {
[K in InputName]: InputFieldRef<
| InputShapeFromFields<Fields>
| (true extends ArgRequired ? never : null | undefined)
>;
},
Fields,
InputName,
ArgRequired
>;

export type EffectPluginOptions<Types extends SchemaTypes> = {
effectRuntime: Types['EffectRuntime'];
};
Expand Down
25 changes: 25 additions & 0 deletions packages/pothos-plugin-effect/tests/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ test('print schema', () => {
type Mutation {
addPost(title: String!): MutationAddPostResult!
sendMessages(input: MutationSendMessagesInput!): String!
}
union MutationAddPostResult = BaseError | MutationAddPostSuccess
Expand All @@ -28,6 +29,14 @@ test('print schema', () => {
data: Boolean!
}
input MutationSendMessagesInput {
"""The message to send"""
message: String!
"""The target of the message"""
target: String!
}
type PageInfo {
endCursor: String
hasNextPage: Boolean!
Expand Down Expand Up @@ -290,3 +299,19 @@ test('subscription newPosts field', async () => {
]
`);
});

test('mutation.sendMessages field', async () => {
const document = parse(`mutation {
sendMessages(input: { target: "John", message: "Hello" })
}`);

const result = await execute({ document, schema });

expect(result).toMatchInlineSnapshot(`
{
"data": {
"sendMessages": "Sent message "Hello" to John successfully!",
},
}
`);
});
9 changes: 8 additions & 1 deletion packages/pothos-plugin-effect/tests/schema/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import SchemaBuilder from '@pothos/core';
import ErrorsPlugin from '@pothos/plugin-errors';
import PrismaPlugin from '@pothos/plugin-prisma';
import RelayPlugin from '@pothos/plugin-relay';
import WithInputPlugin from '@pothos/plugin-with-input';
import type PrismaTypes from '../../prisma/pothos-types.js';
import EffectPlugin from '../../src/index.js';
import { setupTest } from '../setupTest.js';
Expand All @@ -12,7 +13,13 @@ export const builder = new SchemaBuilder<{
EffectRuntime: typeof effectRuntime;
PrismaTypes: PrismaTypes;
}>({
plugins: [EffectPlugin, ErrorsPlugin, RelayPlugin, PrismaPlugin],
plugins: [
EffectPlugin,
ErrorsPlugin,
RelayPlugin,
PrismaPlugin,
WithInputPlugin,
],
effectOptions: { effectRuntime },
errorOptions: {
defaultTypes: [Error],
Expand Down
4 changes: 3 additions & 1 deletion packages/pothos-plugin-effect/tests/schema/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import './base.js';
import './drizzle.js';
import './error.js';
import './base.js';
import './prisma.js';
import './stream.js';
import './withInput.js';

import { builder } from './builder.js';

builder.queryType({});
Expand Down
22 changes: 22 additions & 0 deletions packages/pothos-plugin-effect/tests/schema/withInput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Effect } from 'effect';
import { builder } from './builder.js';

builder.mutationFields((t) => ({
sendMessages: t.effectWithInput({
type: 'String',
input: {
target: t.input.string({
required: true,
description: 'The target of the message',
}),
message: t.input.string({
required: true,
description: 'The message to send',
}),
},
resolve: (_root, { input }) =>
Effect.succeed(
`Sent message "${input.message}" to ${input.target} successfully!`,
),
}),
}));
11 changes: 11 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,16 @@ __metadata:
languageName: node
linkType: hard

"@pothos/plugin-with-input@npm:^3.10.1":
version: 3.10.1
resolution: "@pothos/plugin-with-input@npm:3.10.1"
peerDependencies:
"@pothos/core": "*"
graphql: ">=15.1.0"
checksum: 10c0/c16daa5610ac2933274619e064abc3586cf7326c5e0ac82fbb579afaa245a0211d7fb67bc0384a62e406609e998c8b0c0de7fe532c1334dfd5ac10432b19453a
languageName: node
linkType: hard

"@prisma/client@npm:5.9.1":
version: 5.9.1
resolution: "@prisma/client@npm:5.9.1"
Expand Down Expand Up @@ -4875,6 +4885,7 @@ __metadata:
"@pothos/plugin-errors": "npm:3.11.1"
"@pothos/plugin-prisma": "npm:3.63.1"
"@pothos/plugin-relay": "npm:3.46.0"
"@pothos/plugin-with-input": "npm:^3.10.1"
"@prisma/client": "npm:5.9.1"
"@types/better-sqlite3": "npm:^7.6.9"
"@types/node": "npm:20.11.13"
Expand Down

0 comments on commit 030a664

Please sign in to comment.