Skip to content

Commit 33349ce

Browse files
Added filter & simplify schema plugins, get reports route w/ pagination
1 parent 210ed14 commit 33349ce

File tree

6 files changed

+51
-4
lines changed

6 files changed

+51
-4
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
"dependencies": {},
1616
"devDependencies": {
1717
"@fastify/aws-lambda": "^4.0.0",
18+
"@graphile-contrib/pg-simplify-inflector": "^6.1.0",
1819
"@types/node": "^20.12.7",
1920
"aws-jwt-verify": "^4.0.1",
2021
"esbuild": "^0.20.1",
2122
"fastify": "^4.26.2",
2223
"graphql": "^16.8.1",
2324
"pg": "^8.11.5",
2425
"postgraphile": "^4.13.0",
26+
"postgraphile-plugin-connection-filter": "^2.3.0",
2527
"serverless-postgres": "^2.1.0"
2628
},
2729
"resolutions": {

src/app.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Fastify from 'fastify';
2-
import { getResults, graphql } from './routes/index.js';
2+
import { getReports, getResults, graphql } from './routes/index.js';
33
import { CognitoJwtVerifier } from 'aws-jwt-verify';
44
export const fastify = Fastify({ logger: true });
55
const cognitoJwtVerifier = CognitoJwtVerifier.create({
@@ -21,6 +21,7 @@ fastify.addHook('preHandler', async (request, reply) => {
2121

2222
fastify.post('/graphql', {}, async (request, reply) => graphql({ request, reply }));
2323
fastify.get('/get/results', {}, async (request, reply) => getResults({ request, reply }));
24+
fastify.get('/get/reports', {}, async (request, reply) => getReports({ request, reply }));
2425

2526
fastify.listen({ port: 3000 }, (err) => {
2627
console.log(`Server listening on ${fastify.server.address().port}`)

src/routes/getReports.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { graphqlQuery } from '../utils/index.js';
2+
3+
export const getReports = async ({ request, reply }) => {
4+
const response = (await graphqlQuery({
5+
query: `query($first: Int, $offset: Int){
6+
reports(first: $first, offset: $offset) {
7+
nodes {
8+
id
9+
title
10+
filters
11+
}
12+
totalCount
13+
}
14+
}`,
15+
variables: {
16+
first: parseInt(request.query.first),
17+
offset: parseInt(request.query.offset),
18+
},
19+
}))?.data;
20+
21+
return {
22+
status: 'success',
23+
result: response?.reports?.nodes?.map(obj => ({
24+
...obj,
25+
filters: obj?.filters ? JSON.parse(obj?.filters) : null,
26+
})),
27+
total: response?.reports?.totalCount,
28+
};
29+
}

src/routes/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './getResults.js'
2-
export * from './graphql.js'
2+
export * from './graphql.js'
3+
export * from './getReports.js'

src/utils/graphqlQuery.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { graphql as gql } from 'graphql';
22
import { withPostGraphileContext, createPostGraphileSchema } from 'postgraphile';
3+
import PgSimplifyInflectorPlugin from '@graphile-contrib/pg-simplify-inflector';
4+
import ConnectionFilterPlugin from 'postgraphile-plugin-connection-filter';
35
import pg from 'pg';
46
import { jwtClaims } from '../app.js';
57
const pool = new pg.Pool({ connectionString: process.env.DB_CONNECTION_URL });
6-
const cache = createPostGraphileSchema(pool, 'public');
8+
const cache = createPostGraphileSchema(pool, 'public', { appendPlugins: [PgSimplifyInflectorPlugin, ConnectionFilterPlugin] });
79

810
export const graphqlQuery = async ({ query, variables = null }) => {
911
try {

yarn.lock

+13-1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@
150150
dependencies:
151151
fast-deep-equal "^3.1.3"
152152

153+
"@graphile-contrib/pg-simplify-inflector@^6.1.0":
154+
version "6.1.0"
155+
resolved "https://registry.yarnpkg.com/@graphile-contrib/pg-simplify-inflector/-/pg-simplify-inflector-6.1.0.tgz#5ecb842bd43af4d3ffdfcd25b1e7c452374f7440"
156+
integrity sha512-3eI2FP4ulu/fxwkJBNXhR6XEzqVz4wJWFr4LfeyUNNArUtLFx0DpP6YdcARCYgwLExFcIQNE8fnul3JKiciYIw==
157+
153158
"@graphile/lru@4.11.0":
154159
version "4.11.0"
155160
resolved "https://registry.yarnpkg.com/@graphile/lru/-/lru-4.11.0.tgz#dd805ee083063488796ec0eac5a8b50b21c076f9"
@@ -1033,6 +1038,13 @@ postgraphile-core@4.13.0:
10331038
graphile-build-pg "4.13.0"
10341039
tslib "^2.0.1"
10351040

1041+
postgraphile-plugin-connection-filter@^2.3.0:
1042+
version "2.3.0"
1043+
resolved "https://registry.yarnpkg.com/postgraphile-plugin-connection-filter/-/postgraphile-plugin-connection-filter-2.3.0.tgz#0ef9316f73b3cac6452187debe2053c379cf2ade"
1044+
integrity sha512-TR5bq/NOqqtm4xV3D2Qw+CJo0Hanrx1lqh0KZiU78z8YijcLC0NuT3z0nJyCB842nWbx5Wi6p0S+xmrgzRFNgQ==
1045+
dependencies:
1046+
tslib "^2.3.0"
1047+
10361048
postgraphile@^4.13.0:
10371049
version "4.13.0"
10381050
resolved "https://registry.yarnpkg.com/postgraphile/-/postgraphile-4.13.0.tgz#ed8cb05e6919e09d4a90bd8d9db6941eb3ce7690"
@@ -1348,7 +1360,7 @@ toidentifier@1.0.1:
13481360
resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.1.tgz#3be34321a88a820ed1bd80dfaa33e479fbb8dd35"
13491361
integrity sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==
13501362

1351-
tslib@^2.0.1, tslib@^2.1.0:
1363+
tslib@^2.0.1, tslib@^2.1.0, tslib@^2.3.0:
13521364
version "2.6.2"
13531365
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.6.2.tgz#703ac29425e7b37cd6fd456e92404d46d1f3e4ae"
13541366
integrity sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==

0 commit comments

Comments
 (0)