简体   繁体   English

graphql 响应和 Z1E35BCBA2DBA1089C7BD0805D4517C8 之间的 Typescript 和 GraphQL 不匹配类型

[英]Typescript and GraphQL mismatch types between graphql response and typescript type

I am using typescript , react-apollo and AWS Appsync for a react app I am building, but running into typescript warnings which I'm not sure where I am going wrong.我正在为我正在构建的反应应用程序使用typescriptreact-apolloAWS Appsync ,但遇到了 typescript 警告,我不确定我哪里出错了。

I have a graphql schema that looks like -我有一个 graphql 架构,看起来像 -

schema {
    query: Query
}

type Query {
    events: [Event]
}

type Event {
   ...
}

I am using useQuery to return all events, like -我正在使用useQuery返回所有事件,例如-

const {
   loading: eventsListLoading,
   error: eventsListError,
   data: eventsListData
} = useQuery(EVENTS_LIST_HOME);

EVENTS_LIST_HOME comes from - EVENTS_LIST_HOME 来自 -

export const EVENTS_LIST_HOME = gql`
    query eventsListHome{
        events {
            title
        }
    }
`;

and I am trying to render the items like -我正在尝试渲染这些项目 -

<table>
   {
      eventsListData.events.map((event: IEvent) => (
         <tr>
             <td>{event.title}</td>
         </tr>
      ))
   }
</table>

where I have a typescript interface IEvent我有一个 typescript 接口IEvent

export default interface IEvent {
    title: string;
}

but I am getting the error -但我得到了错误 -

在此处输入图像描述

I have tried renaming the typescript type to Event instead of IEvent and adding the other args, index and array, even though they are not used, but same error我尝试将 typescript 类型重命名为Event而不是IEvent并添加其他参数、索引和数组,即使它们没有被使用,但同样的错误

What am I missing?我错过了什么? How do I use the grapghql type of Event in typescript?如何在 typescript 中使用 grapghql 类型的Event

You should use apollo codegen to generate TypeScript types for the types declared in your graphql schema - https://github.com/apollographql/apollo-tooling#apollo-clientcodegen-output .您应该使用apollo codegen 为 graphql 模式中声明的类型生成 TypeScript 类型 - https://github-output.com/apollograph-client

It looks like there is a type mismatch between Event and IEvent .看起来EventIEvent之间存在类型不匹配。 Can't say much without seeing the definition of Event .没有看到Event的定义就不能说太多。

Using the types generated by apollo codegen should fix the type errors.使用apollo codegen生成的类型应该可以修复类型错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM