|
| 1 | +type Customer { |
| 2 | + id: ID! |
| 3 | + name: String |
| 4 | + email: String |
| 5 | + street: String |
| 6 | + city: String |
| 7 | + region: String |
| 8 | +} |
| 9 | + |
| 10 | +""" |
| 11 | +`CustomerConnection` is the connection type for `Customer` pagination. |
| 12 | +""" |
| 13 | +type CustomerConnection { |
| 14 | + edges: [CustomerEdge] |
| 15 | + pageInfo: PageInfo! |
| 16 | +} |
| 17 | + |
| 18 | +""" |
| 19 | +`CustomerEdge` provides access to the node and its cursor. |
| 20 | +""" |
| 21 | +type CustomerEdge { |
| 22 | + node: Customer |
| 23 | + cursor: String |
| 24 | +} |
| 25 | + |
| 26 | +input StringFilter { |
| 27 | + eq: String |
| 28 | + ne: String |
| 29 | +} |
| 30 | + |
| 31 | +input CustomerFilter { |
| 32 | + name: StringFilter |
| 33 | + email: StringFilter |
| 34 | + city: StringFilter |
| 35 | +} |
| 36 | + |
| 37 | +type _RegionsList { |
| 38 | + regions: [String]! |
| 39 | +} |
| 40 | + |
| 41 | +extend type Query { |
| 42 | + # customers is the exposed field that limits the caller to regions |
| 43 | + # based upon the regions claim in the request's JWT. |
| 44 | + customers(first: Int! = 10, filter: CustomerFilter): [Customer] |
| 45 | + @sequence( |
| 46 | + steps: [ |
| 47 | + { query: "_regions" } |
| 48 | + { |
| 49 | + query: "_customers_flatten" |
| 50 | + arguments: [ |
| 51 | + { name: "first", argument: "first" } |
| 52 | + { name: "filter", argument: "filter" } |
| 53 | + ] |
| 54 | + } |
| 55 | + ] |
| 56 | + ) |
| 57 | + |
| 58 | + # extracts the regions visible to the request from the JWT. |
| 59 | + _regions: _RegionsList |
| 60 | + @value( |
| 61 | + script: { |
| 62 | + src: """ |
| 63 | + {"regions": `$jwt`.regions } |
| 64 | + """ |
| 65 | + language: JSONATA |
| 66 | + } |
| 67 | + ) |
| 68 | + |
| 69 | + # this flattens the customer connection pagination structure |
| 70 | + # into a simple list of Customer objects. |
| 71 | + # This is needed as @sequence is not supported for connection types. |
| 72 | + _customers_flatten( |
| 73 | + first: Int! = 10 |
| 74 | + filter: CustomerFilter |
| 75 | + regions: [String]! |
| 76 | + ): [Customer] @materializer(query: "_customers { edges { node }}") |
| 77 | + |
| 78 | + # Standard paginated field for a customers table in a database. |
| 79 | + # Additional regions argument that is used to limit customer |
| 80 | + # visibility based upon the 'regions' claim in a JWT. |
| 81 | + # The regions allows a list of regions and uses SQL ANY to match rows. |
| 82 | + _customers( |
| 83 | + first: Int! = 10 |
| 84 | + after: String! = "" |
| 85 | + filter: CustomerFilter |
| 86 | + regions: [String]! |
| 87 | + ): CustomerConnection |
| 88 | + @dbquery( |
| 89 | + type: "postgresql" |
| 90 | + schema: "public" |
| 91 | + query: """ |
| 92 | + SELECT C.id, C.name, C.email, A.street, A.city, A.countryregion AS region |
| 93 | + FROM customer C, address A, customeraddress CA |
| 94 | + WHERE |
| 95 | + CA.customerid = C.id AND |
| 96 | + CA.addressid = A.id AND |
| 97 | + A.countryregion = ANY(CAST($1 AS VARCHAR ARRAY)) |
| 98 | + """ |
| 99 | + configuration: "postgresql_config" |
| 100 | + ) |
| 101 | +} |
0 commit comments