Graphql-request使用TypeScript的Webpack配置设置。

huangapple go评论117阅读模式
英文:

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: &quot;5fc2ca90&quot;) 
        {
            name
            phone
        }
    }`;

const client = new GraphQLClient(endpoint, { headers: { authorization: `Bearer ${authToken.value}` } });
client.request(query).then((data) =&gt; { 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>



huangapple
  • 本文由 发表于 2023年7月27日 21:56:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定