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
Typescript: import fail: Cannot find module 'query.gql' #59
Comments
I am also having the same issue. I have also tried workarounds mentioned in #42 and here #59 . Workarounds and outputs are like these:
import query from './query.gql'
/* =>
Ts output: `error TS2307: Cannot find module './query.gql'.`
Browser console output: -
*/
// index.d.ts
declare module "*.gql" {
const content: any;
export default content;
}
// component.tsx
import query from './query.gql'
/* =>
Ts output: Fine / No error
Browser console output: browser.js:40 Uncaught Error: Argument of undefined passed to parser was not a valid GraphQL DocumentNode. You may need to use 'graphql-tag' or another method to convert your operation into a document
*/
// index.d.ts
// // ...same as 2
// component.tsx
import query = require('./query.gql')
/* =>
Ts output: `error TS2345: Argument of type 'typeof "*.gql"' is not assignable to parameter of type 'DocumentNode'.
Property 'kind' is missing in type 'typeof "*.gql"'.`
Browser console output: Fine / No error (actually works)
*/
// index.d.ts
import {
DocumentNode
} from 'graphql'
declare module "*.gql" {
const content: DocumentNode;
export default content;
}
// component.tsx
import query = require('./query.gql')
/* =>
Ts output: `error TS2307: Cannot find module './query.gql'.`
Browser output: -
*/ I can live with typescript error I have tried both |
I found that this works:
then, when I need to import *.graphql
|
Changing the solution by @ddetkovs to
works for ES2015 import. |
@Aides359 graphql loader exports schema like this: This module is then resolved with webpack and since you're using es2015 import syntax it tries to access its default member, which is undefined, thus causing a runtime error. I get the following generated code:
Also, typescript handbook says this:
|
@ddetkovs odd, my described setup works in my project, without any runtime errors 🤔 |
Any updates on this? |
unfortunately i dont know enough about typescript to know what the solution space for this issue might be like. if there's any chance someone can send me a repo (or just some code) that exhibits exactly this issue, i can look into figuring out how to resolve this |
@ddetkovs this fell of my radar, however I have now taken a look at my generated output. While I don't know about
which works just fine. |
@Aides359 what I meant is when graphql-tag/loader compiles your query, it exports it as: when you do: This works too: |
This solution works for me. It's similar to what @ddetkovs posted earlier but allows you to use the All of my declare module '*.graphql' {
import {DocumentNode} from 'graphql';
const value: DocumentNode;
export = value;
} Having all of the I can then import using Thanks to @ddetkovs, @Aides359 |
Any updates here? |
@gustavoalfaro Solution by @Aides359 works for me. |
@nitingupta910 Changing this: |
Did you include the What error do you see? |
Facing the same issue. Any update on this? |
again, unfortunately i dont know enough about typescript in order to figure out what i can do here. please open a PR to provide better documentation about TypeScript usage if that is the general consensus on how to avoid other folks getting hit by this. closing for now, but please re-open if there's more to discuss. |
+1 |
Could anyone having issues with this please post the error message they're getting? I had trouble implementing the above solution because I didn't have the |
Could someone upload a reproducible example of it working? I am getting this error:
|
what is the current status on this issue? import * as type from 'type.grpahql';
// it get me the files like I want
// but gets my this error on console and vscode [ts] Cannot find module './type.graphql' I think this is more in tsconfig problem if anyone can get me some guide I will be great Solved |
@buggy thanks. After add d.ts, My case is: import * as Q from 'gqlMod/queries/library.gql';
import * as M from 'gqlMod/mutations/library.gql';
//...
//...
export default compose(
graphql(Q.CART, {
props: ({ data: { cart } }) => {
return {
cart
};
}
}),
graphql(M.REMOVE_ALL_FROM_COUNT, { name: 'removeAllFromCart' }),
graphql(M.ADD_TO_CART, { name: 'addToCart' }),
graphql(M.REMOVE_FROM_CART, { name: 'removeFromCart' }),
graphql(M.REMOVE_COUNT_FROM_CART, { name: 'removeCountFromCart' })
)(Cart);
So I change the declare module '*.gql' {
import { DocumentNode } from 'graphql';
const value: {
CART: DocumentNode;
REMOVE_ALL_FROM_COUNT: DocumentNode;
ADD_TO_CART: DocumentNode;
REMOVE_FROM_CART: DocumentNode;
REMOVE_COUNT_FROM_CART: DocumentNode;
};
export = value;
} It works fine. So change declare module '*.gql' {
import { DocumentNode } from 'graphql';
const value: {
[key: string]: DocumentNode;
};
export = value;
} |
i got the imported module is just a string? just like that "/static/media/postsByUser.79323037.graphql" |
@buggy thank you for the solution! I'm still having an issue because my GraphQL client (Apollo) is expecting a object typed GraphQLSchema by using your method I just get a DocumentNode.
Do you know how to fit to this requirement? Thank you! Note: I tried to set GraphQLSchema instead of DocumentNode in the |
@annguyen0505 Check out react-app-rewired with react-app-rewire-graphql-tag so you don't need to eject your react app (assuming you have a project created by |
@nemcek Can you elaborate on “you probably aren't using graphql-tag/loader in your webpack configuration”? My webpack configuration looks just like:
I’ve added the following as per this comment: declare module '*.graphql' {
import {DocumentNode} from 'graphql';
const value: DocumentNode;
export = value;
} Which does get rid of my compile-time errors, but I still get hit with the following at runtime:
Any thoughts would be appreciated, I really have no idea how to get past this. |
The solution works for me, but does anyone have a solution that works across many directories? My codebase is based on a module structure so the graphql tags are not flat in a single directory. |
I've tried the solutions listed here and I simply cannot get this to work. I receive the following error, no matter what:
I have tried placing the Here is the tsconfig:
|
@JChapple it would help if you post a link to your file structure (e.g. output of |
I don't see it mentioned here, and this is the top google result for this problem. So FYI, |
Faced the problem with importing Solved it with // src/global.d.ts
declare module '*.gql' {
import { DocumentNode } from 'graphql';
const value: DocumentNode;
export = value;
} Be sure that // tsconfig.json
{
// ...
"include": ["src/**/*.ts"]
} Thanks to @buggy and others. |
I'm facing the same issue, any ideas? tsconfig.json
webpack.config.js
usage:
|
if you having trouble importing gql files with jest I solved by using @ddetkovs solution plus adding this to jest config. Hope this helps. |
I used code from @buggy:
I put this code into
This solution works for me for all |
some ressources I have (partially) read: - https://www.the-guild.dev/blog/graphql-with-typescript-done-right - apollographql/graphql-tag#59
2 ressources I have (partially) read: - apollographql/graphql-tag#59 - https://www.the-guild.dev/blog/graphql-with-typescript-done-right
2 ressources I have (partially) read: - apollographql/graphql-tag#59 - https://www.the-guild.dev/blog/graphql-with-typescript-done-right
2 ressources I have (partially) read: - apollographql/graphql-tag#59 - https://www.the-guild.dev/blog/graphql-with-typescript-done-right
This is a reopen of #42 .
Nothing has changed, it's just that the presence of a workaround is not a fix.
As described before, attempts to import a graphql module result in
Cannot find module...
. Usingrequire
instead ofimport
, eg:const query = require('gql/Query.gql');
works fine... up to a point. Somewhere, the Apollo
graphql
decorator performs a deep copy of the query Thequery
DocumentNode returned above includes adefault
element that has itself as a property. As a result, the deep copy blows out with a soft stack overflow. So the full workaround is:const query = require('gql/Query.gql'); delete query['default']
It's possible that the
default
problem is dependent on thetsconfig.json
setting:"allowSyntheticDefaultImports": true
I need it for other reasons, and was unable to easily test with it turned off.
The text was updated successfully, but these errors were encountered: