Skip to content

Commit d10027e

Browse files
author
mattia rossi
committed
Fix test errors, implement correct schema code evaluation
1 parent 3786a27 commit d10027e

9 files changed

Lines changed: 231 additions & 60 deletions

__tests__/customOperatorsPlugin.ts

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,72 @@ const CustomOperatorsPlugin: GraphileConfig.Plugin = {
1111
sql,
1212
graphql: { GraphQLInt, GraphQLBoolean },
1313
addConnectionFilterOperator,
14+
EXPORTABLE,
1415
} = build;
1516

1617
// simple
1718
addConnectionFilterOperator("InternetAddress", "familyEqualTo", {
1819
description: "Address family equal to specified value.",
19-
resolveInputCodec: () => TYPES.int,
20-
resolve: (i, v) => sql.fragment`family(${i}) = ${v}`,
20+
resolveInputCodec: EXPORTABLE(
21+
(TYPES) =>
22+
function () {
23+
return TYPES.int;
24+
},
25+
[TYPES]
26+
),
27+
resolve: EXPORTABLE(
28+
(sql) =>
29+
function (i, v) {
30+
return sql.fragment`family(${i}) = ${v}`;
31+
},
32+
[sql]
33+
),
2134
});
2235

2336
// using resolveSqlIdentifier
2437
addConnectionFilterOperator("InternetAddress", "familyNotEqualTo", {
2538
description: "Address family equal to specified value.",
26-
resolveInputCodec: () => TYPES.int,
27-
resolve: (i, v) => sql.fragment`${i} <> ${v}`,
28-
resolveSqlIdentifier: (i) => [sql.fragment`family(${i})`, TYPES.int],
39+
resolveInputCodec: EXPORTABLE(
40+
(TYPES) =>
41+
function () {
42+
return TYPES.int;
43+
},
44+
[TYPES]
45+
),
46+
resolve: EXPORTABLE(
47+
(sql) =>
48+
function (i, v) {
49+
return sql.fragment`${i} <> ${v}`;
50+
},
51+
[sql]
52+
),
53+
resolveSqlIdentifier: EXPORTABLE(
54+
(TYPES, sql) =>
55+
function (i) {
56+
return [sql.fragment`family(${i})`, TYPES.int];
57+
},
58+
[TYPES, sql]
59+
),
2960
});
3061

3162
// using resolveInput // typeNames: string | string[]
3263
addConnectionFilterOperator(["InternetAddress"], "isV4", {
3364
description: "Address family equal to specified value.",
34-
resolve: (i, v) => sql.fragment`family(${i}) = ${v}`,
65+
resolve: EXPORTABLE(
66+
(sql) =>
67+
function (i, v) {
68+
return sql.fragment`family(${i}) = ${v}`;
69+
},
70+
[sql]
71+
),
3572
resolveInput: (input) => (input === true ? 4 : 6),
36-
resolveInputCodec: () => TYPES.int,
73+
resolveInputCodec: EXPORTABLE(
74+
(TYPES) =>
75+
function () {
76+
return TYPES.int;
77+
},
78+
[TYPES]
79+
),
3780
resolveType: () => GraphQLBoolean,
3881
});
3982

__tests__/integration/queries.test.ts

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as fs from "fs";
22
import * as path from "path";
33
import * as pg from "pg";
4-
//import * as vm from "node:vm";
54

65
import { promisify } from "util";
76
import { ExecutionArgs, GraphQLSchema, parse, validate } from "graphql";
@@ -16,47 +15,16 @@ import { execute, hookArgs } from "grafast";
1615
import { SchemaResult } from "graphile-build";
1716
import { makeWithPgClientViaPgClientAlreadyInTransaction } from "@dataplan/pg/adaptors/pg";
1817
import { exportSchemaAsString } from "graphile-export";
19-
import _module = require("module");
20-
import { dirname } from "path";
21-
const { Module, builtinModules } = _module;
22-
import { transformSync } from "@babel/core";
18+
import { importFromStringSync } from "module-from-string";
2319

2420
// TODO: remove this once Grafast gets it's planning under control :D
25-
jest.setTimeout(3000000);
26-
/*
27-
const vmEval = (code: string) => {
28-
const context = {} as GraphQLSchema;
29-
// Load the module with the dyanamic script.
30-
vm.runInNewContext(code, vm.createContext(context));
31-
console.log("Returning context: ", JSON.stringify(context, null, 2));
32-
return context;
33-
};
34-
*/
35-
let cachedSchema = {} as GraphQLSchema;
36-
let haveCache = false;
21+
jest.setTimeout(300000);
22+
3723
const vmEval = (code: string) => {
38-
if (!haveCache) {
39-
const filename = "exported-v5-schema.mjs";
40-
// Load the module with the dyanamic script.
41-
const replacementModule = new Module(filename, this);
42-
replacementModule.filename = filename;
43-
// @ts-ignore
44-
replacementModule.paths = Module._nodeModulePaths(dirname(filename));
45-
const commonJScode = transformSync(code, {
46-
filename,
47-
compact: true,
48-
plugins: ["@babel/plugin-transform-runtime"],
49-
});
50-
// @ts-ignore
51-
replacementModule._compile(commonJScode.code, filename);
52-
replacementModule.loaded = true;
53-
cachedSchema = replacementModule.exports.schema as GraphQLSchema;
54-
haveCache = true;
55-
console.log("Schema import done - no cache");
56-
} else {
57-
console.log("Schema import done - from cache");
58-
}
59-
return cachedSchema;
24+
const { schema } = importFromStringSync(code, {
25+
transformOptions: { loader: "js" },
26+
});
27+
return schema as GraphQLSchema;
6028
};
6129

