-
Notifications
You must be signed in to change notification settings - Fork 5
add dbquery filter example #92
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
Open
ddebrunner
wants to merge
1
commit into
main
Choose a base branch
from
dbquery-filter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,6 @@ To run these examples, | |
|
|
||
| This example uses a demo database filled with mock data, you can inspect the `config.yaml` file to find the credentials for this database. | ||
|
|
||
|
|
||
| ## GraphQL pagination | ||
|
|
||
| StepZen supports pagination through the standard [GraphQL Cursor Connection Specification](https://relay.dev/graphql/connections.htm). | ||
|
|
@@ -63,4 +62,51 @@ Fetch the next five, the value of `after` is taken from the `endCursor` field of | |
| stepzen request -f operations.graphql --operation-name=Customers --var first=5 --var after="eyJjIjoiTDpRdWVyeTpjdXN0b21lcnMiLCJvIjo0fQ==" | ||
| ``` | ||
|
|
||
| ## GraphQL filtering | ||
|
|
||
| Filtering using a "MongoDB" inspired style for GraphQL is supported with `@dbquery` and is typically | ||
| used with pagination, though it can be used alone. [Reference](https://www.ibm.com/docs/en/api-connect-graphql/saas?topic=directives-directive-dbquery#filtering__title__1) | ||
|
|
||
| The field `Query.lookup` in [`filter.graphql`](./filter.graphql) is similar to `Query.customers` but adds a `filter` argument. | ||
|
|
||
| This filter value would return all names that start with "S": | ||
|
|
||
| ```json | ||
| { "name": { "like": "S%" } } | ||
| ``` | ||
|
|
||
| This returns two customers using an OR clause: | ||
|
|
||
| ```json | ||
| { | ||
| "or": { | ||
| "name": { "eq": "John Doe" }, | ||
| "email": { "eq": "[email protected]" } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| With no filter all customers are returned (subject to pagination) | ||
|
|
||
| ``` | ||
| stepzen request -f operations.graphql --operation-name=Lookup | ||
| ``` | ||
|
|
||
| With a filter then only matching customers are returned. | ||
|
|
||
| ``` | ||
| stepzen request -f operations.graphql --operation-name=Lookup --var 'f={ "name": { "like": "S%" } }' | ||
| ``` | ||
|
|
||
| ``` | ||
| stepzen request -f operations.graphql --operation-name=Lookup --var first=5 --var 'f={ | ||
| "or": { | ||
| "name": { "eq": "John Doe" }, | ||
| "email": { "eq": "[email protected]" } | ||
| } | ||
| }' | ||
| ``` | ||
|
|
||
| Also note that `totalEdges` returns the number of customers (edges) in the connection subject to the filter. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # This shows how filtering is supported with @dbquery | ||
|
|
||
| input CustomerFilter { | ||
| id: ID | ||
| name: StringFilter | ||
| email: StringFilter | ||
| or: CustomerFilter | ||
| } | ||
|
|
||
| input StringFilter { | ||
| eq: String | ||
| ne: String | ||
| like: String | ||
| ilike: String | ||
| gt: String | ||
| le: String | ||
| } | ||
|
|
||
| extend type Query { | ||
| """ | ||
| `lookup` pages through mock `Customer` objects in a demo PostgreSQL database. | ||
| The data is exposed using standard GraphQL pagination using the connection model. | ||
| The filter argument provides a standard approach to filtering regardless of | ||
| the backend and in this case is dynamically translated to SQL predicates in the `WHERE` clause. | ||
| """ | ||
| lookup( | ||
| first: Int! = 10 | ||
| after: String! = "" | ||
| filter: CustomerFilter | ||
| ): CustomerConnection | ||
| @dbquery( | ||
| type: "postgresql" | ||
| table: "customer" | ||
| schema: "public" | ||
| configuration: "postgresql_config" | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| schema @sdl(files: ["api.graphql"]) { | ||
| schema @sdl(files: ["api.graphql", "filter.graphql"]) { | ||
| query: Query | ||
| } | ||
|
|
||
| # this validates that the executable document is valid for the schema. | ||
| # If the schema and document become out of sync then deployment will fail. | ||
| extend schema @sdl(executables: { document: "operations.graphql" }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,38 +12,38 @@ const requestsFile = path.join(path.dirname(__dirname), "operations.graphql"); | |
| const requests = fs.readFileSync(requestsFile, "utf8").toString(); | ||
|
|
||
| describe(testDescription, function () { | ||
| const CURSOR = "eyJjIjoiTDpRdWVyeTpjdXN0b21lcnMiLCJvIjoxfQ==" | ||
| const CURSOR = "eyJjIjoiTDpRdWVyeTpjdXN0b21lcnMiLCJvIjoxfQ=="; | ||
|
|
||
| const tests = [ | ||
| { | ||
| label: "first set of results", | ||
| query: requests, | ||
| operationName: "Customers", | ||
| variables: { | ||
| first: 2 | ||
| variables: { | ||
| first: 2, | ||
| }, | ||
| expected: { | ||
| customers: { | ||
| edges: [ | ||
| { | ||
| node: { | ||
| id: "1", | ||
| name: "Lucas Bill " | ||
| } | ||
| { | ||
| node: { | ||
| id: "1", | ||
| name: "Lucas Bill ", | ||
| }, | ||
| }, | ||
| { | ||
| node: { | ||
| id: "2", | ||
| name: "Mandy Jones ", | ||
| }, | ||
| }, | ||
| { | ||
| node: { | ||
| id: "2", | ||
| name: "Mandy Jones " | ||
| } | ||
| } | ||
| ], | ||
| pageInfo: { | ||
| hasNextPage: true, | ||
| endCursor: CURSOR | ||
| endCursor: CURSOR, | ||
| }, | ||
| totalEdges: 10 | ||
| } | ||
| totalEdges: 10, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
|
|
@@ -52,30 +52,69 @@ describe(testDescription, function () { | |
| operationName: "Customers", | ||
| variables: { | ||
| first: 2, | ||
| after: CURSOR | ||
| after: CURSOR, | ||
| }, | ||
| expected: { | ||
| customers: { | ||
| edges: [ | ||
| { | ||
| node: { | ||
| id: "3", | ||
| name: "Salim Ali " | ||
| } | ||
| name: "Salim Ali ", | ||
| }, | ||
| }, | ||
| { | ||
| node: { | ||
| id: "4", | ||
| name: "Jane Xiu " | ||
| } | ||
| } | ||
| name: "Jane Xiu ", | ||
| }, | ||
| }, | ||
| ], | ||
| pageInfo: { | ||
| endCursor: "eyJjIjoiTDpRdWVyeTpjdXN0b21lcnMiLCJvIjozfQ==", | ||
| hasNextPage: true | ||
| hasNextPage: true, | ||
| }, | ||
| totalEdges: 10, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| label: "lookup-filter", | ||
| query: requests, | ||
| operationName: "Lookup", | ||
| variables: { | ||
| first: 2, | ||
| f: { | ||
| or: { | ||
| name: { eq: "John Doe" }, | ||
| email: { eq: "[email protected]" }, | ||
| }, | ||
| }, | ||
| }, | ||
| expected: { | ||
| customers: { | ||
| edges: [ | ||
| { | ||
| node: { | ||
| id: "5", | ||
| name: "John Doe ", | ||
| email: "[email protected] ", | ||
| }, | ||
| }, | ||
| { | ||
| node: { | ||
| id: "6", | ||
| name: "Jane Smith ", | ||
| email: "[email protected] ", | ||
| }, | ||
| }, | ||
| ], | ||
| pageInfo: { | ||
| hasNextPage: true, | ||
| endCursor: "eyJjIjoiTDpRdWVyeTpsb29rdXAiLCJvIjoxfQ==", | ||
| }, | ||
| totalEdges: 10 | ||
| } | ||
| totalEdges: 2, | ||
| }, | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be changed to
lookup(hence the test failure) as per this operation https://github.com/stepzen-dev/snippets/pull/92/changes#diff-17e39791f40afabc41c58a04a19272d9ff2f95be98c67c8de5a3f37e03f3482cR20