GraphQL via HTTP
SurrealDB provides a powerful HTTP API that allows you to interact with the database programmatically. This API can be used to perform a wide range of database operations, from querying data to modifying records and managing database structures.
The HTTP API is designed to be simple and intuitive, with a RESTful interface that provides a consistent way to interact with the database. You can use the API to perform a wide range of database operations, from querying data to modifying records and managing database structures.
Starting a new connection
Before you can start making queries, you need to start SurrealDB with the GraphQL module enabled. You can do this by starting a new instance of SurrealDB with the surreal start command.
In order to allow querying the created table using GraphQL, you will need to explicitly enable GraphQL using the DEFINE CONFIG statement. This will allow you to query the table using GraphQL on a per-database basis.
POST /sql
To use the GraphQL API, you first need to enable it using the DEFINE CONFIG statement. This will allow you to query the table using GraphQL on a per-database basis.
To do this, you can send a POST request to the /sql endpoint with a RAW body containing the DEFINE CONFIG statement. For example:
Here are three commands to define this configuration, along with some table and field definitions and sample data. They also assume a root user with the name root and the password secret.
POST /graphql
To use the GraphQL API, you can send a POST request to the /graphql endpoint with a JSON body containing the GraphQL query via Postman or any other HTTP client. For example, to query the person table for all records, you can send the following request:
The GraphQL endpoint enables use of GraphQL queries to interact with your data.
Headers
| Header | Description |
|---|---|
Authorization
| Sets the root, namespace, database, or record authentication data |
Accept
| Sets the desired content-type of the response |
surreal-ns
| Sets the selected Namespace for queries |
surreal-db
| Sets the selected Database for queries |