【发布时间】:2019-10-12 18:11:30
【问题描述】:
我正在使用 gatsby、typescript 和 apollo(用于 graphql 查询)创建一个新的 react 应用。
在 dev 中测试时,应用程序编译并运行,没有抛出任何错误。
当我使用“gatsby build”转换构建时,它失败并出现错误。
我不明白为什么或在哪里触发了它。这似乎与 webpack 在构建时检查的方式有关,但我不知道如何查明问题,而且似乎没有任何材料可以为我提供明确的答案。
好像是httplink模块造成的。出现在任何 .tsx 文件中时触发错误的代码是:
import { HttpLink } from 'apollo-link-http'
const link = new HttpLink({
uri: 'http://localhost:3001/graphql'
})
显示的错误如下:
error Building static HTML failed
See our docs page on debugging HTML builds for help https://gatsby.dev/debug-html
10 | function InvariantError(message) {
11 | if (message === void 0) { message = genericMessage; }
> 12 | var _this = _super.call(this, typeof message === "number"
| ^
13 | ? genericMessage + ": " + message + " (see https://github.com/apollographql/invariant-packages)"
14 | : message) || this;
15 | _this.framesToPop = 1;
WebpackError: Invariant Violation: Invariant Violation: 1 (see https://github.com/apollographql/invariant-packages)
- invariant.esm.js:12 new InvariantError
[lib]/[apollo-link-http-common]/[ts-invariant]/lib/invariant.esm.js:12:1
- bundle.esm.js:64 checkFetcher
[lib]/[apollo-link-http-common]/lib/bundle.esm.js:64:52
- bundle.esm.js:8 createHttpLink
[lib]/[apollo-link-http]/lib/bundle.esm.js:8:17
- bundle.esm.js:139 new HttpLink
[lib]/[apollo-link-http]/lib/bundle.esm.js:139:1
- Strategy.tsx:6 Module../src/components/Strategy.tsx
lib/src/components/Strategy.tsx:6:14
- bootstrap:19 __webpack_require__
lib/webpack/bootstrap:19:1
- bootstrap:19 __webpack_require__
lib/webpack/bootstrap:19:1
- sync-requires.js:10 Object../.cache/sync-requires.js
lib/.cache/sync-requires.js:10:56
- bootstrap:19 __webpack_require__
lib/webpack/bootstrap:19:1
- static-entry.js:9 Module../.cache/static-entry.js
lib/.cache/static-entry.js:9:22
- bootstrap:19 __webpack_require__
lib/webpack/bootstrap:19:1
- bootstrap:83
lib/webpack/bootstrap:83:1
- universalModuleDefinition:3 webpackUniversalModuleDefinition
lib/webpack/universalModuleDefinition:3:1
- universalModuleDefinition:10 Object.<anonymous>
lib/webpack/universalModuleDefinition:10:2"
这是 typescript 问题、gatsby 问题、apollo 问题还是 webpack 问题?还是组合?
感谢您提供的任何帮助!我正在努力理解这里的所有部分。
我了解 Invariant Violations 是关于引用错误类型的问题。因为这发生在模块中,我不确定我是否做错了什么,或者它是否是导入库中的问题。也许这是我强制对基于 JavaScript 的基本库进行打字稿检查的问题。对此我还没有下定论。
我尝试将以下配置添加到 gatsby-node.js 以忽略模块检查(此处建议:https://gatsby.dev/debug-html),但没有成功构建,尽管错误确实发生了变化,因为它看不到模块。
exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
if (stage === "build-html") {
actions.setWebpackConfig({
module: {
rules: [
{
test: /apollo-link-http/,
use: loaders.null(),
},
],
},
})
}
}
回顾一下,这是用于创建客户端对象以启用对 graphql 端点的查询的代码。运行 'gatsby build' 时会出现 in variant 错误(见上文)。
import * as React from 'react'
import { ApolloClient } from 'apollo-client'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { HttpLink } from 'apollo-link-http'
const cache = new InMemoryCache()
const link = new HttpLink({
uri: 'http://localhost:3001/graphql'
})
const client = new ApolloClient({
cache,
link
})
【问题讨论】:
-
你试过
import { createHttpLink } from 'apollo-link-http';吗? -
是的,实际上,我确实尝试过。它仍然触发了同样的错误。我也试过:
import ApolloClient from 'apollo-boost' const client = new ApolloClient({ uri: 'https://48p1r2roz4.sse.codesandbox.io'})但它似乎触发(或引用)了同样的问题。
标签: reactjs typescript webpack apollo gatsby