@@ -3,15 +3,21 @@ import * as path from "path";
33import * as pg from "pg" ;
44
55import { promisify } from "util" ;
6- import { ExecutionArgs , GraphQLSchema , parse , validate } from "graphql" ;
6+ import {
7+ ExecutionArgs ,
8+ GraphQLError ,
9+ GraphQLSchema ,
10+ parse ,
11+ validate ,
12+ } from "graphql" ;
713import { withPgClient , withPgPool } from "../helpers" ;
814import { PgConditionArgumentPlugin } from "graphile-build-pg" ;
915import { postgraphilePresetAmber } from "postgraphile/presets/amber" ;
1016import { makeV4Preset , V4Options } from "postgraphile/presets/v4" ;
1117import { makeSchema } from "postgraphile" ;
1218import { PostGraphileConnectionFilterPreset } from "../../src/index" ;
1319import CustomOperatorsPlugin from "./../customOperatorsPlugin" ;
14- import { execute , hookArgs } from "grafast" ;
20+ import { execute , hookArgs , isSafeError } from "grafast" ;
1521import { SchemaResult } from "graphile-build" ;
1622import { makeWithPgClientViaPgClientAlreadyInTransaction } from "@dataplan/pg/adaptors/pg" ;
1723import { exportSchemaAsString } from "graphile-export" ;
@@ -23,6 +29,7 @@ jest.setTimeout(300000);
2329const vmEval = ( code : string ) => {
2430 const { schema } = importFromStringSync ( code , {
2531 transformOptions : { loader : "js" } ,
32+ useCurrentGlobal : true ,
2633 } ) ;
2734 return schema as GraphQLSchema ;
2835} ;
@@ -117,36 +124,66 @@ beforeAll(async () => {
117124 nullAndEmptyAllowed ,
118125 addConnectionFilterOperator ,
119126 ] = await Promise . all ( [
120- createPostGraphileSchema ( pool , [ "p" ] , {
121- skipPlugins : [ PgConditionArgumentPlugin ] ,
122- } ) ,
123- createPostGraphileSchema ( pool , [ "p" ] , {
124- skipPlugins : [ PgConditionArgumentPlugin ] ,
125- dynamicJson : true ,
126- } ) ,
127- createPostGraphileSchema ( pool , [ "p" ] , {
128- skipPlugins : [ PgConditionArgumentPlugin ] ,
129- graphileBuildOptions : {
130- pgUseCustomNetworkScalars : true ,
127+ createPostGraphileSchema (
128+ pool ,
129+ [ "p" ] ,
130+ {
131+ skipPlugins : [ PgConditionArgumentPlugin ] ,
131132 } ,
132- } ) ,
133- createPostGraphileSchema ( pool , [ "p" ] , {
134- skipPlugins : [ PgConditionArgumentPlugin ] ,
135- graphileBuildOptions : {
136- connectionFilterRelations : true ,
133+ { }
134+ ) ,
135+ createPostGraphileSchema (
136+ pool ,
137+ [ "p" ] ,
138+ {
139+ skipPlugins : [ PgConditionArgumentPlugin ] ,
140+ dynamicJson : true ,
137141 } ,
138- } ) ,
139- createPostGraphileSchema ( pool , [ "p" ] , {
140- skipPlugins : [ PgConditionArgumentPlugin ] ,
141- simpleCollections : "only" ,
142- } ) ,
143- createPostGraphileSchema ( pool , [ "p" ] , {
144- skipPlugins : [ PgConditionArgumentPlugin ] ,
145- graphileBuildOptions : {
146- connectionFilterAllowNullInput : true ,
147- connectionFilterAllowEmptyObjectInput : true ,
142+ { }
143+ ) ,
144+ createPostGraphileSchema (
145+ pool ,
146+ [ "p" ] ,
147+ {
148+ skipPlugins : [ PgConditionArgumentPlugin ] ,
149+ graphileBuildOptions : {
150+ pgUseCustomNetworkScalars : true ,
151+ } ,
148152 } ,
149- } ) ,
153+ { }
154+ ) ,
155+ createPostGraphileSchema (
156+ pool ,
157+ [ "p" ] ,
158+ {
159+ skipPlugins : [ PgConditionArgumentPlugin ] ,
160+ graphileBuildOptions : {
161+ connectionFilterRelations : true ,
162+ } ,
163+ } ,
164+ { }
165+ ) ,
166+ createPostGraphileSchema (
167+ pool ,
168+ [ "p" ] ,
169+ {
170+ skipPlugins : [ PgConditionArgumentPlugin ] ,
171+ simpleCollections : "only" ,
172+ } ,
173+ { }
174+ ) ,
175+ createPostGraphileSchema (
176+ pool ,
177+ [ "p" ] ,
178+ {
179+ skipPlugins : [ PgConditionArgumentPlugin ] ,
180+ graphileBuildOptions : {
181+ connectionFilterAllowNullInput : true ,
182+ connectionFilterAllowEmptyObjectInput : true ,
183+ } ,
184+ } ,
185+ { }
186+ ) ,
150187 createPostGraphileSchema (
151188 pool ,
152189 [ "p" ] ,
@@ -172,73 +209,66 @@ beforeAll(async () => {
172209
173210for ( const queryFileName of queryFileNames ) {
174211 // eslint-disable-next-line jest/valid-title
175- test (
176- queryFileName ,
177- async ( ) => {
178- // Read the query from the file system.
179- const query = await readFile (
180- path . resolve ( queriesDir , queryFileName ) ,
181- "utf8"
212+ test ( queryFileName , async ( ) => {
213+ // Read the query from the file system.
214+ const query = await readFile (
215+ path . resolve ( queriesDir , queryFileName ) ,
216+ "utf8"
217+ ) ;
218+ // Get the appropriate GraphQL schema for this fixture. We want to test
219+ // some specific fixtures against a schema configured slightly
220+ // differently.
221+ const gqlSchemaByQueryFileName : {
222+ [ queryFileName : string ] : SchemaResult ;
223+ } = {
224+ "addConnectionFilterOperator.graphql" :
225+ gqlSchemas . addConnectionFilterOperator ,
226+ "dynamicJsonTrue.graphql" : gqlSchemas . dynamicJson ,
227+ "types.cidr.graphql" : gqlSchemas . networkScalars ,
228+ "types.macaddr.graphql" : gqlSchemas . networkScalars ,
229+ "arrayTypes.cidrArray.graphql" : gqlSchemas . networkScalars ,
230+ "arrayTypes.macaddrArray.graphql" : gqlSchemas . networkScalars ,
231+ "relations.graphql" : gqlSchemas . relations ,
232+ "simpleCollections.graphql" : gqlSchemas . simpleCollections ,
233+ "nullAndEmptyAllowed.graphql" : gqlSchemas . nullAndEmptyAllowed ,
234+ } ;
235+ const { schema, resolvedPreset } =
236+ queryFileName in gqlSchemaByQueryFileName
237+ ? gqlSchemaByQueryFileName [ queryFileName ]
238+ : gqlSchemas . normal ;
239+ const document = parse ( query ) ;
240+ const errors = validate ( schema , document ) ;
241+ if ( errors . length > 0 ) {
242+ throw new Error (
243+ `GraphQL validation errors:\n${ errors . map ( ( e ) => e . message ) . join ( "\n" ) } `
182244 ) ;
183- // Get the appropriate GraphQL schema for this fixture. We want to test
184- // some specific fixtures against a schema configured slightly
185- // differently.
186- const gqlSchemaByQueryFileName : {
187- [ queryFileName : string ] : SchemaResult ;
188- } = {
189- "addConnectionFilterOperator.graphql" :
190- gqlSchemas . addConnectionFilterOperator ,
191- "dynamicJsonTrue.graphql" : gqlSchemas . dynamicJson ,
192- "types.cidr.graphql" : gqlSchemas . networkScalars ,
193- "types.macaddr.graphql" : gqlSchemas . networkScalars ,
194- "arrayTypes.cidrArray.graphql" : gqlSchemas . networkScalars ,
195- "arrayTypes.macaddrArray.graphql" : gqlSchemas . networkScalars ,
196- "relations.graphql" : gqlSchemas . relations ,
197- "simpleCollections.graphql" : gqlSchemas . simpleCollections ,
198- "nullAndEmptyAllowed.graphql" : gqlSchemas . nullAndEmptyAllowed ,
199- } ;
200- const { schema, resolvedPreset } =
201- queryFileName in gqlSchemaByQueryFileName
202- ? gqlSchemaByQueryFileName [ queryFileName ]
203- : gqlSchemas . normal ;
204-
205- const document = parse ( query ) ;
206- const errors = validate ( schema , document ) ;
207- if ( errors . length > 0 ) {
208- throw new Error (
209- `GraphQL validation errors:\n${ errors
210- . map ( ( e ) => e . message )
211- . join ( "\n" ) } `
212- ) ;
213- }
214- const args : ExecutionArgs = {
215- schema,
216- document,
217- } ;
218- await hookArgs ( args , resolvedPreset , { } ) ;
219- //const pgSubscriber = new PgSubscriber(pool);
220- const result = ( await withPgClient ( async ( pgClient ) => {
221- // We must override the context because we didn't use a pool above and so
222- // we need to add our own client
245+ }
246+ const args : ExecutionArgs = {
247+ schema,
248+ document,
249+ } ;
250+ await hookArgs ( args , resolvedPreset , { } ) ;
251+ //const pgSubscriber = new PgSubscriber(pool);
252+ const result = ( await withPgClient ( async ( pgClient ) => {
253+ // We must override the context because we didn't use a pool above and so
254+ // we need to add our own client
223255
224- // NOTE: the withPgClient needed on context is **VERY DIFFERENT** to our
225- // withPgClient test helper. We should rename our test helper ;)
226- const contextWithPgClient =
227- makeWithPgClientViaPgClientAlreadyInTransaction ( pgClient , false ) ;
228- try {
229- args . contextValue = {
230- pgSettings : ( args . contextValue as any ) . pgSettings ,
231- withPgClient : contextWithPgClient ,
232- //pgSubscriber,
233- } ;
256+ // NOTE: the withPgClient needed on context is **VERY DIFFERENT** to our
257+ // withPgClient test helper. We should rename our test helper ;)
258+ const contextWithPgClient =
259+ makeWithPgClientViaPgClientAlreadyInTransaction ( pgClient , false ) ;
260+ try {
261+ args . contextValue = {
262+ pgSettings : ( args . contextValue as any ) . pgSettings ,
263+ withPgClient : contextWithPgClient ,
264+ //pgSubscriber,
265+ } ;
234266
235- return ( await execute ( args ) ) as any ;
236- } finally {
237- contextWithPgClient . release ?.( ) ;
238- }
239- } ) ) as any ;
240- expect ( result ) . toMatchSnapshot ( ) ;
241- } ,
242- 100000000
243- ) ;
267+ return ( await execute ( args ) ) as any ;
268+ } finally {
269+ contextWithPgClient . release ?.( ) ;
270+ }
271+ } ) ) as any ;
272+ expect ( result ) . toMatchSnapshot ( ) ;
273+ } ) ;
244274}
0 commit comments