1

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.

I have a graphql schema that looks like -

schema {
    query: Query
}

type Query {
    events: [Event]
}

type Event {
   ...
}

I am using useQuery to return all events, like -

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

EVENTS_LIST_HOME comes from -

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

export default interface IEvent {
    title: string;
}

but I am getting the error -

enter image description here

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

What am I missing? How do I use the grapghql type of Event in typescript?

2
  • How do you generate your typescript types? TypeScript has structural typing, meaning that types shapes need to be compatible - names don't matter. The easiest thing might be to simply omit the annotation. The type seems to be inferred. Commented Apr 26, 2020 at 19:31
  • @Herku I currently don't generate them. I was doing them manually. Should I be generating them? Unfortunately I wasn't getting any intellisense for event in my IDE when i omit the type in the map callback Commented Apr 26, 2020 at 22:06

1 Answer 1

2

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.

It looks like there is a type mismatch between Event and IEvent. Can't say much without seeing the definition of Event.

Using the types generated by apollo codegen should fix the type errors.

Sign up to request clarification or add additional context in comments.

2 Comments

i ended up using graphql-codegen (npmjs.com/package/@graphql-codegen/cli) in the end, thanks!
I'd recommend using apollo cli because it's the official one and works well with react-apollo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.