graphile / crystal

🔮 Graphile's Crystal Monorepo; home to Grafast, PostGraphile, pg-introspection, pg-sql2 and much more!
https://graphile.org/
Other
12.49k stars 568 forks source link

Using `makeWrapPlansPlugin` creates an error when exporting the schema #2092

Closed FlorentCollin closed 2 weeks ago

FlorentCollin commented 2 weeks ago

Summary

Using the makeWrapPlansPlugin creates an error when trying to export the schema with graphile-export.

Steps to reproduce

I have created a commit with a reproduction of the error https://github.com/FlorentCollin/ouch-my-finger/commit/b41edecaceba1dffbeee982138e5d11f031ef7de

The plugin I tested was the following

const customPlugin = makeWrapPlansPlugin({
    Mutation: {
        updateUserAccount: (plan) => {
            return plan();
        }
    }
});

This version also has the same error.

const customPlugin = makeWrapPlansPlugin({
    Mutation: {
        updateUserAccount: EXPORTABLE(() => (plan) => { return plan(); }, []) }
});

Basically running node export-schema.mjs results in the following error (the full error is available in Additional context)

Error: Attempting to export an instance of a class; you should wrap this definition in EXPORTABLE! (Class: class GraphQLNonNull {

However the makeExtendsSchemaPlugin seems to be exportable with the following plugin so only makeWrapPlansPlugin can't be exported.

import { EXPORTABLE } from "postgraphile/tamedevil";
import { makeExtendSchemaPlugin, gql } from "postgraphile/utils";

export default makeExtendSchemaPlugin({
  typeDefs: gql`
    extend type Query {
      httpBinHeaders: JSON
    }
  `,
  resolvers: {
    Query: {
      httpBinHeaders: EXPORTABLE(async () => {
        const response = await fetch("https://httpbin.org/headers");
        return response.json();
      }, [fetch]),
    },
  },
});

Expected results

I was expecting the schema to be exportable using the makeWrapPlansPlugin.

Actual results

The schema can't be exported.

Additional context

I would like to help on this issue but it might be out of my current knowledge of the project as I just started with it a few weeks ago.

Full error:

Error: Attempting to export an instance of a class; you should wrap this definition in EXPORTABLE! (Class: class GraphQLNonNull {
  constructor(ofType) {
    isNullableType(ofType) ||
      (0, _devAssert.devAssert)(
        false,
        `Expected ${(0, _inspect.inspect)(
          ofType,
        )} to be a GraphQL nullable type.`,
      );
    this.ofType = ofType;
  }

  get [Symbol.toStringTag]() {
    return 'GraphQLNonNull';
  }

  toString() {
    return String(this.ofType) + '!';
  }

  toJSON() {
    return this.toString();
  }
})
    at _convertToAST (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:596:19)
    at handleSubvalue (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:571:25)
    at /Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:603:30
    at Array.forEach (<anonymous>)
    at _convertToAST (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:601:17)
    at handleSubvalue (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:571:25)
    at /Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:603:30
    at Array.forEach (<anonymous>)
    at _convertToAST (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:601:17)
    at handleSubvalue (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:571:25)
    at /Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:603:30
    at Array.forEach (<anonymous>)
    at _convertToAST (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:601:17)
    at convertToIdentifierViaAST (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:698:9)
    at /Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:771:16
    at Array.map (<anonymous>)
    at factoryAst (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:753:39)
    at importWellKnownOrFactory (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:682:16)
    at convertToIdentifierViaAST (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:697:17)
    at handleSubvalue (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:565:25)
    at /Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:603:30
    at Array.forEach (<anonymous>)
    at _convertToAST (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:601:17)
    at handleSubvalue (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:571:25)
    at /Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:603:30
    at Array.forEach (<anonymous>)
    at _convertToAST (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:601:17)
    at convertToIdentifierViaAST (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:698:9)
    at extensions (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:719:12)
    at /Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:361:33
    at Array.reduce (<anonymous>)
    at CodegenFile.makeObjectFields (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:345:44)
    at CodegenFile.makeTypeDeclaration (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:417:60)
    at CodegenFile.declareType (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:281:33)
    at /Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:924:21
    at Array.map (<anonymous>)
    at exportSchemaGraphQLJS (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:923:31)
    at exportSchemaAsString (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:1131:9)
    at exportSchema (/Users/florentcollin/dev/software/ouch-my-finger/node_modules/graphile-export/dist/exportSchema.js:1224:28)
    at main (file:///Users/florentcollin/dev/software/ouch-my-finger/export-schema.mjs:8:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
benjie commented 2 weeks ago

Reproduced; wrapping a simple makeExtendSchemaPlugin Query field worked fine but wrapping an update mutation caused the issue.