Skip to content

How to map Set -> List  #1109

@dave-at-season

Description

@dave-at-season

Hello! First off, I understand the reasoning for not supporting the automatic mapping of Kotlin Sets to GraphQL List types as per the docs and the various issue/pr conversations around the topic.

But I am trying to add graphql to an existing Kotlin project that has a collection of domain objects defined as data classes which heavily use Sets. So I will need some way to do this and its not feasible for me to do this mapping manually. Currently I'm trying to do that by maintaining separate copies of these data classes and mapping all of their properties manually. For example:

data class DomainClient(
    val firstName: String,
    val lastName: String,
    // ... many more properties,
    val locations: Set<Location> = emptySet()
)
data class Client(
    val firstName: String,
    val lastName: String,
    // ... many more properties,
    val locations: List<Location> = emptyList()
)
fun clientFromDomain(c: DomainClient): Client = Client(
    firstName = c.firstName,
    lastName = c.lastName,
    // ... many more properties,
    locations = c.locations.toList()
)

@Provider
class ClientsQuery @Inject constructor(
    private val clientService: ClientService
) : Query {
    fun clients(context: CustomGraphQLContext): List<Client> {
        return clientService.getClients(context.user.accountId)
            .map { clientFromDomain(it) }
    }
}

Which is exceedingly yucky given the number of classes/fields I need to map across this project.

All I really want is a way to automate this mapping, I don't even necessarily want that reflected in the resulting schema. I'm fine with the schema outputting these fields as a List type, and then serializing/deserializing these fields into Sets behind the scenes as long as I don't have to do it manually.

It seems like functionality was introduced to accommodate this workflow as discussed in this issue: #584 but I don't understand how generateAdditionalTypes would be used here.

I would greatly appreciate any help on this

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions