Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't import from graphql-type-json #277

Open
lenneis opened this issue Jun 29, 2020 · 9 comments
Open

Can't import from graphql-type-json #277

lenneis opened this issue Jun 29, 2020 · 9 comments

Comments

@lenneis
Copy link

lenneis commented Jun 29, 2020

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?

@taion
Copy link
Owner

taion commented Jun 29, 2020

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?

@lenneis
Copy link
Author

lenneis commented Jun 29, 2020

OK, will do.

1 similar comment
@lenneis
Copy link
Author

lenneis commented Jul 1, 2020

OK, will do.

@lenneis
Copy link
Author

lenneis commented Jul 1, 2020

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
Copy link
Author

lenneis commented Jul 2, 2020

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
Copy link
Author

lenneis commented Jul 3, 2020

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.

@taion
Copy link
Owner

taion commented Jul 3, 2020

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.

@lenneis
Copy link
Author

lenneis commented Jul 3, 2020

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.

@GiovanniCapizzi
Copy link

any news?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants