Skip to content

feat(snippet): add example for @dbquery with SQL queries #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions dbquery/custom-query/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# @dbquery SQL query Snippet

This example demonstrates how to use the `@dbquery` directive with custom SQL queries for `Customer` data in StepZen.

## Files

- **api.graphql**: Contains the GraphQL schema for querying `Customer` objects using custom SQL queries.
- **config.yaml**: Database configuration, including connection URI.
- **index.graphql**: Entry point schema file.
- **operations.graphql**: Sample queries to test the `@dbquery` directive.
- **stepzen.config.json**: Configuration for the StepZen endpoint.

## Prerequisites

- A PostgreSQL database with a `customer` table containing `id`, `name`, and `email` fields.
- Replace the `uri` in `config.yaml` with your database connection details.

## Usage

1. Deploy the schema using StepZen.
2. Use the sample queries in `operations.graphql` to test the `@dbquery` functionality.
3. Adjust the queries as needed to fit your database schema.
54 changes: 54 additions & 0 deletions dbquery/custom-query/api.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# This example shows how @dbquery is configured for custom SQL queries with Customer data.

"""
Sample Customer type.
"""
type Customer {
id: ID!
name: String
email: String
}

"""
`Customer` queries a mock `Customer` object from a demo PostgreSQL database.
The data is fetched using custom SQL queries.
"""
type Query {
# Fetches all customers.
getAllCustomers: [Customer]
@dbquery(
query: "SELECT id, name, email FROM customer"
type: "postgresql"
configuration: "postgresql_config"
)
# Fetches a customer by their ID.
getCustomerById(id: Int!): Customer
@dbquery(
query: "SELECT id, name, email FROM customer WHERE id = $1"
type: "postgresql"
configuration: "postgresql_config"
)


# Searches customers by name using a pattern match.
searchCustomersByName(name: String!): [Customer]
@dbquery(
query: "SELECT id, name, email FROM customer WHERE name LIKE '%' || $1 || '%'"
type: "postgresql"
configuration: "postgresql_config"
)
}
type Mutation {
# Updates a customer's email by their ID.
updateCustomerEmail(id: Int!, newEmail: String!): Customer
@dbquery(
query: """
UPDATE customer
SET email = $2
WHERE id = $1
RETURNING id, name, email
"""
type: "postgresql"
configuration: "postgresql_config"
)
}
4 changes: 4 additions & 0 deletions dbquery/custom-query/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
configurationset:
- configuration:
name: postgresql_config
uri: postgresql://postgresql.introspection.stepzen.net/introspection?user=testUserIntrospection&password=HurricaneStartingSample1934
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add missing newline

3 changes: 3 additions & 0 deletions dbquery/custom-query/index.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
schema @sdl(files: ["api.graphql"]) {
query: Query
}
26 changes: 26 additions & 0 deletions dbquery/custom-query/operations.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Query to get a customer by ID
query GetCustomerById {
getCustomerById(id: 1) {
id
name
email
}
}

# Query to search customers by name
query SearchCustomersByName {
searchCustomersByName(name: "John") {
id
name
email
}
}

# Query to get all customers
query GetAllCustomers {
getAllCustomers {
id
name
email
}
}
4 changes: 4 additions & 0 deletions dbquery/custom-query/stepzen.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"endpoint": "api/miscellaneous"
}

74 changes: 74 additions & 0 deletions dbquery/custom-query/tests/Test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const fs = require("fs");
const path = require("node:path");
const {
deployAndRun,
stepzen,
getTestDescription,
} = require("../../../tests/gqltest.js");

testDescription = getTestDescription("snippets", __dirname);

// Read the GraphQL operations from the operations file
const requestsFile = path.join(path.dirname(__dirname), "operations.graphql");
const requests = fs.readFileSync(requestsFile, "utf8").toString();

describe(testDescription, function () {

const tests = [
{
label: "fetch all customers",
query: requests,
operationName: "GetAllCustomers",
variables: {},
expected: {
getAllCustomers: [
{ id: "1", name: "Lucas Bill ", email: "[email protected] " },
{ id: "2", name: "Mandy Jones ", email: "[email protected] " },
{ id: "3", name: "Salim Ali ", email: "[email protected] " },
{ id: "4", name: "Jane Xiu ", email: "[email protected] " },
{ id: "5", name: "John Doe ", email: "[email protected] " },
{ id: "6", name: "Jane Smith ", email: "[email protected] " },
{ id: "7", name: "Sandeep Bhushan ", email: "[email protected] " },
{ id: "8", name: "George Han ", email: "[email protected] " },
{ id: "9", name: "Asha Kumari ", email: "[email protected] " },
{ id: "10", name: "Salma Khan ", email: "[email protected] " }
]
},
},
{
label: "fetch customer by ID",
query: requests,
operationName: "GetCustomerById",
variables: {
id: 1
},
expected: {
getCustomerById: {
id: "1",
name: "Lucas Bill ",
email: "[email protected] "
}
},
},
{
label: "search customers by name pattern",
query: requests,
operationName: "SearchCustomersByName",
variables: {
name: "John"
},
expected: {
searchCustomersByName: [
{
id: "5",
email: "[email protected] ",
name: "John Doe "
}
]
},
},
];

// Run the tests against the deployed schema
return deployAndRun(__dirname, tests, stepzen.admin);
});