Skip to content

Commit 301ebce

Browse files
chore: readme updated with specific details
1 parent f79403d commit 301ebce

File tree

5 files changed

+70
-63
lines changed

5 files changed

+70
-63
lines changed

dbquery/custom-query/README.md

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,65 @@
1-
# @dbquery SQL query Snippet
21

3-
This example demonstrates how to use the `@dbquery` directive with custom SQL queries for `Customer` data in StepZen.
2+
# SQL Queries with `@dbquery`
43

5-
## Files
4+
This snippet demonstrates how to configure the `@dbquery` directive for custom SQL queries.
65

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
127

13-
## Prerequisites
8+
[Install](https://www.ibm.com/docs/en/api-connect/ace/saas?topic=setting-up-your-environment) the StepZen command line interface.
149

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,
1711

18-
## Usage
12+
- `git clone` this repo
13+
- Change to the right working directory.
14+
- run `stepzen deploy`
1915

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+

dbquery/custom-query/api.graphql

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,21 @@ Table structure for 'customer':
2222
CONSTRAINT customer_email_key UNIQUE (email) -- Unique constraint on email
2323
);
2424
25-
Foreign key references:
26-
- customeraddress(customerid) references customer(id)
27-
- "order"(customerid) references customer(id)
2825
"""
2926
type Query {
3027
# Fetches all customers.
31-
getAllCustomers: [Customer]
28+
customers: [Customer]
3229
@dbquery(
3330
query: "SELECT id, name, email FROM customer",
3431
type: "postgresql",
3532
configuration: "postgresql_config"
3633
)
3734

3835
# Fetches a customer by their ID.
39-
getCustomerById(id: Int!): Customer
36+
customer(id: Int!): Customer
4037
@dbquery(
4138
query: "SELECT id, name, email FROM customer WHERE id = $1",
4239
type: "postgresql",
4340
configuration: "postgresql_config"
4441
)
45-
46-
# Searches customers by name using a pattern match.
47-
searchCustomersByName(name: String!): [Customer]
48-
@dbquery(
49-
query: "SELECT id, name, email FROM customer WHERE name LIKE '%' || $1 || '%'",
50-
type: "postgresql",
51-
configuration: "postgresql_config"
52-
)
5342
}

dbquery/custom-query/config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
configurationset:
22
- configuration:
33
name: postgresql_config
4-
uri: postgresql://postgresql.introspection.stepzen.net/introspection?user=testUserIntrospection&password=HurricaneStartingSample1934
4+
uri: postgresql://postgresql.introspection.stepzen.net/introspection?user=testUserIntrospection&password=HurricaneStartingSample1934
5+

dbquery/custom-query/operations.graphql

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
1-
# Query to get a customer by ID
2-
query GetCustomerById {
3-
getCustomerById(id: 1) {
1+
# Retrieve a specific customer by ID
2+
query Customer {
3+
customer(id: 1) {
44
id
55
name
66
email
77
}
88
}
99

10-
# Query to search customers by name
11-
query SearchCustomersByName {
12-
searchCustomersByName(name: "John") {
13-
id
14-
name
15-
email
16-
}
17-
}
18-
19-
# Query to get all customers
20-
query GetAllCustomers {
21-
getAllCustomers {
10+
# Retrieve all customers in the system
11+
query Customers {
12+
customers {
2213
id
2314
name
2415
email

dbquery/custom-query/tests/Test.js

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe(testDescription, function () {
1818
{
1919
label: "fetch all customers",
2020
query: requests,
21-
operationName: "GetAllCustomers",
21+
operationName: "Customers",
2222
variables: {},
2323
expected: {
2424
getAllCustomers: [
@@ -38,7 +38,7 @@ describe(testDescription, function () {
3838
{
3939
label: "fetch customer by ID",
4040
query: requests,
41-
operationName: "GetCustomerById",
41+
operationName: "Customer",
4242
variables: {
4343
id: 1
4444
},
@@ -50,23 +50,6 @@ describe(testDescription, function () {
5050
}
5151
},
5252
},
53-
{
54-
label: "search customers by name pattern",
55-
query: requests,
56-
operationName: "SearchCustomersByName",
57-
variables: {
58-
name: "John"
59-
},
60-
expected: {
61-
searchCustomersByName: [
62-
{
63-
id: "5",
64-
email: "[email protected] ",
65-
name: "John Doe "
66-
}
67-
]
68-
},
69-
},
7053
];
7154

7255
// Run the tests against the deployed schema

0 commit comments

Comments
 (0)