Effect CommunityEC
Effect Community6mo ago
5 replies
sohai

Integrating Effect with GraphQL Yoga - Request Batching Issues

Hi everyone! 👋
I'm trying to integrate Effect with GraphQL Yoga and running into issues with request batching. The problem appears to be that Effect's batching isn't working even within the same resolver context.

Current setup:
yoga server:
export class YogaApp extends Effect.Service<YogaApp>()("YogaApp", {
  effect: Effect.gen(function* () {
    const config = yield* GraphqlConfig;
    const runPromise = yield* FiberSet.makeRuntimePromise<GrpcService>();

    const yoga = createYoga({
      schema,
      context: async (initialContext) => {
        const requestCache = Request.makeCache({
          capacity: 1000,
          timeToLive: Duration.infinity,
        });

        const wrappedRunPromise = <A, E, R extends GrpcService>(
          effect: Effect.Effect<A, E, R>,
        ) =>
          runPromise(
            effect.pipe(
              Effect.provide(Layer.setRequestCache(requestCache)),
              Effect.withRequestCaching(true),
              Effect.withRequestBatching(true),
            ),
          );

        return createGraphQLContext(
          initialContext,
          withErrorHandlingAndLogging(wrappedRunPromise),
        );
      },
      graphqlEndpoint: config.endpoint,
    });
    // ... rest of setup
  }),
}) {}


Graphql resolver
import { Effect } from "effect";
import {
    GetProductArgsSchema,
    getProduct,
} from "../../../../grpc/ProductService";
import { validateArgs } from "../../../../validation";
import type { QueryResolvers } from "./../../../types.generated";

export const productById: NonNullable<QueryResolvers["productById"]> = (
    _parent,
    args,
    ctx,
) =>
    ctx.runPromise(
        Effect.gen(function* () {
            const validatedArgs = yield* validateArgs(GetProductArgsSchema)(args);
            return yield* getProduct(validatedArgs);
        }),
    );
Was this page helpful?