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
importsyntax and doesn’t require using/// <reference path="..." />All of my
.graphqlfiles are in a single folder so I could add anindex.d.tsin that folder with the followingHaving all of the
.graphqlfiles in a single directory and naming itindex.d.tsavoids 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.gqlindex.t.dsimport = requiresyntax.DocumentNodefrom graphql typings.I can live with typescript error
Argument of type 'typeof "*.gql"' is not assignablesince it does not break actual usage, but it would be great to fix that in some way.I have tried both
"allowSyntheticDefaultImports": trueanddelete query['default']workarounds, could not get an errorless flow for both typescript and browser usage.@buggy thanks. After add d.ts,
tslintstill throw some error:My case is:
tslintgive 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.tslike this:It works fine.
tslinterror gone. It’s a little more complex for adding definition for eachquery.So change
d.tsagain like this:I don’t see it mentioned here, and this is the top google result for this problem. So FYI,
graphql-code-generatorhas 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.tsfile into mysrc/@typesfolder. Then I add this folder to mytypeRootssetting intsconfing.jsonfile:This solution works for me for all
.graphqlfiles inside mysrcfolder.Faced the problem with importing
.gqlacross many directories (same as @switz has mentioned).Solved it with
global.d.ts. For example, let it be placed insrcfolderBe sure that
srcfolder is inincludesection of yourtsconfig.jsonThanks 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).