@probitas/client-graphql

GraphQL client for Probitas scenario testing framework.

This package provides a GraphQL client designed for integration testing of GraphQL APIs.

Features

  • Query & Mutation: Full support for GraphQL operations
  • Variables Support: Pass typed variables to queries and mutations
  • Duration Tracking: Built-in timing for performance monitoring
  • Error Inspection: Inspect GraphQL errors and their structure
  • Resource Management: Implements AsyncDisposable for proper cleanup
  • Template Literals: Re-exports outdent for clean multi-line queries

Installation

deno add jsr:@probitas/client-graphql

Quick Start

import { createGraphqlClient, outdent } from "@probitas/client-graphql";

const client = createGraphqlClient({ url: "http://localhost:4000/graphql" });

// Query with variables
const res = await client.query(outdent`
  query GetUser($id: ID!) {
    user(id: $id) { id name email }
  }
`, { id: "123" });
console.log("User:", res.data());

// Mutation
const created = await client.mutation(outdent`
  mutation CreateUser($input: CreateUserInput!) {
    createUser(input: $input) { id name }
  }
`, { input: { name: "Jane", email: "jane@example.com" } });
console.log("Created:", created.data());

await client.close();

Using with using Statement

await using client = createGraphqlClient({ url: "http://localhost:4000/graphql" });

const res = await client.query(`{ __typename }`);
console.log(res.data());
// Client automatically closed when block exits
Package Description
@probitas/client Core utilities and types
@probitas/client-http HTTP client

Installation

deno add jsr:@probitas/client-graphql

Classes

class

#GraphqlError

class GraphqlError extends ClientError

Base GraphQL error class.

NameDescription
name
kind
errorsGraphQL errors from response
responseAssociated GraphQL response (if available)
Constructor
new GraphqlError(message: string, errors: readonly GraphqlErrorItem[], options?: GraphqlErrorOptions)
Properties
  • readonlynamestring
  • readonlykind"graphql"
  • readonlyerrorsreadonly GraphqlErrorItem[]

    GraphQL errors from response

  • readonlyresponse?GraphqlResponse

    Associated GraphQL response (if available)

class

#GraphqlExecutionError

class GraphqlExecutionError extends GraphqlError

Error thrown for GraphQL execution errors.

NameDescription
name
Constructor
new GraphqlExecutionError(errors: readonly GraphqlErrorItem[], options?: GraphqlErrorOptions)
Properties
  • readonlynamestring
class

#GraphqlNetworkError

class GraphqlNetworkError extends GraphqlError

Error thrown for network/HTTP errors before GraphQL processing.

NameDescription
name
Constructor
new GraphqlNetworkError(message: string, options?: ErrorOptions)
Properties
  • readonlynamestring
class

#GraphqlValidationError

class GraphqlValidationError extends GraphqlError

Error thrown for GraphQL validation errors.

NameDescription
name
Constructor
new GraphqlValidationError(errors: readonly GraphqlErrorItem[], options?: GraphqlErrorOptions)
Properties
  • readonlynamestring

Interfaces

interface

#GraphqlClient

interface GraphqlClient extends AsyncDisposable

GraphQL client interface.

NameDescription
configClient configuration
query()Execute a GraphQL query
mutation()Execute a GraphQL mutation
execute()Execute a GraphQL document (query or mutation)
subscribe()Subscribe to a GraphQL subscription via WebSocket
close()Close the client and release resources
Properties
Methods
query<TData = any, TVariables = Record<string, any>>(query: string, variables?: TVariables, options?: GraphqlOptions): Promise<GraphqlResponse<TData>>

Execute a GraphQL query

Parameters
mutation<TData = any, TVariables = Record<string, any>>(mutation: string, variables?: TVariables, options?: GraphqlOptions): Promise<GraphqlResponse<TData>>

Execute a GraphQL mutation

Parameters
execute<TData = any, TVariables = Record<string, any>>(document: string, variables?: TVariables, options?: GraphqlOptions): Promise<GraphqlResponse<TData>>

Execute a GraphQL document (query or mutation)

Parameters
subscribe<TData = any, TVariables = Record<string, any>>(document: string, variables?: TVariables, options?: GraphqlOptions): AsyncIterable<GraphqlResponse<TData>>

Subscribe to a GraphQL subscription via WebSocket

Parameters
close(): Promise<void>

Close the client and release resources

interface

#GraphqlClientConfig

interface GraphqlClientConfig extends CommonOptions

GraphQL client configuration.

NameDescription
urlGraphQL endpoint URL.
headersDefault headers for all requests
wsEndpointWebSocket endpoint URL (for subscriptions)
fetchCustom fetch implementation (for testing/mocking)
throwOnErrorWhether to throw GraphqlError when response contains errors.
Properties
  • readonlyurlstring | GraphqlConnectionConfig

    GraphQL endpoint URL.

    Can be a URL string or a connection configuration object.

  • readonlyheaders?Record<string, string>

    Default headers for all requests

  • readonlywsEndpoint?string

    WebSocket endpoint URL (for subscriptions)

  • readonlyfetch?fetch

    Custom fetch implementation (for testing/mocking)

  • readonlythrowOnError?boolean

    Whether to throw GraphqlError when response contains errors. Can be overridden per-request via GraphqlOptions.

