如何将我的 graphql 对象扩展到我的 Kotlin 对象中?
How can I extend my graphql object into my Kotlin object?
当我从 AWS Appsync 检索成分对象列表时,它以 ListIngredientsQuery.Item
.
列表的形式返回给我
我希望我的 Kotlin 代码中的 Ingredient 对象扩展 ListIngredientsQuery.Item
以便我可以从服务器检索成分列表,并执行与我的 Ingredient 对象相关的功能其中的成员。
我在我的数据 class 中使用了 : 来扩展 ListIngredientQuery.item,但是当我从服务器获取我的列表时,如果我尝试将其作为成分列表获取,我得到type mismatch: required:List<Ingredient> found: <ListIngredientsQuery.item!>
错误。
如果我在 Java 中,我只会使用 "Extends" 关键字。
我获取列表的函数:
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)
我的 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
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
}
即使我不明白,使用给定的代码,为什么您要从 IngredientsQuery.Item
更改为 Ingredient
我认为您在这一行中缺少显式转换:
mIngredientsList = response.data()!!.listIngredients()!!.items()!!.toList()
像这样:
mIngredientsList = response.data()!!.listIngredients()!!.items()!!.toList() as List<Ingredient>
这应该可以解决您的问题。
当我从 AWS Appsync 检索成分对象列表时,它以 ListIngredientsQuery.Item
.
我希望我的 Kotlin 代码中的 Ingredient 对象扩展 ListIngredientsQuery.Item
以便我可以从服务器检索成分列表,并执行与我的 Ingredient 对象相关的功能其中的成员。
我在我的数据 class 中使用了 : 来扩展 ListIngredientQuery.item,但是当我从服务器获取我的列表时,如果我尝试将其作为成分列表获取,我得到type mismatch: required:List<Ingredient> found: <ListIngredientsQuery.item!>
错误。
如果我在 Java 中,我只会使用 "Extends" 关键字。
我获取列表的函数:
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)
我的 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
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
}
即使我不明白,使用给定的代码,为什么您要从 IngredientsQuery.Item
更改为 Ingredient
我认为您在这一行中缺少显式转换:
mIngredientsList = response.data()!!.listIngredients()!!.items()!!.toList()
像这样:
mIngredientsList = response.data()!!.listIngredients()!!.items()!!.toList() as List<Ingredient>
这应该可以解决您的问题。