Skip to content

Commit ac58f16

Browse files
feat(snippet): add example for @dbquery with SQL queries
1 parent 0808d91 commit ac58f16

File tree

7 files changed

+162
-0
lines changed

7 files changed

+162
-0
lines changed

dbquery/custom-query/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# @dbquery Example Snippet
2+
3+
This example demonstrates how to use the `@dbquery` directive with custom SQL queries for `Customer` data in StepZen.
4+
5+
## Files
6+
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.
12+
13+
## Prerequisites
14+
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.
17+
18+
## Usage
19+
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.

dbquery/custom-query/api.graphql

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# This example shows how @dbquery is configured for custom SQL queries with Customer data.
2+
3+
"""
4+
Sample Customer type.
5+
"""
6+
type Customer {
7+
id: ID!
8+
name: String
9+
email: String
10+
}
11+
12+
"""
13+
`Customer` queries a mock `Customer` object from a demo PostgreSQL database.
14+
The data is fetched using custom SQL queries.
15+
"""
16+
type Query {
17+
# Fetches all customers.
18+
getAllCustomers: [Customer]
19+
@dbquery(
20+
query: "SELECT id, name, email FROM customer"
21+
type: "postgresql"
22+
configuration: "postgresql_config"
23+
)
24+
# Fetches a customer by their ID.
25+
getCustomerById(id: Int!): Customer
26+
@dbquery(
27+
query: "SELECT id, name, email FROM customer WHERE id = $1"
28+
type: "postgresql"
29+
configuration: "postgresql_config"
30+
)
31+
32+
33+
# Searches customers by name using a pattern match.
34+
searchCustomersByName(name: String!): [Customer]
35+
@dbquery(
36+
query: "SELECT id, name, email FROM customer WHERE name LIKE '%' || $1 || '%'"
37+
type: "postgresql"
38+
configuration: "postgresql_config"
39+
)
40+
}
41+
type Mutation {
42+
# Updates a customer's email by their ID.
43+
updateCustomerEmail(id: Int!, newEmail: String!): Customer
44+
@dbquery(
45+
query: """
46+
UPDATE customer
47+
SET email = $2
48+
WHERE id = $1
49+
RETURNING id, name, email
50+
"""
51+
type: "postgresql"
52+
configuration: "postgresql_config"
53+
)
54+
}

dbquery/custom-query/config.yaml

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

dbquery/custom-query/index.graphql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
schema @sdl(files: ["api.graphql"]) {
2+
query: Query
3+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Query to get a customer by ID
2+
query GetCustomerById {
3+
getCustomerById(id: 1) {
4+
id
5+
name
6+
email
7+
}
8+
}
9+
10+
# Query to search customers by name
11+
query SearchCustomersByName {
12+
searchCustomersByName(name: "John") {
13+
id
14+
name
15+
email
16+
}
17+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"endpoint": "api/miscellaneous"
3+
}
4+

dbquery/custom-query/tests/Test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const fs = require("fs");
2+
const path = require("node:path");
3+
const {
4+
deployAndRun,
5+
stepzen,
6+
getTestDescription,
7+
} = require("../../../tests/gqltest.js");
8+
9+
testDescription = getTestDescription("snippets", __dirname);
10+
11+
// Read the GraphQL operations from the operations file
12+
const requestsFile = path.join(path.dirname(__dirname), "operations.graphql");
13+
const requests = fs.readFileSync(requestsFile, "utf8").toString();
14+
15+
describe(testDescription, function () {
16+
const tests = [
17+
{
18+
label: "fetch customer by ID",
19+
query: requests,
20+
operationName: "GetCustomerById",
21+
variables: {
22+
id: 1
23+
},
24+
expected: {
25+
getCustomerById: {
26+
id: "1",
27+
name: "Lucas Bill",
28+
29+
}
30+
},
31+
},
32+
{
33+
label: "search customers by name pattern",
34+
query: requests,
35+
operationName: "SearchCustomersByName",
36+
variables: {
37+
name: "John"
38+
},
39+
expected: {
40+
searchCustomersByName: [
41+
{
42+
id: "2",
43+
name: "John Doe",
44+
45+
},
46+
{
47+
id: "5",
48+
name: "Johnny Smith",
49+
50+
}
51+
]
52+
},
53+
},
54+
];
55+
56+
// Run the tests against the deployed schema
57+
return deployAndRun(__dirname, tests, stepzen.admin);
58+
});

0 commit comments

Comments
 (0)