interface

#GraphqlConnectionConfig

interface GraphqlConnectionConfig extends CommonConnectionConfig

GraphQL connection configuration.

Extends CommonConnectionConfig with GraphQL-specific options.

NameDescription
protocolProtocol to use.
pathGraphQL endpoint path.
Properties
  • readonlyprotocol?"http" | "https"

    Protocol to use.

  • readonlypath?string

    GraphQL endpoint path.

interface

#GraphqlErrorItem

interface GraphqlErrorItem

GraphQL error item as per GraphQL specification.

NameDescription
messageError message
locationsLocation(s) in the GraphQL document where the error occurred
pathPath to the field that caused the error
extensionsAdditional error metadata
Properties
  • readonlymessagestring

    Error message

  • readonlylocations?readonly { line: number; column: number }[]

    Location(s) in the GraphQL document where the error occurred

  • readonlypath?readonly unknown[]

    Path to the field that caused the error

  • readonlyextensions?Record<string, unknown>

    Additional error metadata

interface

#GraphqlErrorOptions

interface GraphqlErrorOptions extends ErrorOptions

Options for GraphqlError constructor.

NameDescription
responseAssociated GraphQL response
Properties
interface

#GraphqlOptions

interface GraphqlOptions extends CommonOptions

Options for individual GraphQL requests.

NameDescription
headersAdditional request headers
operationNameOperation name (for documents with multiple operations)
throwOnErrorWhether to throw GraphqlError when response contains errors.
Properties
  • readonlyheaders?Record<string, string>

    Additional request headers

  • readonlyoperationName?string

    Operation name (for documents with multiple operations)

  • readonlythrowOnError?boolean

    Whether to throw GraphqlError when response contains errors.

interface

#GraphqlResponse

interface GraphqlResponse<T = any>

GraphQL response interface with pre-loaded body.

NameDescription
typeResult type identifier
okWhether the request was successful (no errors)
errorsGraphQL errors array (null if no errors)
extensionsResponse extensions
durationResponse time in milliseconds
statusHTTP status code
headersHeaders from the HTTP response
rawRaw Web standard Response (for streaming or special cases)
data()Get response data (null if no data).
Properties
  • readonlytype"graphql"

    Result type identifier

  • readonlyokboolean

    Whether the request was successful (no errors)

  • readonlyerrorsreadonly GraphqlErrorItem[] | null

    GraphQL errors array (null if no errors)

  • readonlyextensions?Record<string, unknown>

    Response extensions

  • readonlydurationnumber

    Response time in milliseconds

  • readonlystatusnumber

    HTTP status code

  • readonlyheadersHeaders

    Headers from the HTTP response

  • readonlyrawglobalThis.Response

    Raw Web standard Response (for streaming or special cases)

Methods
data<U = T>(): U | null

Get response data (null if no data). Does not throw even if errors are present.

interface

#GraphqlResponseOptions

interface GraphqlResponseOptions<T>

Options for creating a GraphqlResponse.

NameDescription
data
errors
extensions
duration
status
raw
Properties
  • readonlydataT | null
  • readonlyerrorsreadonly GraphqlErrorItem[] | null
  • readonlyextensions?Record<string, unknown>
  • readonlydurationnumber
  • readonlystatusnumber
  • readonlyrawglobalThis.Response

Functions

function

#createGraphqlClient

function createGraphqlClient(config: GraphqlClientConfig): GraphqlClient

Create a new GraphQL client instance.

The client provides methods for executing GraphQL queries, mutations, and subscriptions with automatic error handling and response parsing.

Parameters
Returns

GraphqlClient — A new GraphQL client instance

Examples

Basic query

const client = createGraphqlClient({
  url: "http://localhost:4000/graphql",
});

const response = await client.query(`
  query GetUser($id: ID!) {
    user(id: $id) { id name email }
  }
`, { id: "123" });

console.log(response.data());
await client.close();

Using connection config object

const client = createGraphqlClient({
  url: { host: "api.example.com", port: 443, protocol: "https" },
});

Mutation with error handling

const response = await client.mutation(`
  mutation CreateUser($input: CreateUserInput!) {
    createUser(input: $input) { id }
  }
`, { input: { name: "Alice", email: "alice@example.com" } });

if (response.ok) {
  console.log("Created user:", response.data().createUser.id);
}

Using await using for automatic cleanup

await using client = createGraphqlClient({
  url: "http://localhost:4000/graphql",
});
const response = await client.query(`{ __typename }`);
// Client automatically closed when scope exits
function

#createGraphqlResponse

function createGraphqlResponse<T>(options: GraphqlResponseOptions<T>): GraphqlResponse<T>

Create a GraphqlResponse from parsed response data.

Parameters

Variables

const

#outdent

const outdent: Outdent
Search Documentation