Skip to content

Resolver with a lambda which returns a generic #139

@zypus

Description

@zypus

I have many different resources for which I define the same kind of queries or mutations, ie. getting all entities, getting an entity by id, getting the count etc.

So I tried to write a generic function which constructs these queries on the SchemaBuilder, something like this:

fun <T> SchemaBuilder.sharedQueries(resourceName: String, repository: Repository<T>)  {
   query("${resourceName}s") {
       resolver { ctx: Context ->
           repository.getAll()
       }
   }

   query(resourceName) {
       resolver { id: Int, ctx: Context ->
           repository.get(id)
       }
   }

   query("${resourceName}Count") {
      resolver { ctx: Context ->
          repository.count()
      }
   }
}

Which is then called like so:

schema {
    sharedQueries<User>("user", userRepository)
    sharedQueries<Role>("role", roleRepository)
    // etc.
}

However, this fails because the generic is not yet resolved when the reflection is at work, resulting in this exception:

Caused by: kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Container of deserialized member is not resolved: local final fun <T> <anonymous>(id: Int, ctx: com.apurebase.kgraphql.Context): T[DeserializedSimpleFunctionDescriptor@3ff57625]
	at kotlin.reflect.jvm.internal.KTypeParameterImpl.getContainerClass(KTypeParameterImpl.kt:79)

Inlining also doesn't help.

So my question is, is there some other way to avoid writing structurally identical queries all the time?

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