naystack - v1.5.10
    Preparing search index...

    Function query

    • Defines a type-graphql query or mutation with typed input/output and optional auth. Use with QueryLibrary to build resolver classes for initGraphQLServer.

      Each query definition gets .call(data) and .authCall(data) methods for direct server-side invocation (e.g. in Server Components or other resolvers). Both are cached via React's cache().

      Parameters

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

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

        • ctx is Context (or AuthorizedContext when authorized: true) with userId.
        • data is the typed GraphQL input (or void if no input).
      • options: Omit<
            QueryDefinition<T, U, IsAuth, OutputNullable, InputNullable, R>,
            "fn" | "authCall" | "call",
        >

        Configuration for the query/mutation.

        • output

          The GraphQL return type (type-graphql @ObjectType class, String, Boolean, Number, etc.).

        • input

          Optional GraphQL input type (@InputType class or scalar) for the input argument.

        • outputOptions

          Optional. Set { nullable: true } to allow null returns.

        • inputOptions

          Optional. Set { nullable: true } to allow null input.

        • authorized

          If true, the resolver requires an authenticated user. ctx is typed as AuthorizedContext (non-null userId).

        • mutation

          If true, registers as a @Mutation; otherwise registers as a @Query.

      Returns QueryDefinition<T, U, IsAuth, OutputNullable, InputNullable, R>

      A QueryDefinition with .call(data) and .authCall(data) for server-side invocation.

      import { query } from "naystack/graphql";

      export default query(
      async (ctx) => {
      if (!ctx.userId) return null;
      const [user] = await db.select().from(UserTable).where(eq(UserTable.id, ctx.userId));
      return user || null;
      },
      { output: User, outputOptions: { nullable: true } },
      );
      export default query(
      async (ctx, input: SubmitFeedbackInput) => {
      await db.insert(FeedbackTable).values({ userId: ctx.userId, score: input.score, text: input.text });
      return true;
      },
      { output: Boolean, input: SubmitFeedbackInput, authorized: true, mutation: true },
      );
      const user = await getCurrentUser.authCall();   // Reads refresh cookie for auth
      const planets = await getPlanets.call(); // No auth needed