Typescript and Graphql Fragments

Typescript and Graphql Fragments


0

I use codegen to generate TS types and I use Apollo client to send queries to server.

when I generate code for the following example, Typescript dosen’t know that people has firstName and lastName fields, It only knows that avatar field exists. If I remove fragment and move fields directly to query all fields are usable.

What should I do to support fragments correctly?

fragments/person.graphql

fragment NameParts on Person {
  firstName
  lastName
}

queries/person.ts

import { graphql } from '@/gql'

export const getPersonDocument = graphql(`
   query GetPerson {
     people(id: "7") {
       ...NameParts
       avatar(size: LARGE)
     }
   }
`)

'@/gql' is the output directory of codegen

2

  • I think the frag should be in a separate file or something from what I'm looking at on Google.

    – kelsny

    46 mins ago

  • This is just an example , my fragments are in separate files. I have updated question to clarify that they are not in the same file.

    – A.A

    21 mins ago


Load 5 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *