graphql-tag: Typescript: import fail: Cannot find module 'query.gql'
This is a reopen of https://github.com/apollographql/graphql-tag/issues/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...
. Using require
instead of import
, 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 The query
DocumentNode returned above includes a default
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 the tsconfig.json
setting:
"allowSyntheticDefaultImports": true
I need it for other reasons, and was unable to easily test with it turned off.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 28
- Comments: 33 (2 by maintainers)
Commits related to this issue
- attempt at extracting gql files from .ts some ressources I have (partially) read: - https://www.the-guild.dev/blog/graphql-with-typescript-done-right - https://github.com/apollographql/graphql-tag/is... — committed to GaloyMoney/galoy-client by nicolasburtey 2 years ago
- chore: attempt at extracting .graphql from .ts 2 ressources I have (partially) read: - https://github.com/apollographql/graphql-tag/issues/59 - https://www.the-guild.dev/blog/graphql-with-typescript-... — committed to GaloyMoney/galoy-client by nicolasburtey 2 years ago
- chore: attempt at extracting .graphql from .ts 2 ressources I have (partially) read: - https://github.com/apollographql/graphql-tag/issues/59 - https://www.the-guild.dev/blog/graphql-with-typescript-... — committed to GaloyMoney/galoy-client by nicolasburtey 2 years ago
- chore: attempt at extracting .graphql from .ts 2 ressources I have (partially) read: - https://github.com/apollographql/graphql-tag/issues/59 - https://www.the-guild.dev/blog/graphql-with-typescript-... — committed to GaloyMoney/galoy-client by nicolasburtey 2 years ago
This solution works for me. It’s similar to what @ddetkovs posted earlier but allows you to use the
import
syntax and doesn’t require using/// <reference path="..." />
All of my
.graphql
files are in a single folder so I could add anindex.d.ts
in that folder with the followingHaving all of the
.graphql
files in a single directory and naming itindex.d.ts
avoids the need to add/// <reference path="./graphql.d.ts" />
in other files.I can then import using
import * as MyQuery from "../graphql/MyQuery.graphql"
Thanks to @ddetkovs, @Aides359
Changing the solution by @ddetkovs to
works for ES2015 import.
I found that this works:
then, when I need to import *.graphql
I am also having the same issue. I have also tried workarounds mentioned in #42 and here #59 . Also tried adding custom module declaration for gql files and playing around with alternatives.
Workarounds and outputs are like these:
query.gql
index.t.ds
import = require
syntax.DocumentNode
from graphql typings.I can live with typescript error
Argument of type 'typeof "*.gql"' is not assignable
since it does not break actual usage, but it would be great to fix that in some way.I have tried both
"allowSyntheticDefaultImports": true
anddelete query['default']
workarounds, could not get an errorless flow for both typescript and browser usage.@buggy thanks. After add d.ts,
tslint
still throw some error:My case is:
tslint
give me some errors:[ts] Property 'CART' does not exist on type 'DocumentNode'.
[ts] Property 'REMOVE_ALL_FROM_COUNT' does not exist on type 'DocumentNode'.
…So I change the
d.ts
like this:It works fine.
tslint
error gone. It’s a little more complex for adding definition for eachquery
.So change
d.ts
again like this:I don’t see it mentioned here, and this is the top google result for this problem. So FYI,
graphql-code-generator
has a plugin to generate these module definitions. It solved the problem for me. https://graphql-code-generator.com/docs/plugins/typescript-graphql-files-modulesI used code from @buggy:
I put this code into
index.d.ts
file into mysrc/@types
folder. Then I add this folder to mytypeRoots
setting intsconfing.json
file:This solution works for me for all
.graphql
files inside mysrc
folder.Faced the problem with importing
.gql
across many directories (same as @switz has mentioned).Solved it with
global.d.ts
. For example, let it be placed insrc
folderBe sure that
src
folder is ininclude
section of yourtsconfig.json
Thanks to @buggy and others.
@Aides359 graphql loader exports schema like this:
module.exports = doc;
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:
http://www.typescriptlang.org/docs/handbook/modules.html
@annguyen0505 I got the same problem and you probably aren’t using graphql-tag/loader in your webpack configuration.
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
create-react-app
).