dataservice.graphql
Module dataservice.graphql
API
Declarations
Definitions
ballerina/dataservice.graphql Ballerina library
Ballerina Data Service GraphQL Library
Overview
The ballerina/dataservice.graphql package is the GraphQL runtime of the data services that bal persist generates from a persist model. Generated GraphQL services call it to build their queries, map errors and check auth, so fixes reach them without regeneration.
Key Features
- Filter and sort input types shared by every generated service, and the queries of lists and relations built from them
- GraphQL errors that carry the data service error category in
extensions.code - An authenticator that checks Basic, JWT and OAuth2 credentials before a document is parsed, and the scope checks of each root field and relation
- The runtime settings: port, listener, CORS, query depth and complexity limits, auth and page sizes
Quick start
The package is used by generated code, not imported by hand. To generate a GraphQL data service:
-
Add the persist entry with a GraphQL data service to the package:
bal persist add --datastore h2 --dataservice graphql -
Annotate each entity to expose in
persist/model.balwith@dataservice:Expose. -
Build and run the package. The service is served on port
9090under/graphql, with GraphiQL at/graphiql:bal run curl localhost:9090/graphql -H 'Content-Type: application/json' -d '{"query": "{ employees { items { id name } hasMore } }"}'
A field that fails is null, with an entry in errors that names the error category:
{"errors": [{"message": "A record with the key '99' does not exist for the entity 'Employee'.", "path": ["employee"], "extensions": {"code": "NOT_FOUND"}}], "data": {"employee": null}}
For the details, see the specification.
Functions
checkFinite
Checks that an input float is a finite number. ballerina/graphql takes a literal such as 1e400 as Infinity.
Parameters
- value float - The value
- entity string - The name of the entity
- 'field string - The name of the field
Return Type
- Error? - The
INVALID_INPUTerror when the value isNaNor infinite
checkLimits
function checkLimits(ServiceConfig|AuthServiceConfig config) returns error?Checks the limits of a service at startup, so that a value out of range stops the program.
Parameters
- config ServiceConfig|AuthServiceConfig - The settings of the service
Return Type
- error? - An error naming the first limit below 1
checkRelationScopes
function checkRelationScopes(Context ctx, Field 'field, string entity, map<RelationScopes> relations, string[]? entityScopes) returns Error?Checks that the caller may read each relation that a root field's selection reads, once, before the read. Only the relations of the root field's own records are checked, since a related record has no relation fields.
Parameters
- ctx Context - The request context, which holds the caller
- 'field Field - The root field
- entity string - The name of the entity the root field returns
- relations map<RelationScopes> - The related entity and its
readscopes, by relation field
- entityScopes string[]? (default ()) - For a mutation's result, the entity's own
readscopes, which a selected relation also needs
Return Type
- Error? - The
FORBIDDENerror for the first relation the caller cannot read
checkScopes
Checks that the caller holds one of the scopes of a root field's action.
Parameters
- ctx Context - The request context, which holds the caller
- scopes string[] - The scopes of the action; an empty list takes the provider's scopes
Return Type
- Error? - The
FORBIDDENerror when the caller holds none
hasScope
Tells whether the caller holds one of the scopes, as patch and delete check before they return the record.
Parameters
- ctx Context - The request context, which holds the caller
- scopes string[] - The scopes; an empty list takes the provider's scopes
Return Type
- boolean -
truewhen the caller holds one
toCreateError
Maps an error from creating records, or from reading the created records back, to its error.
Return Type
- Error - The
CONFLICT,CONSTRAINT_VIOLATIONorINTERNALerror
toDeleteError
Maps an error from deleting a record to its error.
Parameters
- err error - The error the persist client returned
- entity string - The name of the entity
- key KeyValue[] - The values of the record's key fields, in key order
Return Type
- Error - The
NOT_FOUND,CONSTRAINT_VIOLATIONorINTERNALerror
toError
function toError(DataserviceError body) returns ErrorMaps an error body of ballerina/dataservice, such as from dataservice:checkCreateSize, to its error.
Parameters
- body DataserviceError - The error body
Return Type
- Error - The error, with the body's message and code
toFinite
Returns a stored float, which JSON cannot hold when it is NaN or infinite.
toGetError
Maps an error from reading one record to the NOT_FOUND or INTERNAL error.
Parameters
- err error - The error the persist client returned
- entity string - The name of the entity
- key KeyValue[] - The values of the record's key fields, in key order
- operation string (default "get one") - The name of the operation, which is logged with the error
Return Type
- Error - The
NOT_FOUNDorINTERNALerror
toInvalidInput
function toInvalidInput(ConversionError err, string entity, string 'field) returns ErrorMaps a value that is not in its format to the INVALID_INPUT error.
Parameters
- err ConversionError - The error from the
ballerina/dataserviceconversion
- entity string - The name of the entity
- 'field string - The name of the field
Return Type
- Error - The
INVALID_INPUTerror
toKeyOrder
function toKeyOrder(map<anydata>[] rows, Entity entity, string relation, map<anydata>[] links) returns (map<anydata>?)[]Puts the related records of a dataloader batch in the order of its links.
Parameters
- rows map<anydata>[] - The related records, as
dataservice:toIncludeQueryread them
- entity Entity - The entity description of the records that hold the relation
- relation string - The relation field, a to-one relation
- links map<anydata>[] - The links of the batch: records holding the fields that link the relation
Return Type
- (map<anydata>?)[] - The related record of each link, or
()for a link without one
toListError
Maps an error from reading many records to the INTERNAL error, and logs it.
Parameters
- err error - The error the persist client returned
- entity string - The name of the entity
- operation string (default "get many") - The name of the operation, which is logged with the error
Return Type
- Error - The
INTERNALerror
toQuery
function toQuery(Entity entity, PageConfig config, record {}? filter, record {}[]? sort, int? size, int? offset) returns Query|ErrorChecks the filter, sort and page arguments of a list and builds its query, through dataservice:toQuery.
Parameters
- entity Entity - The entity description
- config PageConfig - The page sizes of the data service
- filter record {}? - The
filterargument
- sort record {}[]? - The
sortargument, each entry naming one field
- size int? - The
sizeargument
- offset int? - The
offsetargument
toRelatedQuery
function toRelatedQuery(Entity entity, string relation, map<anydata> row, PageConfig config, record {}? filter, record {}[]? sort, int? size, int? offset) returns Query|ErrorChecks the filter, sort and page arguments of a to-many relation and builds the query of one record's related
records, through dataservice:toRelatedQuery.
Parameters
- entity Entity - The entity description of the record
- relation string - The relation field
- row map<anydata> - The record, holding at least the fields that link the relation
- config PageConfig - The page sizes of the data service
- filter record {}? - The
filterargument
- sort record {}[]? - The
sortargument, each entry naming one field
- size int? - The
sizeargument
- offset int? - The
offsetargument
toSetAndUnsetError
Maps a patch that both sets and unsets a field to the INVALID_INPUT error.
Return Type
- Error - The
INVALID_INPUTerror
toUpdateError
Maps an error from updating a record to its error. An update that is saved but cannot be read back has no error, and is logged.
Parameters
- err error - The error the persist client returned
- entity string - The name of the entity
- key KeyValue[] - The values of the record's key fields, in key order
Return Type
- Error? - The
NOT_FOUND,CONFLICT,CONSTRAINT_VIOLATIONorINTERNALerror, or()
Classes
dataservice.graphql: Authenticator
Authenticates each request of a data service with auth, before ballerina/graphql parses and validates its document.
Constructor
Creates the authenticator from the configured providers.
init (ListenerAuthConfig[] providers)- providers ListenerAuthConfig[] - The authentication providers, such as a JWT validator
authenticate
function authenticate(Context ctx, Request request) returns AuthnError|AuthzError?Authenticates a request and keeps the caller in the context, as contextInit calls it.
Return Type
- AuthnError|AuthzError? -
graphql:AuthnErrorwithout a valid credential,graphql:AuthzErrorfor a request other thanPOST
Enums
dataservice.graphql: SortDirection
The direction of a sort entry.
Members
Records
dataservice.graphql: AuthConfig
The runtime settings of a generated GraphQL data service with auth.
Fields
- Fields Included from *Config
- listenerConfig ListenerConfiguration(default {}) - The other settings of the listener, such as TLS
- serviceConfig AuthServiceConfig - The settings of the service, with the authentication providers
dataservice.graphql: AuthServiceConfig
The settings of a generated service with auth.
Fields
- Fields Included from *ServiceConfig
- cors CorsConfig
- maxQueryDepth int
- maxQueryComplexity int
- auth ListenerAuthConfig[] - The authentication providers, such as a JWT validator
dataservice.graphql: BooleanFilter
The conditions on a boolean field; every condition set must hold.
Fields
- eq boolean? - Equal to the value
- ne boolean? - Not equal to the value
- anyOf boolean[]? - Equal to any of the values
dataservice.graphql: BooleanNullableFilter
The conditions on a nullable boolean field. A null field matches only isNull.
Fields
- eq boolean? - Equal to the value
- ne boolean? - Not equal to the value; a null field does not match
- anyOf boolean[]? - Equal to any of the values
- isNull boolean? -
truefor null,falsefor not null
dataservice.graphql: CivilFilter
The conditions on a time:Civil field, as ISO 8601 date-times; every condition set must hold.
Fields
- eq string? - Equal to the date-time
- ne string? - Not equal to the date-time
- lt string? - Less than the date-time
- lte string? - At most the date-time
- gt string? - Greater than the date-time
- gte string? - At least the date-time
- anyOf string[]? - Equal to any of the date-times
dataservice.graphql: CivilNullableFilter
The conditions on a nullable time:Civil field, as ISO 8601 date-times. A null field matches only isNull.
Fields
- eq string? - Equal to the date-time
- ne string? - Not equal to the date-time; a null field does not match
- lt string? - Less than the date-time
- lte string? - At most the date-time
- gt string? - Greater than the date-time
- gte string? - At least the date-time
- anyOf string[]? - Equal to any of the date-times
- isNull boolean? -
truefor null,falsefor not null
dataservice.graphql: Config
The runtime settings of a generated GraphQL data service, the type of its configurable dataserviceConfig.
Fields
- Fields Included from *Config
- listenerConfig ListenerConfiguration(default {}) - The other settings of the listener, such as TLS
- serviceConfig ServiceConfig(default {}) - The settings of the service
dataservice.graphql: DateFilter
The conditions on a time:Date field, as ISO 8601 dates; every condition set must hold.
Fields
- eq string? - Equal to the date
- ne string? - Not equal to the date
- lt string? - Less than the date
- lte string? - At most the date
- gt string? - Greater than the date
- gte string? - At least the date
- anyOf string[]? - Equal to any of the dates
dataservice.graphql: DateNullableFilter
The conditions on a nullable time:Date field, as ISO 8601 dates. A null field matches only isNull.
Fields
- eq string? - Equal to the date
- ne string? - Not equal to the date; a null field does not match
- lt string? - Less than the date
- lte string? - At most the date
- gt string? - Greater than the date
- gte string? - At least the date
- anyOf string[]? - Equal to any of the dates
- isNull boolean? -
truefor null,falsefor not null
dataservice.graphql: DecimalFilter
The conditions on a decimal field; every condition set must hold.
Fields
- eq decimal? - Equal to the value
- ne decimal? - Not equal to the value
- lt decimal? - Less than the value
- lte decimal? - At most the value
- gt decimal? - Greater than the value
- gte decimal? - At least the value
- anyOf decimal[]? - Equal to any of the values
dataservice.graphql: DecimalNullableFilter
The conditions on a nullable decimal field. A null field matches only isNull.
Fields
- eq decimal? - Equal to the value
- ne decimal? - Not equal to the value; a null field does not match
- lt decimal? - Less than the value
- lte decimal? - At most the value
- gt decimal? - Greater than the value
- gte decimal? - At least the value
- anyOf decimal[]? - Equal to any of the values
- isNull boolean? -
truefor null,falsefor not null
dataservice.graphql: FloatFilter
The conditions on a float field; every condition set must hold.
Fields
- eq float? - Equal to the value
- ne float? - Not equal to the value
- lt float? - Less than the value
- lte float? - At most the value
- gt float? - Greater than the value
- gte float? - At least the value
- anyOf float[]? - Equal to any of the values
dataservice.graphql: FloatNullableFilter
The conditions on a nullable float field. A null field matches only isNull.
Fields
- eq float? - Equal to the value
- ne float? - Not equal to the value; a null field does not match
- lt float? - Less than the value
- lte float? - At most the value
- gt float? - Greater than the value
- gte float? - At least the value
- anyOf float[]? - Equal to any of the values
- isNull boolean? -
truefor null,falsefor not null
dataservice.graphql: IntFilter
The conditions on a int field; every condition set must hold.
Fields
- eq int? - Equal to the value
- ne int? - Not equal to the value
- lt int? - Less than the value
- lte int? - At most the value
- gt int? - Greater than the value
- gte int? - At least the value
- anyOf int[]? - Equal to any of the values
dataservice.graphql: IntNullableFilter
The conditions on a nullable int field. A null field matches only isNull.
Fields
- eq int? - Equal to the value
- ne int? - Not equal to the value; a null field does not match
- lt int? - Less than the value
- lte int? - At most the value
- gt int? - Greater than the value
- gte int? - At least the value
- anyOf int[]? - Equal to any of the values
- isNull boolean? -
truefor null,falsefor not null
dataservice.graphql: RelationScopes
The related entity of a relation and its read scopes, which a caller needs to read the relation.
Fields
- target string - The name of the related entity
- scopes string[] - The
readscopes of the related entity
dataservice.graphql: ServiceConfig
The settings of the generated service that vary by environment.
Fields
- cors CorsConfig(default {}) - The CORS settings; no cross-origin request is allowed by default
- maxQueryDepth int(default 15) - The deepest document the service runs
- maxQueryComplexity int(default 100) - The largest complexity of a document the service runs
dataservice.graphql: StringFilter
The conditions on a string field; every condition set must hold.
Fields
- eq string? - Equal to the value
- ne string? - Not equal to the value
- lt string? - Less than the value
- lte string? - At most the value
- gt string? - Greater than the value
- gte string? - At least the value
- anyOf string[]? - Equal to any of the values
- contains string? - Contains the value, following the database's collation
- startsWith string? - Starts with the value, following the database's collation
dataservice.graphql: StringNullableFilter
The conditions on a nullable string field. A null field matches only isNull.
Fields
- eq string? - Equal to the value
- ne string? - Not equal to the value; a null field does not match
- lt string? - Less than the value
- lte string? - At most the value
- gt string? - Greater than the value
- gte string? - At least the value
- anyOf string[]? - Equal to any of the values
- contains string? - Contains the value, following the database's collation
- startsWith string? - Starts with the value, following the database's collation
- isNull boolean? -
truefor null,falsefor not null
dataservice.graphql: TimeOfDayFilter
The conditions on a time:TimeOfDay field, as ISO 8601 times; every condition set must hold.
Fields
- eq string? - Equal to the time
- ne string? - Not equal to the time
- lt string? - Less than the time
- lte string? - At most the time
- gt string? - Greater than the time
- gte string? - At least the time
- anyOf string[]? - Equal to any of the times
dataservice.graphql: TimeOfDayNullableFilter
The conditions on a nullable time:TimeOfDay field, as ISO 8601 times. A null field matches only isNull.
Fields
- eq string? - Equal to the time
- ne string? - Not equal to the time; a null field does not match
- lt string? - Less than the time
- lte string? - At most the time
- gt string? - Greater than the time
- gte string? - At least the time
- anyOf string[]? - Equal to any of the times
- isNull boolean? -
truefor null,falsefor not null
dataservice.graphql: UtcFilter
The conditions on a time:Utc field, as ISO 8601 date-times; every condition set must hold.
Fields
- eq string? - Equal to the date-time
- ne string? - Not equal to the date-time
- lt string? - Less than the date-time
- lte string? - At most the date-time
- gt string? - Greater than the date-time
- gte string? - At least the date-time
- anyOf string[]? - Equal to any of the date-times
dataservice.graphql: UtcNullableFilter
The conditions on a nullable time:Utc field, as ISO 8601 date-times. A null field matches only isNull.
Fields
- eq string? - Equal to the date-time
- ne string? - Not equal to the date-time; a null field does not match
- lt string? - Less than the date-time
- lte string? - At most the date-time
- gt string? - Greater than the date-time
- gte string? - At least the date-time
- anyOf string[]? - Equal to any of the date-times
- isNull boolean? -
truefor null,falsefor not null
Import
import ballerina/dataservice.graphql;Other versions
0.1.0
Metadata
Released date: about 20 hours ago
Version: 0.1.0
License: Apache-2.0
Security
No known vulnerabilities in libraries
Checked on 2026-11-27 via OSV.dev
Compatibility
Platform: any
Ballerina version: 2201.14.0
GraalVM compatible: Yes
Pull count
Total: 0
Current verison: 0
Weekly downloads
Keywords
dataservice
persist
graphql
Type/Library
Area/Database
Name/Data Service GraphQL
Contributors