Skip to content

Another way to generate schema for @oneOf type #2755

Description

@ETCHKILI

Hi, I've been using this library for a while and it's pretty good, but today I'd like to discuss an improvement about schema generation of @oneOf.
According to https://github.com/graphql/graphql-spec/pull/825/files, the @oneOf is merged to main line of graphql-spec.
So I think now is a good time to talk about schema generation of @oneOf.

Currently, according to the doc, the type def below

case class Name(firstName: String, lastName: String)

@GQLOneOfInput
sealed trait AuthorInput
object AuthorInput {
  case class ById(id: String)
  case class ByName(name: Name)
}

will generate schema below

input NameInput {
    firstName: String!
    lastName: String!
}

input AuthorInput @oneOf {
    id: String
    name: NameInput
}

,
which brings limitation that, every kind of AuthorInput should contains exactly one field that is different from others.
Of course we can just wrap fields into new case classes, but that's a bit not ergonomically friendly.

But if we provide an alternative, which generate schema below

input ById {
    id: String!
}

input ByName {
    firstName: String!
    lastName: String!
}

input AuthorInput @oneOf {
    ById: ById
    ByName: ByName
}

for type def below

@GQLOneOfInput
sealed trait AuthorInput
object AuthorInput {
  case class ById(id: String)
  case class ByName(firstName: String, lastName: String)
}

We can enjoy a more ergonomic model derivation.

This is just a preliminary idea, but if it's feasible, I'd love to help implement this feature.
What do you think?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesttoolsIssue related to Caliban tools like code generation or schema comparison

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions