dgs-codegen icon indicating copy to clipboard operation
dgs-codegen copied to clipboard

Unable to generate projection for an interface

Open martinvisser opened this issue 7 months ago • 1 comments

If we take this as the graph where one interface implements/extends another interface, and generate client code:

type Query {
    profile: Profile
}

type Profile {
    id: ID!
    agreements: [Agreement!]!
}

interface Agreement {
    id: ID!
}

interface Account implements Agreement {
    id: ID!
    name: String!
    balance: Float!
}

type PaymentAccount implements Agreement & Account {
    id: ID!
    name: String!
    balance: Float!
    currency: String!
}

type SavingsAccount implements Agreement & Account {
    id: ID!
    name: String!
    balance: Float!
    interestRate: Float!
}

...then I can generate a request as follows:

val projection = ProfileProjectionRoot<Nothing, Nothing>()
    .id()
    .agreements()
    .onPaymentAccount().balance().id().name()
    .parent()
    .onSavingsAccount().balance().id().name()
    .root()

val query = ProfileGraphQLQuery.Builder().build()
val request = GraphQLQueryRequest(query, projection)

Which generates a query like this:

{
  profile {
    id
    agreements {
      ... on PaymentAccount {
        __typename
        balance
        id
        name
      }
      ... on SavingsAccount {
        __typename
        balance
        id
        name
      }
    }
  }
}

However, this would also be a valid query:

{
  profile {
    id
    agreements {
      ... on Account {
        __typename
        balance
        id
        name
      }
    }
  }
}

But the code generation doesn't allow code like this:

val projection = ProfileProjectionRoot<Nothing, Nothing>()
    .id()
    .agreements()
    .onAccount().balance().id().name() // onAccount doesn't exist
    .root()

However, a tool like GraphiQL does give this option:

Image

Would this be possible to do with the current codegen, or would it be possible to add such an option?

I created an example in https://github.com/martinvisser/dgs-codegen-interfaces to show my idea.

martinvisser avatar Jul 01 '25 07:07 martinvisser

If I understand the codegen correctly it only creates on<NameHere> methods for concrete types, which Account isn't of course.

martinvisser avatar Jul 08 '25 06:07 martinvisser