graphql-type-json icon indicating copy to clipboard operation
graphql-type-json copied to clipboard

Can't import from graphql-type-json

Open lenneis opened this issue 5 years ago • 9 comments

Hi,

this is a minimal code snippet from a module I am developing:

import { GraphQLJSONObject } from 'graphql-type-json';

It lives inside a xxx.mjs file, which is imported by other ES6 modules in the application.

When I run this (node version 13.14) I get a fatal error:

import { GraphQLJSONObject } from 'graphql-type-json'; ^^^^^^^^^^^^^^^^^ SyntaxError: The requested module 'graphql-type-json' does not provide an export named 'GraphQLJSONObject'

I looked at the package.json file in your module and I am not sure it does the right thing, according to the node.js documentation:

"module": "es/index.js",

This is ignored by node, according to their docs (https://nodejs.org/docs/latest-v13.x/api/esm.html). Instead, one is supposed to use a conditional export in package.json , like this:

"exports": { "import": "./main-module.js", "require": "./main-require.cjs" }, main-module.js does the ES6 thing and main-require.cjs the commonJS one. Any thoughts?

lenneis avatar Jun 29 '20 22:06 lenneis

Yeah, my build is wrong – I didn't properly tag the ES modules to be usable by Node. Do you want to send in a PR to fix the package.json?

taion avatar Jun 29 '20 22:06 taion

OK, will do.

lenneis avatar Jun 29 '20 22:06 lenneis

OK, will do.

lenneis avatar Jul 01 '20 19:07 lenneis

OK, this is all way messier than I thought. I am trying to do this using "pure" node, so not the esm module. First, the fix for your code seems to be the following. Add this to package.json (top level):

"exports": {
    "import": "./es/index.mjs",
    "require": "./lib/index.js"
  }

this will redirect import and require to the appropriate files. Note that I have renamed es/index.js to es/index.mjs to force node to read it as ES6 module code. Otherwise it will try and parse it as commonJS. However, that is not enough because the graphql import/export does not work properly either. This code

import { GraphQLScalarType } from 'graphql';
import { Kind, print } from 'graphql/language';

in es/index.mjs throws an error as well, because graphql itself does not provide proper entry points for their import/export stuff either. This goes to the relevant graphql files and subdirectories directly and works:

import { GraphQLScalarType } from 'graphql/index.mjs';
import { Kind, print } from 'graphql/language/index.mjs';

This, however is in direct contradiction to the sample code they list on https://www.npmjs.com/package/graphql. There, they do an

 import {
  graphql,
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString,
} from 'graphql';

which does not work. I just verified that yesterday. I am not sure this is the right time yet to apply a fix to your code before the graphql people have not fixed theirs. What do you think?

Here are my workarounds for the project I am working on:

use imports from graphql like this

import { GraphQLScalarType } from 'graphql/index.mjs';

until further notice.

For your module I use a little two liner local ES6 module that imports the top level export object from your module and then exports the two definitions for GraphQLJSON and GraphQLJSONObject.

lenneis avatar Jul 01 '20 19:07 lenneis

OK, I am giving up on import/export from graphql and graphql-type-json and will revert back to require. This is simply not ready for prime time. I was testing the workaround mentioned above and that leads to the following infamous error:

 Error: Cannot use GraphQLScalarType "JSONObject" 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.

Apparently, two instances of the graphql import are generated. I'll check back in a year or so, this looks like it will take some time. Shame about the ES6 modules, working with import/export leads to cleaner code and the circular references possible enable better modularization.

lenneis avatar Jul 02 '20 16:07 lenneis

OK, I have reported the

import { GraphQLScalarType } from 'graphql';

to the graphQL developers. If they fix the issue, maybe we can also fix this one.

lenneis avatar Jul 03 '20 09:07 lenneis

Ahh, okay, thanks for digging into things. Sorry this didn't quite work. Let me know if there's any progress on the upstream end.

I guess I should probably remove the ES module exports here, as they don't really help anyone and at best just lead to confusion.

taion avatar Jul 03 '20 15:07 taion

Maybe wait a couple of days before removing, I got a really fast reaction from one of the graphql developers. I have sent him a minimal example to reproduce the problem and hopefully there will be a reply soon.

lenneis avatar Jul 03 '20 15:07 lenneis

any news?

GiovanniCapizzi avatar Dec 04 '20 15:12 GiovanniCapizzi