Tutorial: Create a GraphQL API for a Db2 database
Create a GraphQL API for a Db2 database by letting API Connect for GraphQL introspect the database and generate a GraphQL schema.
This tutorial uses the CLI command stepzen
import db2
to introspect a backend Db2 database and generate a GraphQL schema.
Tip: You can alternatively write your schema in a
.graphql
GraphQL
Schema Definition Language (SDL) file, using the GraphQL directive @dbquery
to
connect to the Db2 database. See Connecting a Db2 database for an
example.Generate the GraphQL Schema
The API Connect for GraphQL CLI can create a GraphQL API that connects data from a database backend.
- Login to API Connect for GraphQL. See Setting up your environment for steps.
- Run the following CLI command on your local machine to import schema:
stepzen import db2 jdbc:db2://<host>:<port>/<database>:user=<user>;password=<password>;sslConnection=<true/false>;currentSchema=<schema>;
Note: Ensure that the JDBC URL that connects to your Db2 database is valid.The command starts creating a GraphQL API that connects the data from your Db2 database, and prompts you for information as shown in the following steps.
- Specify the preferred endpoint where you want your GraphQL deployed. The CLI prompts with a
default name for your endpoint (in this example, called
api/binging-puma
), but you can change it:? What would you like your endpoint to be called? (api/binging-puma)
The command creates a new directory that contains a
stepzen.config.json
file, which in turn contains your project's CLI configuration. The configuration includes the name of your endpoint, in this case, (api/binging-puma
):Created /Users/path/to/project-folder/stepzen.config.json Downloading from StepZen...... done
- Deploy schema to
StepZen
.stepzen deploy
- Once the deployment is complete, test your GraphQL API using Postman or API Connect Essentials Dashboard.