英文:
Graphql-request using typescript webpack config set up
问题
我有一个用TypeScript编写的REST API,其中一个下游API是GraphQL。我正在使用graphql-request来消耗下游的GraphQL API。
package.json中的版本:
"graphql": "^16.6.0",
"graphql-request": "^6.1.0",
我的代码如下:
```typescript
import { GraphQLClient, gql } from 'graphql-request';
export const getUser = async (authToken: AuthToken) => {
const endpoint = `${process.env.Url}`;
const query: string = gql`
query {
getUserInfo(Id: "5fc2ca90")
{
name
phone
}
}`;
const client = new GraphQLClient(endpoint, { headers: { authorization: `Bearer ${authToken.value}` } });
client.request(query).then((data) => { console.log(data) });
}
我在终端中看到构建错误:
ERROR in ./node_modules/graphql-request/build/cjs/index.js 108:57
Module parse failed: Unexpected token (108:57)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| variables,
| operationName,
> jsonSerializer: fetchOptions.jsonSerializer ?? defaultJsonSerializer_js_1.defaultJsonSerializer,
| });
| }
Webpack配置:
const path = require('path');
module.exports = {
entry: {
MyLambda: 'LambdaPath',
,
target: 'node',
output: {
path: path.resolve(__dirname, 'terraform', 'dist'),
filename: '[name].js',
libraryTarget: 'commonjs2'
},
mode:'production',
resolve: {
extensions: ['.tsx', '.ts', '.js', '.json']
},
module: {
rules: [
{
test: /\.tsx?$/,
use: ['ts-loader'],
exclude: /node_modules/
}
]
},
externals: [/^aws-sdk/]
};
<details>
<summary>英文:</summary>
I have a rest api written in typeScript and one of my downstream API is in Graphql. I am using graphql-request to consume the downstream graphql api.
Version in package.json
"graphql": "^16.6.0",
"graphql-request": "^6.1.0",
and my code is
import { GraphQLClient, gql } from 'graphql-request'
export const getUser = async (authToken: AuthToken) => {
const endpoint = `${process.env.Url}`;
const query: string = gql`
query {
getUserInfo(Id: "5fc2ca90")
{
name
phone
}
}`;
const client = new GraphQLClient(endpoint, { headers: { authorization: `Bearer ${authToken.value}` } });
client.request(query).then((data) => { console.log(data) });
I am seeing build error in my terminal
ERROR in ./node_modules/graphql-request/build/cjs/index.js 108:57
Module parse failed: Unexpected token (108:57)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| variables,
| operationName,
> jsonSerializer: fetchOptions.jsonSerializer ?? defaultJsonSerializer_js_1.defaultJsonSerializer,
| });
| }
Webpack.config
const path = require('path');
module.exports = {
entry: {
MyLambda: 'LambdaPath',
,
target: 'node',
output: {
path: path.resolve(__dirname, 'terraform', 'dist'),
filename: '[name].js',
libraryTarget: 'commonjs2'
},
mode:'production',
resolve: {
extensions: ['.tsx', '.ts', '.js', '.json']
},
module: {
rules: [
{
test: /.tsx?$/,
use: ['ts-loader'],
exclude: /node_modules/
}
]
},
externals: [/^aws-sdk/]
};
</details>
# 答案1
**得分**: 1
我修复了这个问题,安装了 graphql-request 的版本 5.1.0。
<details>
<summary>英文:</summary>
I fixed this installing version 5.1.0 of graphql-request.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论