@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
AsyncDisposablefor proper cleanup - Template Literals: Re-exports
outdentfor 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
Related Packages
| Package | Description |
|---|---|
@probitas/client |
Core utilities and types |
@probitas/client-http |
HTTP client |
Links
Installation
deno add jsr:@probitas/client-graphqlClasses
#GraphqlError
class GraphqlError extends ClientErrorClientErrorBase GraphQL error class.
| Name | Description |
|---|---|
name | — |
kind | — |
errors | GraphQL errors from response |
response | Associated GraphQL response (if available) |
Constructor
new GraphqlError(message: string, errors: readonly GraphqlErrorItem[], options?: GraphqlErrorOptions)Properties
- readonly
namestring - readonly
kind"graphql" GraphQL errors from response
Associated GraphQL response (if available)
#GraphqlExecutionError
class GraphqlExecutionError extends GraphqlErrorGraphqlErrorError thrown for GraphQL execution errors.
| Name | Description |
|---|---|
name | — |
Constructor
new GraphqlExecutionError(errors: readonly GraphqlErrorItem[], options?: GraphqlErrorOptions)Properties
- readonly
namestring
#GraphqlNetworkError
class GraphqlNetworkError extends GraphqlErrorGraphqlErrorError thrown for network/HTTP errors before GraphQL processing.
| Name | Description |
|---|---|
name | — |
Constructor
new GraphqlNetworkError(message: string, options?: ErrorOptions)Properties
- readonly
namestring
#GraphqlValidationError
class GraphqlValidationError extends GraphqlErrorGraphqlErrorError thrown for GraphQL validation errors.
| Name | Description |
|---|---|
name | — |
Constructor
new GraphqlValidationError(errors: readonly GraphqlErrorItem[], options?: GraphqlErrorOptions)Properties
- readonly
namestring
Interfaces
#GraphqlClient
interface GraphqlClient extends AsyncDisposableGraphQL client interface.
| Name | Description |
|---|---|
config | Client 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
Client configuration
Methods
query<TData = any, TVariables = Record<string, any>>(query: string, variables?: TVariables, options?: GraphqlOptions): Promise<GraphqlResponse<TData>>Execute a GraphQL query
Parameters
querystringvariables?TVariablesoptions?GraphqlOptions
mutation<TData = any, TVariables = Record<string, any>>(mutation: string, variables?: TVariables, options?: GraphqlOptions): Promise<GraphqlResponse<TData>>Execute a GraphQL mutation
Parameters
mutationstringvariables?TVariablesoptions?GraphqlOptions
execute<TData = any, TVariables = Record<string, any>>(document: string, variables?: TVariables, options?: GraphqlOptions): Promise<GraphqlResponse<TData>>Execute a GraphQL document (query or mutation)
Parameters
documentstringvariables?TVariablesoptions?GraphqlOptions
subscribe<TData = any, TVariables = Record<string, any>>(document: string, variables?: TVariables, options?: GraphqlOptions): AsyncIterable<GraphqlResponse<TData>>Subscribe to a GraphQL subscription via WebSocket
Parameters
documentstringvariables?TVariablesoptions?GraphqlOptions
close(): Promise<void>Close the client and release resources
#GraphqlClientConfig
interface GraphqlClientConfig extends CommonOptionsGraphQL client configuration.
| Name | Description |
|---|---|
url | GraphQL endpoint URL. |
headers | Default headers for all requests |
wsEndpoint | WebSocket endpoint URL (for subscriptions) |
fetch | Custom fetch implementation (for testing/mocking) |
throwOnError | Whether to throw GraphqlError when response contains errors. |
Properties
GraphQL endpoint URL.
Can be a URL string or a connection configuration object.
- readonly
headers?Record<string, string>Default headers for all requests
- readonly
wsEndpoint?stringWebSocket endpoint URL (for subscriptions)
- readonly
fetch?fetchCustom fetch implementation (for testing/mocking)
- readonly
throwOnError?booleanWhether to throw GraphqlError when response contains errors. Can be overridden per-request via GraphqlOptions.
#GraphqlConnectionConfig
interface GraphqlConnectionConfig extends CommonConnectionConfigGraphQL connection configuration.
Extends CommonConnectionConfig with GraphQL-specific options.
Properties
- readonly
protocol?"http" | "https"Protocol to use.
- readonly
path?stringGraphQL endpoint path.
#GraphqlErrorItem
interface GraphqlErrorItemGraphQL error item as per GraphQL specification.
| Name | Description |
|---|---|
message | Error message |
locations | Location(s) in the GraphQL document where the error occurred |
path | Path to the field that caused the error |
extensions | Additional error metadata |
Properties
- readonly
messagestringError message
- readonly
locations?readonly { line: number; column: number }[]Location(s) in the GraphQL document where the error occurred
- readonly
path?readonly unknown[]Path to the field that caused the error
- readonly
extensions?Record<string, unknown>Additional error metadata
#GraphqlErrorOptions
interface GraphqlErrorOptions extends ErrorOptionsOptions for GraphqlError constructor.
| Name | Description |
|---|---|
response | Associated GraphQL response |
Properties
Associated GraphQL response
#GraphqlOptions
interface GraphqlOptions extends CommonOptionsOptions for individual GraphQL requests.
| Name | Description |
|---|---|
headers | Additional request headers |
operationName | Operation name (for documents with multiple operations) |
throwOnError | Whether to throw GraphqlError when response contains errors. |
Properties
- readonly
headers?Record<string, string>Additional request headers
- readonly
operationName?stringOperation name (for documents with multiple operations)
- readonly
throwOnError?booleanWhether to throw GraphqlError when response contains errors.
#GraphqlResponse
interface GraphqlResponse<T = any>GraphQL response interface with pre-loaded body.
| Name | Description |
|---|---|
type | Result type identifier |
ok | Whether the request was successful (no errors) |
errors | GraphQL errors array (null if no errors) |
extensions | Response extensions |
duration | Response time in milliseconds |
status | HTTP status code |
headers | Headers from the HTTP response |
raw | Raw Web standard Response (for streaming or special cases) |
data() | Get response data (null if no data). |
Properties
- readonly
type"graphql"Result type identifier
- readonly
okbooleanWhether the request was successful (no errors)
GraphQL errors array (null if no errors)
- readonly
extensions?Record<string, unknown>Response extensions
- readonly
durationnumberResponse time in milliseconds
- readonly
statusnumberHTTP status code
- readonly
headersHeadersHeaders from the HTTP response
- readonly
rawglobalThis.ResponseRaw Web standard Response (for streaming or special cases)
Methods
data<U = T>(): U | nullGet response data (null if no data). Does not throw even if errors are present.
#GraphqlResponseOptions
interface GraphqlResponseOptions<T>Options for creating a GraphqlResponse.
Properties
- readonly
dataT | null - readonly
extensions?Record<string, unknown> - readonly
durationnumber - readonly
statusnumber - readonly
rawglobalThis.Response
Functions
#createGraphqlClient
function createGraphqlClient(config: GraphqlClientConfig): GraphqlClientCreate a new GraphQL client instance.
The client provides methods for executing GraphQL queries, mutations, and subscriptions with automatic error handling and response parsing.
Parameters
configGraphqlClientConfig- Client configuration including URL and default options
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
#createGraphqlResponse
function createGraphqlResponse<T>(options: GraphqlResponseOptions<T>): GraphqlResponse<T>Create a GraphqlResponse from parsed response data.
Parameters
optionsGraphqlResponseOptions<T>
Variables
#outdent
const outdent: Outdent