将 graphql 枚举映射到 kotlin 枚举

mapping a graphql enum to a kotlin enum

我对 Graphql 和 Kotlin 都很陌生,我遇到了一个问题。我有一个名为 Ingredient 的对象(kotlin 中的数据 Class,输入 graphql)。成分有一个名称和一个 ID,都是字符串,还有素食和无麸质值,都是枚举。

我遇到的问题是,当我尝试创建带有突变的成分时,我发现 Vegan 和 GlutenFree 的类型不匹配,它期望 "type.Vegan" 并找到 "me.paxana.myapplication.models.Vegan"

这是我用来创建成分的函数。由于上述类型不匹配,其中 ingredient.vegan 和 ingredient.gf 带有下划线。

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)
}

这是成分数据class:

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

这是我的素食枚举 class

enum class Vegan {
    VEGAN, NONVEGAN, UNKNOWN
}

这是我的 graphQL 架构

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
}

好的,所以在我的项目 运行 amplify codegen 之后,我有一个 type.Vegan 和 type.GlutenFree 我可以使用的枚举,所以我摆脱了我的本地 kotlin枚举并制作 Ingredient 对象的属性 type.Vegan 和 type.GlutenFree.

很有魅力。