Skip to content

Martini Creating a Database Reader GraphQL API

The GraphQL database reader endpoint enables the creation of a GraphQL server that reflects the structure and data of a specified database. This server, once deployed, acts as an API, facilitating operations such as select, insert, update, and delete through GraphQL query syntax.

Properties

General Configuration

The table below outlines the general configuration properties for the GraphQL database reader endpoint:

Property Default Description
Auto Start true Determines if the endpoint should automatically start when the package is initiated.
Database Connection Name (dbConnectionName) (none) Identifies the database connection within the Martini runtime.
Database Name (dbName) (none) Specifies the schema/keyspace name for JDBC, Cassandra, and MongoDB databases.
Document Type Defines the document type for adding documents to Tracker upon endpoint activation.
Log To Tracker false Indicates whether executions should be logged to Tracker.
Name (required) The unique name assigned to the endpoint.
URL (none) The URL at which the GraphQL service will be accessible.

Example

The package.xml configuration for a GraphQL database reader endpoint might look as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<package stateOnStartup="started" id="graphql-methods" version="1.0.0-SNAPSHOT" context-path="/graphql-methods">
 <package-dependencies/>
 <startup-services/>
 <shutdown-services/>
 <endpoints>
 <endpoint type="graphql-db-reader" name="MyEndpoint" service="" enabled="true" modifiable="true">
 <properties>
 <property name="track">false</property>
 <property name="documentType">GraphQL Database Reader</property>
 <property name="replicated">true</property>
 <property name="url">/my/graphql/api</property>
 <property name="dbConnectionName">mysql</property>
 <property name="dbName">sampledb</property>
 </properties>
 </endpoint>
 </endpoints>
</package>

This configuration automatically translates the specified database into a GraphQL schema, including objects, queries, and mutations that clients can interact with.

The resulting schema can be accessed at http://localhost:8080/graphql/mypackage/spacex/schema.graphql.

Schema Methods Overview

The table below provides an overview of the schema methods and their functionalities:

GraphQL Type Schema Method Prefix Functionality
Query get (e.g., getRocket) Retrieves a row from the specified table.
Mutation create (e.g., createRocket) Adds a new row to the specified table.
Mutation update (e.g., updateRocket) Modifies an existing row in the table.
Mutation delete (e.g., deleteRocket) Removes a row from the table.

The GraphQL server can be accessed at http://localhost:8080/api/spacex/graphql, which expects a POST request with a JSON body. For instance, to query a rocket by ID:

1
2
3
{
  "query": "Query{ getRocket(id:1) { id name } }"
}

For testing and development purposes, it's recommended to utilize the built-in HTTP client within Martini for sending requests, providing an integrated and streamlined workflow.

Handling Schema Constraints

The schema definition enforces constraints such as non-nullable fields and foreign key relationships. Violating these constraints results in errors. For example, attempting to insert a row without a required field or deleting a row that violates a foreign key constraint will trigger error messages highlighting the specific issue.

To resolve foreign key constraint violations, ensure that dependent rows are removed or modified appropriately before proceeding with operations that might violate these constraints.