Skip to content
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

Fragments on interfaces should have exposed generated types #265

Open
twavv opened this issue Jan 19, 2024 · 1 comment
Open

Fragments on interfaces should have exposed generated types #265

twavv opened this issue Jan 19, 2024 · 1 comment
Labels
decision needed decision needed Sounds like good idea, but will need closer scrutiny for final decision. roadmap

Comments

@twavv
Copy link

twavv commented Jan 19, 2024

Currently, creating a fragment on an interface type generally results in that type being "inlined" into the Pydantic models where they're used.

# Schema
interface Actor {
  id: ID!
  username: String!
  # ...
}
type User implements Actor { ... }
type Bot implements Actor { ... }

type Query {
  actor(id: ID!): Actor
}


# Queries
fragment ActorInfo on Actor {
  id
  ... on User { ... }
  ... on Bot { ... }
}

query GetActor($id: ID!) {
  actor(id: $id) { ...ActorInfo }
}

results in the generated code akin to

class GetActor(BaseModel):
    actor: Optional[GetActorActorActor | GetActorActorUser | GetActorActorBot]

(i've omitted some of the less interesting bits)

It would be nice if this was exposed directly in the generated fragments file that way it can be referenced in other places in the code that consumes the GraphQL API.


The hardest thing with this approach is that it's hard to combine with Python inheritance I think.

For example, if the query above didn't modify the fragment but added username to the result set in the query, it wouldn't be possible to inherit from the union to extend the type with the additional username field. This library doesn't currently use inheritance for that anyway,,, but,,, yeah.

Just ran into this issue and though I could post this for posterity at least.

@rafalp rafalp added roadmap decision needed decision needed Sounds like good idea, but will need closer scrutiny for final decision. labels Jan 25, 2024
@rafalp
Copy link
Contributor

rafalp commented Jan 25, 2024

I agree with the usecase for this, but I am marking this as "decision needed" because its something that I have no idea currently how we can achieve in the codegen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
decision needed decision needed Sounds like good idea, but will need closer scrutiny for final decision. roadmap
Projects
None yet
Development

No branches or pull requests

2 participants