0
votes

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?

1

1 Answers

0
votes

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