首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rtk-查询-graphql-请求和graphql-请求的类型不匹配

rtk-查询-graphql-请求和graphql-请求的类型不匹配
EN

Stack Overflow用户
提问于 2022-10-17 18:07:42
回答 2查看 89关注 0票数 1

我试图用GraphQL构建rtk查询api。但是,似乎@rtk-query/graphql-request-base-querygraphql-request之间的包类型不匹配,我得到的错误如下;

代码语言:javascript
复制
Type 'import("path/to/file/node_modules/graphql-request/dist/index").GraphQLClient' is not assignable to type 'import("path/to/file/node_modules/@rtk-query/graphql-request-base-query/node_modules/graphql-request/dist/index").GraphQLClient'.
  Types have separate declarations of a private property 'url'.ts(2322)

我得到了**client**字段上的错误,我试图删除node_modules并重新安装,不幸的是没有工作.

这是我的api.ts

代码语言:javascript
复制
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import { graphqlRequestBaseQuery } from '@rtk-query/graphql-request-base-query'; 
import { GraphQLClient } from 'graphql-request';
import { gqlClient } from '../gql/gql-client';

 
export const api = createApi({
  baseQuery: graphqlRequestBaseQuery({ client: new GraphQLClient('http://localhost:4000/graphql') }), // error is at "client" field
  tagTypes: [],
  endpoints: (builder) => ({
    getUsers: builder.query({
      query: () => '/users',
    }),
  }),
});

export const { useGetUsersQuery } = api;
EN

回答 2

Stack Overflow用户

发布于 2022-11-17 17:42:44

下面是一个使用@rtk-query/graphql-请求基查询版本2.2.0的示例。

代码语言:javascript
复制
import { createApi } from "@reduxjs/toolkit/query/react";
import { graphqlRequestBaseQuery } from "@rtk-query/graphql-request-base-query";
import { ClientError } from "graphql-request";

import { RootState } from "../store";

const baseUrl = process.env.REACT_APP_TECHNOTES_API_HTTP_GRAPHQL;
const techNotesHttpAPI = baseUrl ? baseUrl : "http://localhost:4001/graphql";

const baseQuery = graphqlRequestBaseQuery<
  Partial<ClientError> & { statusCode: number; error: string }
>({
  url: techNotesHttpAPI,
  prepareHeaders: (headers, { getState }) => {
    const state = getState() as RootState;
    const token = state.auth.token;

    if (token) {
      headers.set("Authorization", `Bearer ${token}`);
    }

    return headers;
  },
  customErrors: ({ name, stack, response }) => {
    const {
      message = "",
      statusCode = 500,
      error = "",
    } = response?.errors?.length
      ? response?.errors[0]?.extensions.response
      : {};

    return {
      name,
      message,
      statusCode,
      error,
      stack,
    };
  },
});

export const apiSlice = createApi({
  reducerPath: "api",
  baseQuery,
  tagTypes: ["User", "Note"],
  endpoints: () => ({}),
});
票数 0
EN

Stack Overflow用户

发布于 2022-11-22 17:47:17

我在遵循代码-gen文档时遇到了同样的问题。为了添加@dwight建议的内容,您还可以复制@rtk-query/graphql-request-base-query包的代码文件并将它们放在项目中。从那里导入graphqlRequestBaseQuery

@rtk-query/graphql-request-base-queryGraphQLClient内部导入GraphQLClient,类型错配可能是因为依赖关系尚未在官方存储库上更新。

希望这能帮上忙

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74101408

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档