Search code examples
kotlinenumsgraphqlaws-appsync

mapping a graphql enum to a kotlin enum


I'm fairly new to both Graphql and Kotlin, and I'm having an issue. I have an object (Data Class in kotlin, type in graphql) called Ingredient. Ingredient has a name and an ID, both strings, and a vegan and glutenfree value, both enums.

The problem that I'm having is that when I try to create an ingredient with a mutation, I get a type mismatch for Vegan and GlutenFree where it expects "type.Vegan" and finds "me.paxana.myapplication.models.Vegan"

This is the function I'm using to create the ingredient. In it ingredient.vegan and ingredient.gf are underlined because of the above mentioned type mismatch.

fun createIngredient(ingredient: Ingredient ) {
    val createIngredientInput = CreateIngredientInput.builder().name(ingredient.name).vegan(ingredient.vegan).gf(ingredient.gf).build()
    mAWSAppSyncClient!!.mutate(CreateIngredientMutation.builder().input(createIngredientInput).build())
        .enqueue(mutationCallback)
}

This is the ingredient data class:

data class Ingredient(val id: String, val name: String, val vegan: Vegan = Vegan.UNKNOWN, val gf: GlutenFree = GlutenFree.UNKNOWN ) 

this is my enum class for Vegan

enum class Vegan {
    VEGAN, NONVEGAN, UNKNOWN
}

Here's my graphQL schema

input CreateIngredientInput {
    name: String!
    vegan: Vegan
    gf: GlutenFree
}

input DeleteIngredientInput {
    id: ID!
}

enum GlutenFree {
    GLUTENFREE
    CONTAINSGLUTEN
    UNKNOWN
}

type Ingredient {
    id: ID!
    name: String!
    vegan: Vegan
    gf: GlutenFree
}

type IngredientConnection {
    items: [Ingredient]
    nextToken: String
}

input ModelBooleanFilterInput {
    ne: Boolean
    eq: Boolean
}

input ModelFloatFilterInput {
    ne: Float
    eq: Float
    le: Float
    lt: Float
    ge: Float
    gt: Float
    contains: Float
    notContains: Float
    between: [Float]
}

input ModelIDFilterInput {
    ne: ID
    eq: ID
    le: ID
    lt: ID
    ge: ID
    gt: ID
    contains: ID
    notContains: ID
    between: [ID]
    beginsWith: ID
}

input ModelIntFilterInput {
    ne: Int
    eq: Int
    le: Int
    lt: Int
    ge: Int
    gt: Int
    contains: Int
    notContains: Int
    between: [Int]
}

enum ModelSortDirection {
    ASC
    DESC
}

input ModelStringFilterInput {
    ne: String
    eq: String
    le: String
    lt: String
    ge: String
    gt: String
    contains: String
    notContains: String
    between: [String]
    beginsWith: String
}

type Mutation {
    createIngredient(input: CreateIngredientInput!): Ingredient
    updateIngredient(input: UpdateIngredientInput!): Ingredient
    deleteIngredient(input: DeleteIngredientInput!): Ingredient
}

type Query {
    getIngredient(id: ID!): Ingredient
    listIngredients(filter: TableIngredientFilterInput, limit: Int, nextToken: String): IngredientConnection
}

type Subscription {
    onCreateIngredient(
        id: ID,
        name: String,
        vegan: Vegan,
        gf: GlutenFree
    ): Ingredient
        @aws_subscribe(mutations: ["createIngredient"])
    onUpdateIngredient(
        id: ID,
        name: String,
        vegan: Vegan,
        gf: GlutenFree
    ): Ingredient
        @aws_subscribe(mutations: ["updateIngredient"])
    onDeleteIngredient(
        id: ID,
        name: String,
        vegan: Vegan,
        gf: GlutenFree
    ): Ingredient
        @aws_subscribe(mutations: ["deleteIngredient"])
}

input TableBooleanFilterInput {
    ne: Boolean
    eq: Boolean
}

input TableFloatFilterInput {
    ne: Float
    eq: Float
    le: Float
    lt: Float
    ge: Float
    gt: Float
    contains: Float
    notContains: Float
    between: [Float]
}

input TableIDFilterInput {
    ne: ID
    eq: ID
    le: ID
    lt: ID
    ge: ID
    gt: ID
    contains: ID
    notContains: ID
    between: [ID]
    beginsWith: ID
}

input TableIngredientFilterInput {
    id: TableIDFilterInput
    name: TableStringFilterInput
    vegan: TableBooleanFilterInput
    gf: TableBooleanFilterInput
}

input TableIntFilterInput {
    ne: Int
    eq: Int
    le: Int
    lt: Int
    ge: Int
    gt: Int
    contains: Int
    notContains: Int
    between: [Int]
}

input TableStringFilterInput {
    ne: String
    eq: String
    le: String
    lt: String
    ge: String
    gt: String
    contains: String
    notContains: String
    between: [String]
    beginsWith: String
}

input UpdateIngredientInput {
    id: ID!
    name: String
    vegan: Vegan
    gf: GlutenFree
}

enum Vegan {
    VEGAN
    NONVEGAN
    UNKNOWN
}

Solution

  • Ok, so after I ran amplify codegen on my project I had a type.Vegan and type.GlutenFree enums I could use, so I got rid of my local kotlin enums and made the properties of the Ingredient object type.Vegan and type.GlutenFree.

    Works like a charm.