|
| 1 | +# This example shows how @dbquery is configured for custom SQL queries with Customer data. |
| 2 | + |
| 3 | +""" |
| 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. |
| 7 | +""" |
| 8 | +type Customer { |
| 9 | + id: ID! |
| 10 | + name: String |
| 11 | + email: String |
| 12 | +} |
| 13 | + |
| 14 | +""" |
| 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. |
| 18 | +
|
| 19 | +The 'customer' table in PostgreSQL has the following structure: |
| 20 | +
|
| 21 | + CREATE TABLE customer ( |
| 22 | + id SERIAL PRIMARY KEY, -- Unique identifier with sequence |
| 23 | + name CHARACTER(50) NOT NULL, -- Customer's name, max 50 characters |
| 24 | + email CHARACTER(50) NOT NULL, -- Customer's email, max 50 characters, must be unique |
| 25 | + CONSTRAINT customer_email_key UNIQUE (email) -- Unique constraint on email |
| 26 | + ); |
| 27 | +These queries demonstrate basic SQL interactions with this table. |
| 28 | +""" |
| 29 | +type Query { |
| 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 field 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 | + - Maps the `id` argument to the `$1` marker in the SQL query, allowing dynamic ID-based retrieval. |
| 55 | + - `$1` serves as a placeholder, which will be replaced by the provided `id` value during execution. |
| 56 | +
|
| 57 | + **@dbquery Directive Usage**: |
| 58 | + - `query`: This is the SQL query that will be executed. Here, the customer data is fetched based on the provided `id` value. |
| 59 | + - The `$1` marker is a placeholder for the value of the `id` argument, which is passed as a variable when executing the query. |
| 60 | + - `type`: Specifies the type of database being queried (PostgreSQL). |
| 61 | + - `configuration`: References the database configuration (connection details) in StepZen. |
| 62 | +
|
| 63 | + This field requires an `id` argument as input, which is passed as a variable from the GraphQL request. The variable's value is used to fetch the corresponding customer from the database. |
| 64 | + """ |
| 65 | + customer(id: Int!): Customer |
| 66 | + @dbquery( |
| 67 | + query: "SELECT id, name, email FROM customer WHERE id = $1" |
| 68 | + type: "postgresql" |
| 69 | + configuration: "postgresql_config" |
| 70 | + ) |
| 71 | +} |
0 commit comments