GraphQL Client API Reference
The GraphQL client provides a comprehensive interface for making GraphQL queries and mutations with caching, error handling, and performance optimizations.GraphQLClient
The main GraphQL client class for executing queries and mutations.Constructor
endpoint
(string): GraphQL API endpoint URLoptions
(object, optional): Client configuration options
headers
(object): Default headers to include in requeststimeout
(number): Request timeout in milliseconds (default: 30000)retries
(number): Number of retry attempts (default: 3)cache
(CacheManager): Cache manager instance for caching responsesauth
(AuthManager): Authentication manager for handling authmiddlewares
(array): Array of middleware functions
Methods
query(query, variables, options)
Execute a GraphQL query.query
(string): GraphQL query stringvariables
(object, optional): Query variablesoptions
(object, optional): Request-specific options
mutate(mutation, variables, options)
Execute a GraphQL mutation.mutation
(string): GraphQL mutation stringvariables
(object, optional): Mutation variablesoptions
(object, optional): Request-specific options
subscribe(subscription, variables, options)
Execute a GraphQL subscription (WebSocket-based).subscription
(string): GraphQL subscription stringvariables
(object, optional): Subscription variablesoptions
(object, optional): Subscription options
batch(operations)
Execute multiple GraphQL operations in a single request.operations
(array): Array of operation objects withquery
andvariables
setHeader(name, value)
Set a default header for all requests.name
(string): Header namevalue
(string): Header value
setHeaders(headers)
Set multiple default headers.headers
(object): Object containing header key-value pairs
clearCache()
Clear the client’s cache.getCacheStats()
Get cache statistics.GraphQLQueryBuilder
Builder class for constructing GraphQL queries programmatically.Constructor
Methods
select(field)
Add a field to select.field
(string): Field name to select
selectWithAlias(field, alias)
Add a field with an alias.field
(string): Actual field namealias
(string): Alias for the field
selectObject(field, subBuilder)
Add a nested object selection.field
(string): Object field namesubBuilder
(GraphQLQueryBuilder): Builder for nested selection
withArguments(args)
Add arguments to the current selection.args
(object): Arguments object
withDirective(directive)
Add a directive to the current selection.directive
(string): GraphQL directive
build()
Build the GraphQL query string.buildQuery(operationName, variables)
Build a complete query with operation name and variables.operationName
(string, optional): Operation namevariables
(object, optional): Variables object
GraphQLSchema
Utilities for working with GraphQL schemas.Constructor
schema
(string|object): GraphQL schema SDL string or parsed schema object
Methods
getType(name)
Get a type from the schema.name
(string): Type name
getQueryType()
Get the Query type.getMutationType()
Get the Mutation type.getFields(typeName)
Get fields for a given type.typeName
(string): Type name
validateQuery(query)
Validate a GraphQL query against the schema.query
(string): GraphQL query string
getPossibleTypes(typeName)
Get possible types for a union or interface.typeName
(string): Union or interface type name
createGraphQLMiddleware
Create middleware for handling GraphQL requests.options
(object): Middleware configuration options
endpoint
(string): GraphQL endpoint URLcache
(CacheManager, optional): Cache manager instanceauth
(AuthManager, optional): Authentication managerintrospection
(boolean): Enable GraphQL introspection (default: false)playground
(boolean): Enable GraphQL playground (default: false)
Error Types
GraphQLError
Custom error class for GraphQL-related errors.code
(string): Error codedetails
(object): Additional error details
Common Error Codes
GRAPHQL_VALIDATION_ERROR
: Query validation failedGRAPHQL_EXECUTION_ERROR
: Query execution failedNETWORK_ERROR
: Network request failedAUTHENTICATION_ERROR
: Authentication failedAUTHORIZATION_ERROR
: Authorization failedRATE_LIMIT_ERROR
: Rate limit exceeded