kinetex - v1.0.0-rc.1
    Preparing search index...

    Class GraphQLClient

    Index

    Constructors

    Methods

    • Clear the process-wide APQ cache shared by all client instances.

      Returns void

    • Get APQ cache metrics for this client.

      Returns APQMetrics

      An object with size (number of cached query hashes), hits (cache hit count), and misses (cache miss count).

    • Execute a GraphQL query and return the data payload. Automatically detects the operation name if one is not explicitly provided.

      Type Parameters

      • T = unknown

        Shape of the expected data object in the response

      • V extends Record<string, unknown> = Record<string, unknown>

        Shape of the variables object

      Parameters

      • query: string

        GraphQL query string

      • Optionalvariables: V

        Optional query variables

      • options: {
            operationName?: string;
            headers?: Record<string, string>;
            signal?: AbortSignal;
            extensions?: Record<string, unknown>;
        } = {}

        Additional options (operationName, headers, signal, extensions)

      Returns Promise<T>

      The data field from the GraphQL response

      GraphQLClientError On network errors, GraphQL errors, or missing data

    • Execute a GraphQL mutation. Delegates to GraphQLClient.query — mutations use POST semantics automatically via the client's transport layer.

      Type Parameters

      • T = unknown

        Shape of the expected data object in the response

      • V extends Record<string, unknown> = Record<string, unknown>

        Shape of the variables object

      Parameters

      • query: string

        GraphQL mutation string

      • Optionalvariables: V

        Optional mutation variables

      • options: {
            operationName?: string;
            signal?: AbortSignal;
            extensions?: Record<string, unknown>;
        } = {}

        Additional options

      Returns Promise<T>

      The data field from the GraphQL response

    • Execute a GraphQL mutation with file uploads using the GraphQL multipart request spec.

      The request body is sent as multipart/form-data with:

      • operations — the JSON-encoded GraphQL request (file variables replaced with null)
      • map — mapping of file indices to variable paths
      • 0, 1, … — the actual file blobs

      Type Parameters

      • T = unknown

        Shape of the expected data object in the response

      Parameters

      • query: string

        GraphQL mutation string

      • variables: Record<string, unknown>

        Mutation variables (file variables excluded — pass them via uploads)

      • uploads: GraphQLUpload[]

        Array of file uploads with variable paths

      • options: { operationName?: string; signal?: AbortSignal } = {}

        Additional options

      Returns Promise<T>

      The data field from the GraphQL response

      const data = await client.upload(
      `mutation ($file: Upload!) { uploadFile(file: $file) { id } }`,
      {},
      [{ file: myBlob, path: "file" }],
      );
    • Execute multiple GraphQL requests in a single batch HTTP call. All requests are sent as a JSON array to the endpoint, and the response must be a JSON array of GraphQLResponse objects.

      Type Parameters

      • T = unknown[]

        Expected element type in the returned array (defaults to unknown[] for maximum flexibility)

      Parameters

      • requests: GraphQLRequest<Record<string, unknown>>[]

        Array of GraphQL requests

      • options: { signal?: AbortSignal } = {}

        Additional options (signal)

      Returns Promise<T[]>

      Array of data fields, one per request in order

      GraphQLClientError If the batch response is not an array, contains GraphQL errors, or is missing data for any request

    • Subscribe to GraphQL subscriptions using Server-Sent Events (SSE).

      Type Parameters

      • T = unknown

        Shape of yielded data values

      • V extends Record<string, unknown> = Record<string, unknown>

        Shape of subscription variables

      Parameters

      • query: string

        GraphQL subscription string

      • Optionalvariables: V

        Optional subscription variables

      • options: { operationName?: string; signal?: AbortSignal; url?: string } = {}

        Options: custom url, signal, operationName

      Returns AsyncGenerator<T>

      data payloads from each incoming subscription event

      // Basic subscription
      for await (const data of client.subscribe(`subscription { onMessage { text } }`)) {
      console.log(data);
      }

      // With variables and custom endpoint
      for await (const data of client.subscribe(
      `subscription OnMessage($chatId: ID!) { onMessage(chatId: $chatId) { text } }`,
      { chatId: "123" },
      { url: "wss://api.example.com/graphql" }
      )) {
      console.log(data);
      }

      Use SSE (this method) when:

      • Server supports GraphQL over SSE (popular with Apollo, Hasura)
      • Need simple server-sent events without WebSocket complexity
      • Running in environment with limited WebSocket support

      Use WebSocket (ws.ts) when:

      • Server requires bidirectional communication
      • Need more efficient persistent connections
      • Server supports graphql-ws protocol

      Currently reconnect: false - subscriptions do NOT auto-reconnect on disconnect. This is intentional: re-establishing a subscription requires re-subscribing with the same operation, which may have side effects on the server. Caller should handle reconnection logic if needed.

    • Introspect the GraphQL schema using the standard __schema query. Returns the raw schema object without type-safety.

      Returns Promise<unknown>

      The full __schema introspection result

    • Execute a raw GraphQLRequest and return the full GraphQLResponse including data, errors, and extensions. Unlike query, this does NOT throw on GraphQL errors — the caller inspects the response directly.

      Type Parameters

      • T = unknown

        Expected type of the data field

      Parameters

      • req: GraphQLRequest

        Fully-formed GraphQL request

      • Optionalsignal: AbortSignal

        Optional abort signal

      Returns Promise<GraphQLResponse<T>>

      The full GraphQL response envelope