naystack - v1.5.10
    Preparing search index...

    Function field

    • Defines a type-graphql field resolver with typed root, context, and input. Use with FieldLibrary to attach computed fields to a parent GraphQL type.

      Like query(), each field definition gets .call(root, data) and .authCall(root, data) for server-side invocation.

      Parameters

      • fn: (
            root: Root,
            ctx: IsAuth extends true ? AuthorizedContext : Context,
            data: ParsedGQLTypeWithNullability<U, InputNullable, false>,
        ) => R

        Resolver function: (root, ctx, data) => result.

        • root is the parent object (e.g. the User row from the database).
        • ctx is Context (or AuthorizedContext when authorized: true).
        • data is the optional typed input argument.
      • options: Omit<
            FieldResolverDefinition<
                T,
                U,
                Root,
                IsAuth,
                OutputNullable,
                InputNullable,
                R,
            >,
            "fn"
            | "authCall"
            | "call",
        >

        Configuration (same as query() but without mutation).

      Returns FieldResolverDefinition<T, U, Root, IsAuth, OutputNullable, InputNullable, R>

      A FieldResolverDefinition with .call and .authCall for server-side invocation.

      import { field } from "naystack/graphql";

      // Resolve the "seller" field on a Property type
      const seller = field(
      async (property: PropertyDB) => {
      if (!property.sellerId) return null;
      const [seller] = await db.select().from(ContactTable).where(eq(ContactTable.id, property.sellerId));
      return seller || null;
      },
      { output: ContactGQL, outputOptions: { nullable: true } },
      );
      const OrgFields = FieldLibrary<OrgDB>(OrgGQL, {
      access: field(
      async (root, ctx) => {
      if (!ctx.userId) return null;
      return checkOrgAccess(ctx.userId, root.id);
      },
      { output: AccessRole, outputOptions: { nullable: true } },
      ),
      });