Skip to main content

Actions

The GraphQL connector spans 3 packages:

  • ballerina/graphql
  • ballerina/graphql.dataloader
  • ballerina/graphql.subgraph

Available clients:

ClientPurpose
ClientSends GraphQL queries and mutations to a remote GraphQL endpoint and returns type-safe responses.

For event-driven integration, see the Trigger Reference.


Client​

Sends GraphQL queries and mutations to a remote GraphQL endpoint and returns type-safe responses.

Configuration​

FieldTypeDefaultDescription
authClientAuthConfig?()Authentication configuration. Supports CredentialsConfig, BearerTokenConfig, JwtIssuerConfig, OAuth2ClientCredentialsGrantConfig, OAuth2PasswordGrantConfig, OAuth2RefreshTokenGrantConfig, or OAuth2JwtBearerGrantConfig.
timeoutdecimal60Request timeout in seconds.
retryConfigRetryConfig?()Retry configuration for failed requests.
circuitBreakerCircuitBreakerConfig?()Circuit breaker configuration for fault tolerance.
cacheCacheConfig{}HTTP response cache configuration.
compressionCompressionCOMPRESSION_AUTOCompression setting for requests.
secureSocketClientSecureSocket?()SSL/TLS configuration for HTTPS endpoints.
proxyProxyConfig?()Proxy server configuration.
validationbooleantrueEnable or disable client-side GraphQL document validation.
followRedirectsFollowRedirects?()HTTP redirect following configuration.
cookieConfigCookieConfig?()Cookie management configuration.
responseLimitsResponseLimitConfigs{}Response size limit configuration.
http1SettingsClientHttp1Settings{}HTTP/1.1-specific client settings.
poolConfigPoolConfiguration?()Connection pool configuration.
forwardedstring"disable"Forwarded header handling mode.

Initializing the client​

import ballerina/graphql;

configurable string serviceUrl = ?;

graphql:Client graphqlClient = check new (serviceUrl);

Operations​

Query and mutation execution​

execute

Sends a GraphQL document (query or mutation) to the remote endpoint and returns the response. Supports variables, operation name selection, custom headers, and type-safe response binding.

Parameters:

NameTypeRequiredDescription
documentstringYesThe GraphQL query or mutation document string.
variablesmap<anydata>?NoA map of variable names to values for parameterized queries.
operationNamestring?NoThe name of the operation to execute when the document contains multiple operations.
headersmap<string|string[]>?NoAdditional HTTP headers to include in the request.
targetTypetypedesc<GenericResponseWithErrors|record {}|json>NoThe expected response type for type-safe binding.

Returns: targetType|ClientError

Sample code:

json response = check graphqlClient->execute(
string `query { allLifts { id name status } }`
);

Sample response:

{
"data": {
"allLifts": [
{"id": "astra-express", "name": "Astra Express", "status": "OPEN"},
{"id": "jazz-cat", "name": "Jazz Cat", "status": "OPEN"},
{"id": "jolly-roger", "name": "Jolly Roger", "status": "CLOSED"}
]
}
}