Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDb ObjectId in serialized data class error " An Object type must define one or more fields. Found none on type Id" #181

Open
ronjunevaldoz opened this issue Feb 26, 2022 · 2 comments

Comments

@ronjunevaldoz
Copy link

Having an error com.apurebase.kgraphql.schema.SchemaException: An Object type must define one or more fields. Found none on type Id

Mongo User Document Model

@Serializable
data class User(
    @SerialName("_id")
    @Contextual val id: Id<User> = newId(),
    val username: String,
    val password: String,
)

Schema Type

type<Id<User>>()
type<User>()
@PssbleTrngle
Copy link

Should the ObjectId of a model not rather be defined in graphql using a scalar and mapping it to a string?

@poovarasanvasudevan
Copy link

Solved with

schema {
        configure {
            genericTypeResolver = object: DefaultGenericTypeResolver() {
                override fun resolveMonad(type: KType): KType = when {
                    type.jvmErasure.isSubclassOf(Id::class) -> type.arguments[0].type!!.withNullability(type.isMarkedNullable)
                    else -> super.resolveMonad(type)
                }

                override fun unbox(obj: Any): Any? = when(obj) {
                    is Id<*> -> obj.toString()
                    else -> super.unbox(obj)
                }
            }
        }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants