Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
GraphQL service - Record as output object
The Ballerina graphql
module allows returning record
types from the resource
or remote
methods of the graphql:Service
. These record
types are mapped to GraphQL output object types in the GraphQL schema in which the type name and the field names are mapped one-to-one from Ballerina to GraphQL. Use a record
type to represent a GraphQL output object type only when all fields of that object type do not have any input arguments or the field resolution does not require any complex logic execution. The record
type is preferred over the service
object type in this case as it makes the code more concise.
import ballerina/graphql;
// Define the custom record types for the returning data.
public type Profile record {|
string name;
int age;
Address address;
|};
public type Address record {|
string number;
string street;
string city;
|};
service /graphql on new graphql:Listener(9090) {
// Ballerina GraphQL resolvers can return `record` values. The record will be mapped to a
// GraphQL output object type in the generated GraphQL schema with the same name and fields.
resource function get profile() returns Profile {
return {
name: "Walter White",
age: 51,
address: {
number: "308",
street: "Negra Arroyo Lane",
city: "Albuquerque"
}
};
}
}
Run the service by executing the following command.
$ bal run graphql_returning_record_values.bal
Send the following document to the GraphQL endpoint to test the service.
{
profile {
name,
address {
city
}
}
}
To send the document, execute the following cURL command in a separate terminal.
$ curl -X POST -H "Content-type: application/json" -d '{ "query": "{ profile { name, address { city } } }" }' 'http://localhost:9090/graphql'{"data":{"profile":{"name":"Walter White", "address":{"city":"Albuquerque"}}}}
Tip: You can invoke the above service via the GraphQL client.