Skip to content

Unable to generate projection for an interface #855

Description

@martinvisser

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.

Activity

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions