import ballerina/graphql;

// The `graphql:Service` exposes a GraphQL service on the provided port.
service /graphql on new graphql:Listener(4000) {

    // A resource function inside a `graphql:Service` represents a resolver.
    // This resource can be queried using the `{ greeting }`.
    resource function get greeting() returns string {

        return "Hello, World";
    }
}

Hello World

A GraphQL service in Ballerina represents a GraphQL schema. Each resource function of the graphql:Service represents a resolver function in the root Query type. The return type of each resource function will be the type of each field represented by the resource function.

For more information on the underlying package, see the GraphQL package.

This example shows a simple GraphQL endpoint that has a single field in the root Query type, which returns a string.

import ballerina/graphql;
service /graphql on new graphql:Listener(4000) {

The graphql:Service exposes a GraphQL service on the provided port.

    resource function get greeting() returns string {

A resource function inside a graphql:Service represents a resolver. This resource can be queried using the { greeting }.

        return "Hello, World";
    }
}
bal run graphql_hello_world.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:4000
# Send a query to the GraphQL endpoint using a cURL command.
# The query used: { greeting }
curl -X POST -H "Content-type: application/json" -d '{ "query": "{ greeting }" }' 'http://localhost:4000/graphql'
{"data":{"greeting":"Hello, World"}}