简体   繁体   中英

Apollo-tooling: including a defined type in another defined type

This may be a basic Graphql question or it may be related Apollo Tooling.

I am trying to use Apollo Tooling to generate typescript types from my client side schema. I have a NavItem type which looks like this:

type NavItem {
  id: ID!
  to: String!
  icon: String!
  text: String!
  highlight: String!
  children: [NavItemChild]
}

type NavItemChild {
  id: ID!
  to: String!
  icon: String!
  text: String!
  highlight: String!
}

Basically a NavItem can have multiple NavItemChildren . When I go to generate types using apollo codegen:generate src/graphql/types --target=typescript --outputFlat I get an error

Field "children" of type "[NavItemChild]" must have a selection of subfields. Did you mean "children { ... }"?

What am I doing wrong and how should I correct it?

The problem was in the query I was trying to generate types for:

It looks, in part, like this:

links {
  to,
  icon,
  text,
  highlight,
  children,
}

Since we declared children as a non primitive type ( String , Int etc) its expected that we define the sub fields that we expect to get back. Therefore changing it to

        links {
          to,
          icon,
          text,
          highlight,
          children {
            to,
            icon,
            text,
            highlight,
          }
        }

works fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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