|
1 | 1 | # This example shows how @dbquery is configured for custom SQL queries with Customer data.
|
2 | 2 |
|
3 | 3 | """
|
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. |
5 | 7 | """
|
6 | 8 | type Customer {
|
7 |
| - id: ID! |
8 |
| - name: String |
9 |
| - email: String |
| 9 | + id: ID! |
| 10 | + name: String |
| 11 | + email: String |
10 | 12 | }
|
11 | 13 |
|
12 | 14 | """
|
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. |
15 | 18 |
|
16 |
| -Table structure for 'customer': |
| 19 | +The 'customer' table in PostgreSQL has the following structure: |
17 | 20 |
|
18 | 21 | CREATE TABLE customer (
|
19 | 22 | id SERIAL PRIMARY KEY, -- Unique identifier with sequence
|
20 | 23 | name CHARACTER(50) NOT NULL, -- Customer's name, max 50 characters
|
21 | 24 | email CHARACTER(50) NOT NULL, -- Customer's email, max 50 characters, must be unique
|
22 | 25 | CONSTRAINT customer_email_key UNIQUE (email) -- Unique constraint on email
|
23 | 26 | );
|
24 |
| -
|
| 27 | +These queries demonstrate basic SQL interactions with this table. |
25 | 28 | """
|
26 | 29 | 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 | + ) |
42 | 72 | }
|
0 commit comments