Skip to content

Commit 25089fc

Browse files
fix: Enhance comments in api.graphql to clarify argument-to-variable mapping and @dbquery usage
1 parent fac6e75 commit 25089fc

File tree

3 files changed

+57
-28
lines changed

3 files changed

+57
-28
lines changed

dbquery/custom-query/api.graphql

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,72 @@
11
# This example shows how @dbquery is configured for custom SQL queries with Customer data.
22

33
"""
4-
Sample Customer type.
4+
Represents a customer in the system, stored in the 'customer' table of a PostgreSQL database.
5+
Each customer has an ID, name, and email. The 'Customer' type maps directly to the
6+
corresponding table fields.
57
"""
68
type Customer {
7-
id: ID!
8-
name: String
9-
email: String
9+
id: ID!
10+
name: String
11+
email: String
1012
}
1113

1214
"""
13-
`Customer` queries a mock `Customer` object from a demo PostgreSQL database.
14-
The data is fetched using custom SQL queries.
15+
Defines the root-level queries for fetching customer data.
16+
These queries use the `@dbquery` directive to execute custom SQL queries.
17+
The SQL queries include parameter markers, which correspond to the GraphQL field arguments.
1518
16-
Table structure for 'customer':
19+
The 'customer' table in PostgreSQL has the following structure:
1720
1821
CREATE TABLE customer (
1922
id SERIAL PRIMARY KEY, -- Unique identifier with sequence
2023
name CHARACTER(50) NOT NULL, -- Customer's name, max 50 characters
2124
email CHARACTER(50) NOT NULL, -- Customer's email, max 50 characters, must be unique
2225
CONSTRAINT customer_email_key UNIQUE (email) -- Unique constraint on email
2326
);
24-
27+
These queries demonstrate basic SQL interactions with this table.
2528
"""
2629
type Query {
27-
# Fetches all customers.
28-
customers: [Customer]
29-
@dbquery(
30-
query: "SELECT id, name, email FROM customer",
31-
type: "postgresql",
32-
configuration: "postgresql_config"
33-
)
34-
35-
# Fetches a customer by their ID.
36-
customer(id: Int!): Customer
37-
@dbquery(
38-
query: "SELECT id, name, email FROM customer WHERE id = $1",
39-
type: "postgresql",
40-
configuration: "postgresql_config"
41-
)
30+
"""
31+
Fetches a list of all customers from the database.
32+
33+
The custom SQL query retrieves the `id`, `name`, and `email` fields from the 'customer' table.
34+
35+
**@dbquery Directive Usage**:
36+
- `query`: This is the SQL query that will be executed. Here, it fetches all records from the 'customer' table.
37+
- `type`: Specifies the type of database being queried. In this case, it’s a PostgreSQL database.
38+
- `configuration`: References the database configuration (connection details) in StepZen.
39+
40+
This query does not take any arguments, and hence there are no parameter markers in the SQL query.
41+
The SQL query is static, always returning all customers from the database.
42+
"""
43+
customers: [Customer]
44+
@dbquery(
45+
query: "SELECT id, name, email FROM customer"
46+
type: "postgresql"
47+
configuration: "postgresql_config"
48+
)
49+
50+
"""
51+
Fetches a single customer by their ID from the database.
52+
53+
**Field Argument to Parameter Marker Mapping**:
54+
- The `id` argument provided in the GraphQL query maps to the `$1` marker in the SQL query.
55+
- SQL queries typically use numbered markers (e.g., `$1`, `$2`, etc.) for parameterized inputs. The number refers to the position of the argument in the query.
56+
- When a query is executed, the value passed to the `id` argument in GraphQL (via variables) will replace the `$1` marker in the SQL query.
57+
58+
**@dbquery Directive Usage**:
59+
- `query`: This is the SQL query that will be executed. Here, the customer data is fetched based on the provided `id` value.
60+
- The `$1` marker is a placeholder for the value of the `id` argument, which is passed as a variable when executing the query.
61+
- `type`: Specifies the type of database being queried (PostgreSQL).
62+
- `configuration`: References the database configuration (connection details) in StepZen.
63+
64+
This query requires an `id` argument as input, which is passed as a variable from the GraphQL query. The variable's value is used to fetch the corresponding customer from the database.
65+
"""
66+
customer(id: Int!): Customer
67+
@dbquery(
68+
query: "SELECT id, name, email FROM customer WHERE id = $1"
69+
type: "postgresql"
70+
configuration: "postgresql_config"
71+
)
4272
}

dbquery/custom-query/config.yaml

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

dbquery/custom-query/operations.graphql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Retrieve a specific customer by ID
2-
query Customer {
3-
customer(id: 1) {
1+
# Retrieve a specific customer by ID using a variable
2+
query Customer($id: Int!) {
3+
customer(id: $id) {
44
id
55
name
66
email

0 commit comments

Comments
 (0)