I have followed the documentation to integrate a .graphql file loader into an existing Angular application. My application is running on Angular 8.2.14 and was setup with Angular CLI.
I'm importing a file containing a query like this:
import { configuratorDataQuery } from '../graphql/configurator-data.graphql';
which throws an error:
ERROR in src/app/configurator/services/configurator-data.service.ts(8,39): error TS2307: Cannot find module '../graphql/configurator-data.graph
ql'.
I've setup the custom-webpack builder in angular.json for the serve, build and karma schemas:
angular.json
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"offag": {
"projectType": "application",
"schematics": {
"@schematics/angular:component": {
"style": "scss",
"skipTests": true
},
"@schematics/angular:class": {
"skipTests": true
},
"@schematics/angular:directive": {
"skipTests": true
},
"@schematics/angular:guard": {
"skipTests": true
},
"@schematics/angular:module": {
"skipTests": true
},
"@schematics/angular:pipe": {
"skipTests": true
},
"@schematics/angular:service": {
"skipTests": true
}
},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"outputPath": "dist/offag",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"aot": false,
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles/main.dev.scss"
],
"stylePreprocessorOptions": {
"includePaths": [
"src/styles"
]
},
"scripts": [
{
"input": "node_modules/document-register-element/build/document-register-element.js"
}
],
"customWebpackConfig": {
"path": "./extra-webpack.config.js"
}
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
},
{
"replace": "src/styles/main.dev.scss",
"with": "src/styles/main.prod.scss"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "6kb",
"maximumError": "10kb"
}
]
},
"integration": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.integration.ts"
},
{
"replace": "src/styles/main.dev.scss",
"with": "src/styles/main.prod.scss"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "6kb",
"maximumError": "10kb"
}
]
},
"local": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.local.ts"
},
{
"replace": "src/styles/main.dev.scss",
"with": "src/styles/main.prod.scss"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "6kb",
"maximumError": "10kb"
}
]
}
}
},
"serve": {
"builder": "@angular-builders/custom-webpack:dev-server",
"options": {
"browserTarget": "offag:build",
"customWebpackConfig": {
"path": "./extra-webpack.config.js"
}
},
"configurations": {
"production": {
"browserTarget": "offag:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "offag:build"
}
},
"test": {
"builder": "@angular-builders/custom-webpack:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles/main.dev.scss"
],
"stylePreprocessorOptions": {
"includePaths": [
"src/styles"
]
},
"scripts": [],
"customWebpackConfig": {
"path": "./extra-webpack.config.js"
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"tsconfig.app.json",
"tsconfig.spec.json",
"e2e/tsconfig.json"
],
"exclude": [
"**/node_modules/**"
]
}
},
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "offag:serve"
},
"configurations": {
"production": {
"devServerTarget": "offag:serve:production"
},
"integration": {
"devServerTarget": "offag:serve:production"
},
"local": {
"devServerTarget": "offag:serve:production"
}
}
}
}
}
},
"defaultProject": "offag"
}
My webpack config looks like:
console.log('Appending custom webpack config');
module.exports = {
module: {
rules: [{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: 'graphql-tag/loader'
}]
}
};
I can confirm it's applied because the log is printed to the console.
Intended outcome:
Even if the documentation is quite rudimentary and not in detail for Angular or Angular CLI, I would expect it to work, as my Webpack configuration is applied and thus, the only thing is in place (webpack configuration).
Actual outcome:
I'm receiving an error:
ERROR in src/app/configurator/services/configurator-data.service.ts(8,39): error TS2307: Cannot find module '../graphql/configurator-data.graph
ql'.
When prefixing the import statement with // @ts-ignore the import works. However, then, in the browser console an error will be thrown:
ERROR Error: "Uncaught (in promise): Invariant Violation: Expecting a parsed GraphQL document. Perhaps you need to wrap the query string in a "gql" tag? http://docs.apollostack.com/apollo-client/core.html#gql
ERROR in src/app/configurator/services/configurator-data.service.ts(8,39): error TS2307: Cannot find module '../graphql/configurator-data.graphql'.
You receive it because TypeScript doesn't understand what *.graphql files are. TS tries to find ../graphql/configurator-data.graphql.ts and it's not there.
One thing you could do is to add declare module '*.graphql' somewhere in the *.d.ts files.
The graphql-tag/loader part is just to create a module in place of ../graphql/configurator-data.graphql that exports an object which is your parsed query. Try to use a default export there.
For anyone stumbling on this thread as I did, here is a nice explanation of the additional steps to take: https://dev.to/open-graphql/how-to-resolve-import-for-the-graphql-file-with-typescript-and-webpack-35lf
(could be added to the docs btw)
@qortex so please add it :)
Most helpful comment
You receive it because TypeScript doesn't understand what
*.graphqlfiles are. TS tries to find../graphql/configurator-data.graphql.tsand it's not there.One thing you could do is to add
declare module '*.graphql'somewhere in the*.d.tsfiles.The
graphql-tag/loaderpart is just to create a module in place of../graphql/configurator-data.graphqlthat exports an object which is your parsed query. Try to use a default export there.