Skip to content

Commit

Permalink
Export wrap (#2094)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Jun 14, 2024
2 parents b1ffe28 + c0e50a1 commit ab3ac22
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-dogs-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"graphile-export": patch
---

Output location in more error messages.
6 changes: 6 additions & 0 deletions .changeset/silent-cups-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"graphile-utils": patch
"postgraphile": patch
---

makeWrapPlansPlugin more likely to be exportable.
11 changes: 7 additions & 4 deletions graphile-build/graphile-utils/src/makeWrapPlansPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,18 @@ export function makeWrapPlansPlugin<T>(
[access, fieldName],
),
} = field;
const typeName = Self.name;
return {
...field,
plan: EXPORTABLE(
(
ExecutableStep,
field,
fieldName,
inspect,
isExecutableStep,
oldPlan,
planWrapper,
typeName,
) =>
(...planParams) => {
// A replacement for `oldPlan` that automatically passes through arguments that weren't replaced
Expand All @@ -172,9 +174,9 @@ export function makeWrapPlansPlugin<T>(
);
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(
Expand Down Expand Up @@ -206,11 +208,12 @@ export function makeWrapPlansPlugin<T>(
},
[
ExecutableStep,
field,
fieldName,
inspect,
isExecutableStep,
oldPlan,
planWrapper,
typeName,
],
),
};
Expand Down
20 changes: 19 additions & 1 deletion postgraphile/postgraphile/graphile.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions utils/graphile-export/src/exportSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -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)),
Expand Down

0 comments on commit ab3ac22

Please sign in to comment.