|
1 |
| -# @dbquery SQL query Snippet |
2 | 1 |
|
3 |
| -This example demonstrates how to use the `@dbquery` directive with custom SQL queries for `Customer` data in StepZen. |
| 2 | +# SQL Queries with `@dbquery` |
4 | 3 |
|
5 |
| -## Files |
| 4 | +This snippet demonstrates how to configure the `@dbquery` directive for custom SQL queries. |
6 | 5 |
|
7 |
| -- **api.graphql**: Contains the GraphQL schema for querying `Customer` objects using custom SQL queries. |
8 |
| -- **config.yaml**: Database configuration, including connection URI. |
9 |
| -- **index.graphql**: Entry point schema file. |
10 |
| -- **operations.graphql**: Sample queries to test the `@dbquery` directive. |
11 |
| -- **stepzen.config.json**: Configuration for the StepZen endpoint. |
| 6 | +## Getting Started |
12 | 7 |
|
13 |
| -## Prerequisites |
| 8 | +[Install](https://www.ibm.com/docs/en/api-connect/ace/saas?topic=setting-up-your-environment) the StepZen command line interface. |
14 | 9 |
|
15 |
| -- A PostgreSQL database with a `customer` table containing `id`, `name`, and `email` fields. |
16 |
| -- Replace the `uri` in `config.yaml` with your database connection details. |
| 10 | +To run these examples, |
17 | 11 |
|
18 |
| -## Usage |
| 12 | +- `git clone` this repo |
| 13 | +- Change to the right working directory. |
| 14 | +- run `stepzen deploy` |
19 | 15 |
|
20 |
| -1. Deploy the schema using StepZen. |
21 |
| -2. Use the sample queries in `operations.graphql` to test the `@dbquery` functionality. |
22 |
| -3. Adjust the queries as needed to fit your database schema. |
| 16 | +This example uses a demo database filled with mock data, you can inspect the `config.yaml` file to find the credentials for this database. Also you can configure the connection in the `config.yaml` file by providing your database credentials. |
| 17 | + |
| 18 | +Here’s a more generalized and specific description of the `@dbquery` directive functionality without referring to any particular data: |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## The `@dbquery` Directive with SQL Query |
| 23 | + |
| 24 | +The `@dbquery` directive in StepZen is used to connect your GraphQL API to databases and allows you to execute custom SQL queries within your schema. It supports various database types, such as MySQL, PostgreSQL, MSSQL, and Snowflake. |
| 25 | + |
| 26 | +In this snippet, the `query` argument is used to define custom SQL queries for more control over the data being fetched. The functionality of the `query` argument allows for: |
| 27 | + |
| 28 | +- Running complex SQL queries directly from GraphQL fields. |
| 29 | +- Retrieving data from specific columns or joining multiple tables based on your query requirements. |
| 30 | +- Filtering and querying data using SQL syntax when database field names or structures differ from the GraphQL schema. |
| 31 | + |
| 32 | +The `query` argument provides the flexibility to write any SQL statement, while the **configuration** argument references the connection settings defined in the `config.yaml` file. |
| 33 | + |
| 34 | +For example, a query may look like this: |
| 35 | + |
| 36 | +```graphql |
| 37 | +@dbquery( |
| 38 | + query: "SELECT column1, column2 FROM your_table WHERE condition = $1", |
| 39 | + type: "postgresql", |
| 40 | + configuration: "your_config" |
| 41 | +) |
| 42 | +``` |
| 43 | + |
| 44 | +This allows you to fetch exactly the data you need, based on the custom SQL query provided. You can adjust the queries to match your database schema and use case. |
| 45 | + |
| 46 | +## Try it Out! |
| 47 | + |
| 48 | +Deploy the schema from `dbquery/pagination` relative to the repository's root directory: |
| 49 | + |
| 50 | +``` |
| 51 | +stepzen deploy |
| 52 | +``` |
| 53 | + |
| 54 | +Run the [sample operations](operations.graphql): |
| 55 | + |
| 56 | +- **Fetch all customers**: |
| 57 | + ``` |
| 58 | + stepzen request -f operations.graphql --operation-name=Customers |
| 59 | + ``` |
| 60 | + |
| 61 | +- **Fetch a customer by ID**: |
| 62 | + ``` |
| 63 | + stepzen request -f operations.graphql --operation-name=Customer --var id=1 |
| 64 | + ``` |
| 65 | + |
0 commit comments