From 3726a75a71a7efc21ec303ba437d30ac7058c5b7 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Fri, 14 Jun 2024 16:58:25 +0100 Subject: [PATCH 1/6] Add locationHint to more errors --- utils/graphile-export/src/exportSchema.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/utils/graphile-export/src/exportSchema.ts b/utils/graphile-export/src/exportSchema.ts index 841796e87..1d43a628d 100644 --- a/utils/graphile-export/src/exportSchema.ts +++ b/utils/graphile-export/src/exportSchema.ts @@ -1031,13 +1031,13 @@ function _convertToAST( return func(file, thing as AnyFunction, locationHint, nameHint); } else if (isSchema(thing)) { throw new Error( - "Attempted to export GraphQLSchema directly from `_convertToAST`; this is currently unsupported.", + `Attempted to export GraphQLSchema directly from \`_convertToAST\` (at ${locationHint}); this is currently unsupported.`, ); } else if (typeof thing === "object" && thing != null) { const prototype = Object.getPrototypeOf(thing); if (prototype !== null && prototype !== Object.prototype) { throw new Error( - `Attempting to export an instance of a class; you should wrap this definition in EXPORTABLE! (Class: ${thing.constructor})`, + `Attempting to export an instance of a class (at ${locationHint}); you should wrap this definition in EXPORTABLE! (Class: ${thing.constructor})`, ); } const propertyPairs: Array< @@ -1092,7 +1092,9 @@ function _convertToAST( } } else { if (hasUnsafeKeys) { - throw new Error(`Unexportable key found on non-null-prototype object`); + throw new Error( + `Unexportable key found on non-null-prototype object (at ${locationHint})`, + ); } else { const obj = t.objectExpression( propertyPairs.map(([key, val]) => t.objectProperty(key, val)), From 98d36bdf336c18b3161b45bd5cd29873435cf41e Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Fri, 14 Jun 2024 17:01:28 +0100 Subject: [PATCH 2/6] Don't export the entire field --- .../graphile-utils/src/makeWrapPlansPlugin.ts | 24 ++++--------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/graphile-build/graphile-utils/src/makeWrapPlansPlugin.ts b/graphile-build/graphile-utils/src/makeWrapPlansPlugin.ts index 935dc054f..eb4ca34af 100644 --- a/graphile-build/graphile-utils/src/makeWrapPlansPlugin.ts +++ b/graphile-build/graphile-utils/src/makeWrapPlansPlugin.ts @@ -150,18 +150,11 @@ export function makeWrapPlansPlugin( [access, fieldName], ), } = field; + const typeName = Self.name; return { ...field, plan: EXPORTABLE( - ( - ExecutableStep, - field, - inspect, - isExecutableStep, - oldPlan, - planWrapper, - ) => - (...planParams) => { + (ExecutableStep, fieldName, inspect, isExecutableStep, oldPlan, planWrapper, typeName) => (...planParams) => { // A replacement for `oldPlan` that automatically passes through arguments that weren't replaced const smartPlan = (...overrideParams: Array) => { const $prev = oldPlan( @@ -172,9 +165,9 @@ export function makeWrapPlansPlugin( ); if (!($prev instanceof ExecutableStep)) { console.error( - `Wrapped a plan function, but that function did not return a step!\n${String( + `Wrapped a plan function at ${typeName}.${fieldName}, but that function did not return a step!\n${String( oldPlan, - )}\n${inspect(field)}`, + )}`, ); throw new Error( @@ -204,14 +197,7 @@ export function makeWrapPlansPlugin( } return $newPlan; }, - [ - ExecutableStep, - field, - inspect, - isExecutableStep, - oldPlan, - planWrapper, - ], + [ExecutableStep, fieldName, inspect, isExecutableStep, oldPlan, planWrapper, typeName], ), }; }, From 7a708983ca3d36716288fa329b7bd64570829e0a Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Fri, 14 Jun 2024 17:03:24 +0100 Subject: [PATCH 3/6] Wrap a couple plans to make sure exporting still works --- postgraphile/postgraphile/graphile.config.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/postgraphile/postgraphile/graphile.config.ts b/postgraphile/postgraphile/graphile.config.ts index cc8d4edcc..7dfbf9396 100644 --- a/postgraphile/postgraphile/graphile.config.ts +++ b/postgraphile/postgraphile/graphile.config.ts @@ -3,7 +3,11 @@ import type { PgSelectSingleStep } from "@dataplan/pg"; import { TYPES } from "@dataplan/pg"; import PersistedPlugin from "@grafserv/persisted"; import { EXPORTABLE, exportSchema } from "graphile-export"; -import { gql, makeExtendSchemaPlugin } from "graphile-utils"; +import { + gql, + makeExtendSchemaPlugin, + makeWrapPlansPlugin, +} from "graphile-utils"; import * as jsonwebtoken from "jsonwebtoken"; import type {} from "postgraphile"; import { jsonParse } from "postgraphile/@dataplan/json"; @@ -291,10 +295,12 @@ const preset: GraphileConfig.Preset = { } extend type Query { throw: Int + wrapMe: Int } `, plans: { Query: { + wrapMe: EXPORTABLE((constant) => () => constant(42), [constant]), throw: EXPORTABLE( (error) => () => { return error( @@ -331,6 +337,18 @@ const preset: GraphileConfig.Preset = { }, }; }), + makeWrapPlansPlugin({ + Query: { + wrapMe(plan) { + return plan(); + }, + }, + Mutation: { + updatePost(plan) { + return plan(); + }, + }, + }), makeExtendSchemaPlugin({ typeDefs: gql` extend type Query { From 0e62620f38853eafb588430c62df67d557b5e229 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Fri, 14 Jun 2024 17:03:30 +0100 Subject: [PATCH 4/6] Lint --- .../graphile-utils/src/makeWrapPlansPlugin.ts | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/graphile-build/graphile-utils/src/makeWrapPlansPlugin.ts b/graphile-build/graphile-utils/src/makeWrapPlansPlugin.ts index eb4ca34af..7975c3f17 100644 --- a/graphile-build/graphile-utils/src/makeWrapPlansPlugin.ts +++ b/graphile-build/graphile-utils/src/makeWrapPlansPlugin.ts @@ -154,7 +154,16 @@ export function makeWrapPlansPlugin( return { ...field, plan: EXPORTABLE( - (ExecutableStep, fieldName, inspect, isExecutableStep, oldPlan, planWrapper, typeName) => (...planParams) => { + ( + ExecutableStep, + fieldName, + inspect, + isExecutableStep, + oldPlan, + planWrapper, + typeName, + ) => + (...planParams) => { // A replacement for `oldPlan` that automatically passes through arguments that weren't replaced const smartPlan = (...overrideParams: Array) => { const $prev = oldPlan( @@ -197,7 +206,15 @@ export function makeWrapPlansPlugin( } return $newPlan; }, - [ExecutableStep, fieldName, inspect, isExecutableStep, oldPlan, planWrapper, typeName], + [ + ExecutableStep, + fieldName, + inspect, + isExecutableStep, + oldPlan, + planWrapper, + typeName, + ], ), }; }, From d2d9b1965b0cad29891220c32c978a147d8a4015 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Fri, 14 Jun 2024 17:04:22 +0100 Subject: [PATCH 5/6] docs(changeset): Output location in more error messages. --- .changeset/quick-dogs-clap.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/quick-dogs-clap.md diff --git a/.changeset/quick-dogs-clap.md b/.changeset/quick-dogs-clap.md new file mode 100644 index 000000000..d3e28e77c --- /dev/null +++ b/.changeset/quick-dogs-clap.md @@ -0,0 +1,5 @@ +--- +"graphile-export": patch +--- + +Output location in more error messages. From c0e50a1b4f1c95bfcafb5458dce0d5e56852d7d0 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Fri, 14 Jun 2024 17:04:39 +0100 Subject: [PATCH 6/6] docs(changeset): makeWrapPlansPlugin more likely to be exportable. --- .changeset/silent-cups-allow.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/silent-cups-allow.md diff --git a/.changeset/silent-cups-allow.md b/.changeset/silent-cups-allow.md new file mode 100644 index 000000000..b6e3686b0 --- /dev/null +++ b/.changeset/silent-cups-allow.md @@ -0,0 +1,6 @@ +--- +"graphile-utils": patch +"postgraphile": patch +--- + +makeWrapPlansPlugin more likely to be exportable.