Seamless integration between the Pothos GraphQL and Effect.
You can use Effect.Effect<A, E, R>
within resolver functions using t.effect
. and that's it.
import EffectPlugin from "pothos-plugin-effect";
const builder = new SchemaBuilder({
plugins: [EffectPlugin],
});
builder.queryFields((t) => ({
roll: t.int({
resolve: () => t.effect(Effect.succeed(6)),
}),
}));
Have you been used since 0.x? See MIGRATION.md.
Install pothos-effect-plugin
and effect
. Effect is a peer-dependency.
yarn add pothos-plugin-effect effect
@pothos/core^3
effect>=3.0.0
- 🪄 Ridiculously simple API, the only thing added is
t.effect
. - 🌿 Can be used by any Pothos field resolver.
- 📦 Supports Option data type.
- ⚙️ Custom Runtime can be configured.
- ⏳ Promise objects can be used as result values.
- ✅ Well-written test cases
t.effect
returns the given Option.Option<T>
data by converting its value to (T | null)
. This is useful for creating fields that are nullable, while actively using the Option data type in Effect.
t.effect(Effect.succeed(1));
// ^? Promise<number>
t.effect(Effect.succeedSome(1));
// ^? Promise<number | null>
t.effect(
Effect.succeedSome([
//
Option.some(1),
Option.none(),
Option.some(3),
])
);
// ^? Promise<(number | null)[] | null>
You can configure a custom runtime when you configure SchemaBuilder
.
declare const effectRuntime: Runtime.Runtime<UserService>;
const builder = new SchemaBuilder<{
EffectRuntime: typeof effectRuntime;
}>({
plugins: [EffectPlugin],
effectOptions: { effectRuntime },
});
builder.queryFields((t) => ({
user: t.field({
type: User,
resolve: () =>
t.effect(
pipe(
UserService,
Effect.flatMap((userService) => userService.getUser(1))
)
),
}),
}));
By default, you can't use promises directly in Effect; you need to convert them to Effect via Effect.tryPromise. However, promises are available by default within GraphQL resolvers.
If t.effect
returns a Promise from an Effect, it will return that Promise as the result, making it available for use within the resolver. It will also correctly infer the type of the Promise.
This is useful when using an ORM like Prisma or Drizzle with it.
t.effect(Effect.succeed(Promise.resolve({ id: 1, name: "John" })));
// ^? Promise<{ id: number; name: string; }>
When I rewrote this library, I put a lot of effort into the test cases. You can find test cases for the core functionality of the library, as well as simple E2E test cases.
- Pothos by @hayes (GitHub/Docs) - A nice GraphQL Schema builder. I heavily relied on the README for this project and The documentation of the plugin implementation is excellent.
- Effect (GitHub/Docs) - This is a pretty amazing growth trend. A library that has the charm to bring you back. (including me, of course)
Made with contrib.rocks.
MIT