Search code examples
kotlingraphqlaws-appsync

How can I extend my graphql object into my Kotlin object?


When I retrieve a list of Ingredient objects from AWS Appsync, it comes back to me in the form of a list of ListIngredientsQuery.Item.

I'd like to my Ingredient object in my Kotlin code to extend the ListIngredientsQuery.Item so I can retrieve a list of Ingredients from the server instead, and perform the functions that are relevant to my Ingredient object on the members therein.

I used the : in my data class, to extend ListIngredientQuery.item, but when I get my list from the server, if I try to get it as a list of Ingredients, I get a type mismatch: required:List<Ingredient> found: <ListIngredientsQuery.item!> error.

If I was in Java I would just use the "Extends" keyword.

my function to get the list:

fun getIngredients() {
    mAWSAppSyncClient!!.query(ListIngredientsQuery.builder().build())
        .responseFetcher(AppSyncResponseFetchers.CACHE_AND_NETWORK)
        .enqueue(ingredientsCallback)
}


private val ingredientsCallback = object:GraphQLCall.Callback<ListIngredientsQuery.Data>() {
    override fun onResponse(response: com.apollographql.apollo.api.Response<ListIngredientsQuery.Data>) {
        var mIngredientsList: List<Ingredient>
        mIngredientsList = response.data()!!.listIngredients()!!.items()!!.toList()
        mIngredientsList!!.forEach {
            Log.d("All Ingredients: ", it.name())}
    }
    override fun onFailure(e: ApolloException) {
        Log.e("Error", e.toString())
    }
}

Ingredient.kt:

import com.amazonaws.amplify.generated.graphql.ListIngredientsQuery

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

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
    getIngredientByName(name: String!): 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

  • Even though I don't understand, with the given code, why you would want to change from IngredientsQuery.Item to Ingredient I think you are missing an explicit cast in this line:

        mIngredientsList = response.data()!!.listIngredients()!!.items()!!.toList()
    

    like this:

        mIngredientsList = response.data()!!.listIngredients()!!.items()!!.toList() as List<Ingredient>
    

    That should fix your problem.