-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathQueryRunner.ts
42 lines (36 loc) · 1.29 KB
/
QueryRunner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import 'dotenv/config'
import { introspectionFromSchema } from 'graphql'
import { ApolloClient, InMemoryCache } from '@apollo/client'
import { Pool } from 'pg'
import { createPostGraphileSchema } from 'postgraphile'
import PgSimplifyInflectorPlugin from '@graphile-contrib/pg-simplify-inflector'
// @ts-ignore
import PgConnectionFilterPlugin from 'postgraphile-plugin-connection-filter'
import GraphileLink from './GraphileLink'
export async function makeQueryRunner(
connectionString = process.env.DATABASE_URL || 'postgres:///',
schemaName = process.env.DATABASE_SCHEMA || 'app_public',
options = {
appendPlugins: [PgSimplifyInflectorPlugin, PgConnectionFilterPlugin],
} // See https://www.graphile.org/postgraphile/usage-schema/ for options
) {
const pgPool = new Pool({
connectionString,
})
// Create the PostGraphile schema
const schema = await createPostGraphileSchema(connectionString, schemaName, options)
const introspection = introspectionFromSchema(schema as any, { descriptions: true })
const apolloClient = new ApolloClient({
ssrMode: true,
cache: new InMemoryCache(),
link: new GraphileLink({ pgPool, schema: schema as any }),
})
const release = () => {
pgPool.end()
}
return {
release,
apolloClient,
schema: introspection.__schema,
}
}