Skip to content

Key Field(s) '[id]' are not queried on XUnion at Operation(XQuery).union #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Tracked by #2331
sonatard opened this issue Aug 10, 2023 · 1 comment
Open
Tracked by #2331

Comments

@sonatard
Copy link

Version

3.8.2

Summary

I have set the extend interface Node @typePolicy(keyFields: "id").

And when I build, the following error occurs.
Key Field(s) '[id]' are not queried on X2 at Operation(XQuery).union

Steps to reproduce the behavior

union X = X1 | X2

type Query {
 x: X!
}

type X1 {
 id: ID!
 name: String
}

type X2 {
 id: ID!
 name: String
}
extend interface Node @typePolicy(keyFields: "id")
query XQuery {
  x {
    ... on X1 {
       id
       name
    }
  }
}
  • build
`Key Field(s) '[id]' are not queried on X2 at Operation(XQuery).union`

I found a workaround. By modifying the Query as follows, the error disappears.

query XQuery {
  x {
    ... on X1 {
       id
       name
    }
    ... on X2 {
       id
    }
  }
}

Logs

Key Field(s) '[id]' are not queried on X2 at Operation(XQuery).union

@sonatard sonatard changed the title Key Field(s) '[id]' are not queried on XUnion at Operation(YQuery).union Key Field(s) '[id]' are not queried on XUnion at Operation(XQuery).union Aug 10, 2023
@martinbonnin
Copy link
Contributor

martinbonnin commented Aug 11, 2023

Hi 👋

The reason this error is thrown is because we can't add id on an union field:

query XQuery {
  x {
    # This does not validate as no field except __typename can be queried on unions
    id 
    ... on X1 {
       name
    }
  }
}

We could add fragments that query id on all types automatically:

query XQuery {
  x {
    # fragments added by the Apollo compiler
    ... on X1 {
       id 
    }
    ... on X2 {
       id 
    }
    # etc... if more types in the union

    # original fragment 
    ... on X1 {
       name
    }
  }
}

But we have always resisted doing so because it felt like a larger modification of the user query that could be surprising.

Do you think adding the fragments automatically would help in that case?

@BoD BoD transferred this issue from apollographql/apollo-kotlin Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants