Skip to content

Commit f79403d

Browse files
feat(snippet): add example for @dbquery with SQL queries
1 parent 6ce966e commit f79403d

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

dbquery/custom-query/api.graphql

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,42 @@ type Customer {
1212
"""
1313
`Customer` queries a mock `Customer` object from a demo PostgreSQL database.
1414
The data is fetched using custom SQL queries.
15+
16+
Table structure for 'customer':
17+
18+
CREATE TABLE customer (
19+
id SERIAL PRIMARY KEY, -- Unique identifier with sequence
20+
name CHARACTER(50) NOT NULL, -- Customer's name, max 50 characters
21+
email CHARACTER(50) NOT NULL, -- Customer's email, max 50 characters, must be unique
22+
CONSTRAINT customer_email_key UNIQUE (email) -- Unique constraint on email
23+
);
24+
25+
Foreign key references:
26+
- customeraddress(customerid) references customer(id)
27+
- "order"(customerid) references customer(id)
1528
"""
1629
type Query {
17-
# Fetches all customers.
30+
# Fetches all customers.
1831
getAllCustomers: [Customer]
1932
@dbquery(
20-
query: "SELECT id, name, email FROM customer"
21-
type: "postgresql"
33+
query: "SELECT id, name, email FROM customer",
34+
type: "postgresql",
2235
configuration: "postgresql_config"
2336
)
37+
2438
# Fetches a customer by their ID.
25-
getCustomerById(id: Int!): Customer
39+
getCustomerById(id: Int!): Customer
2640
@dbquery(
27-
query: "SELECT id, name, email FROM customer WHERE id = $1"
28-
type: "postgresql"
41+
query: "SELECT id, name, email FROM customer WHERE id = $1",
42+
type: "postgresql",
2943
configuration: "postgresql_config"
3044
)
3145

32-
3346
# Searches customers by name using a pattern match.
3447
searchCustomersByName(name: String!): [Customer]
3548
@dbquery(
36-
query: "SELECT id, name, email FROM customer WHERE name LIKE '%' || $1 || '%'"
37-
type: "postgresql"
49+
query: "SELECT id, name, email FROM customer WHERE name LIKE '%' || $1 || '%'",
50+
type: "postgresql",
3851
configuration: "postgresql_config"
3952
)
4053
}
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-
}

0 commit comments

Comments
 (0)