6230
const createPostGraphileSchema = async (

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"graphile-build-pg": "^5.0.0-beta.1",
5656
"graphql": "16.1.0-experimental-stream-defer.6",
5757
"jest": "29.5.0",
58+
"module-from-string": "^3.3.0",
5859
"pg": "8.8.0",
5960
"pg-introspection": "^0.0.1-beta.1",
6061
"pg-sql2": "^5.0.0-beta.1",
@@ -66,4 +67,4 @@
6667
"files": [
6768
"dist"
6869
]
69-
}
70+
}

src/PgConnectionArgFilterAttributesPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export const PgConnectionArgFilterAttributesPlugin: GraphileConfig.Plugin = {
9898
) {
9999
throw Object.assign(
100100
new Error(
101-
"Null literals are forbidden in filter argument input."
101+
`Null literals are forbidden in filter argument input. - AllowNullInput: ${connectionFilterAllowNullInput}`
102102
),
103103
{
104104
//TODO: mark this error as safe

src/PgConnectionArgFilterForwardRelationsPlugin.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export const PgConnectionArgFilterForwardRelationsPlugin: GraphileConfig.Plugin
4444
fieldWithHooks,
4545
scope: { pgCodec, isPgConnectionFilter },
4646
} = context;
47-
4847
const assertAllowed = makeAssertAllowed(build.options);
4948

5049
const source =

src/PgConnectionArgFilterOperatorsPlugin.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,17 @@ export const PgConnectionArgFilterOperatorsPlugin: GraphileConfig.Plugin = {
6161
}
6262

6363
/** Turn `[Foo]` into `[Foo!]` */
64-
const resolveTypeToListOfNonNullable = (type: GraphQLInputType) => {
65-
if (isListType(type) && !isNonNullType(type.ofType)) {
66-
return new GraphQLList(new GraphQLNonNull(type.ofType));
67-
} else {
68-
return type;
69-
}
70-
};
64+
const resolveTypeToListOfNonNullable = EXPORTABLE(
65+
(GraphQLList, GraphQLNonNull, isListType, isNonNullType) =>
66+
function (type: GraphQLInputType) {
67+
if (isListType(type) && !isNonNullType(type.ofType)) {
68+
return new GraphQLList(new GraphQLNonNull(type.ofType));
69+
} else {
70+
return type;
71+
}
72+
},
73+
[GraphQLList, GraphQLNonNull, isListType, isNonNullType]
74+
);
7175

7276
const forceTextTypesSensitive = [
7377
TYPES.citext,
@@ -1199,7 +1203,9 @@ export function makeApplyPlanFromOperatorSpec(
11991203
if (!connectionFilterAllowNullInput && $input.evalIs(null)) {
12001204
// Forbidden
12011205
throw Object.assign(
1202-
new Error("Null literals are forbidden in filter argument input."),
1206+
new Error(
1207+
`Null literals are forbidden in filter argument input. - AllowNullInput: ${connectionFilterAllowNullInput}`
1208+
),
12031209
{
12041210
//TODO: mark this error as safe
12051211
}

src/PgConnectionArgFilterPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { PgSelectStep, PgCodec } from "@dataplan/pg";
2-
import type { ConnectionStep, FieldArgs } from "grafast";
2+
import type { ConnectionStep } from "grafast";
33
import type {
44
GraphQLInputType,
55
GraphQLOutputType,

src/utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ export function makeAssertAllowed(options: GraphileBuild.SchemaOptions) {
7373
$raw.evalIsEmpty()
7474
) {
7575
throw Object.assign(
76-
new Error("Empty objects are forbidden in filter argument input."),
76+
new Error(`Empty objects are forbidden in filter argument input. - AllowEmptyObjectInput;
77+
: [${connectionFilterAllowEmptyObjectInput}]`),
7778
{
7879
//TODO: mark this error as safe
7980
}
@@ -104,7 +105,9 @@ export function makeAssertAllowed(options: GraphileBuild.SchemaOptions) {
104105
// For all modes, check null
105106
if (!connectionFilterAllowNullInput && $raw.evalIs(null)) {
106107
throw Object.assign(
107-
new Error("Null literals are forbidden in filter argument input."),
108+
new Error(
109+
`Null literals are forbidden in filter argument input. - AllowNullInput: ${connectionFilterAllowNullInput}`
110+
),
108111
{
109112
//TODO: mark this error as safe
110113
}

0 commit comments

Comments
 (0)