tjmoses / postgraphile-plugin-batch-create-update-delete

A postgraphile plugin that allows for batch create, update, & delete mutations in a single transaction.
https://www.npmjs.com/package/postgraphile-plugin-many-create-update-delete
MIT License
37 stars 9 forks source link

removed graphql import to fix version conflicts #12

Closed warrenisarobot closed 2 years ago

warrenisarobot commented 2 years ago

When using this plugin on the current version of postgraphile mutations are no longer included and I get the following error:

Recoverable error occurred:
2022-05-03T21:37:58.792Z graphile-build:warn Error: Cannot use GraphQLNonNull "CallCarrierLinePatch!" from another module or realm.

Ensure that there is only one instance of "graphql" in the node_modules
directory. If different versions of "graphql" are the dependencies of other
relied on modules, use "resolutions" to ensure only one version is installed.

https://yarnpkg.com/en/docs/selective-version-resolutions

Duplicate "graphql" modules cannot be used at the same time since different
versions may have different capabilities and behavior. The data from one
version used in the function from another could produce confusing and
spurious results.
    at instanceOf (/usr/local/lib/node_modules/postgraphile-plugin-many-create-update-delete/node_modules/graphql/jsutils/instanceOf.js:35:13)
    at isNonNullType (/usr/local/lib/node_modules/postgraphile-plugin-many-create-update-delete/node_modules/graphql/type/definition.js:200:34)
    at isType (/usr/local/lib/node_modules/postgraphile-plugin-many-create-update-delete/node_modules/graphql/type/definition.js:92:167)
    at assertType (/usr/local/lib/node_modules/postgraphile-plugin-many-create-update-delete/node_modules/graphql/type/definition.js:96:8)
    at new GraphQLList (/usr/local/lib/node_modules/postgraphile-plugin-many-create-update-delete/node_modules/graphql/type/definition.js:323:19)
    at /usr/local/lib/node_modules/postgraphile-plugin-many-create-update-delete/build/PostGraphileManyUpdatePlugin.js:116:35
    at Array.forEach (<anonymous>)
    at handleAdditionsFromTableInfo (/usr/local/lib/node_modules/postgraphile-plugin-many-create-update-delete/build/PostGraphileManyUpdatePlugin.js:89:31)
    at GQLObjectFieldsHookHandlerFcn (/usr/local/lib/node_modules/postgraphile-plugin-many-create-update-delete/build/PostGraphileManyUpdatePlugin.js:32:13)
    at SchemaBuilder.applyHooks (/usr/local/lib/node_modules/postgraphile/node_modules/graphile-build/node8plus/SchemaBuilder.js:264:20)

This is due to the graphql import in 2 packages. This uses the included graphql object provided from postgraphile so the import is not needed. This solves the error and mutations are included, along with the mn prefixed mutations.

tjmoses commented 2 years ago

I have the latest postgraphile version installed and just ran a test without issue. Although, due to other old packages I had to force a specific version of graphql using the new package.json overrides field. Can you run npm ls graphql (or the yarn equivalent) and let me know if there are other versions used and if this package has one of those versions (maybe a snippet/screenshot would be nice). I'm just curious.

What's interesting here is that package "graphql" is not installed on this project (see the package.json). I was relying on that being installed from postgraphile (or another package). I suppose NodeJS just found the first version on your project's dependencies or nested dependencies and used it.

What's also interesting is where GraphQLNonNull was used (directly from postgraphile's context object), which is what your initial error is from.

I also see there's still a 'graphql' import used on the plugin types file. I suppose this really doesn't matter since it was only used for types and is probably ignored/discarded in compilation.

I also suppose I missed GraphQLList being on the postgraphile context object (that or my code autocompleted and imported it). Good Catch and thanks for your contribution. I'll merge this and push the update.

warrenisarobot commented 2 years ago

I bet you're right about the auto-complete since all the other graphql objects come from the context object.

You should be able to duplicate with this Dockerfile:

FROM node:alpine
LABEL description="Postgraphile test"

RUN npm install -g postgraphile
RUN npm install -g postgraphile-plugin-many-create-update-delete@1.0.6

ENTRYPOINT ["postgraphile", "-n", "0.0.0.0", "--append-plugins","postgraphile-plugin-many-create-update-delete"]

From the folder this Dockerfile is in you'd do a:

docker build -t pgtest .

Set the database url as an env var (if the db is running on your local machine use the host host.docker.internal instead of localhost:

export DATABASE_URL=postgres://user:pass@host.docker.internal:5432/dbname

Then run:

docker run --env DEBUG="graphile-build:warn" pgtest --connection=$DATABASE_URL

This will give you the error above if the db is able to connect.

In the container if I want to do a npm list -g graphql I do a:

docker run --entrypoint="npm" pgtest ls -g graphql

This is the output for that:

image