gpt4 book ai didi

GraphQL Resolver 用于按名称而不是 ID 获取项目

转载 作者:行者123 更新时间:2023-12-02 21:52:09 30 4
gpt4 key购买 nike

我有一个包含成分的 DynamoDB。 AWS Appsync 为我创建了一个解析器,以便我可以通过 ID 获取成分,但我需要能够通过名称获取成分。我尝试为此编写一个解析器,但它不起作用。

最终我需要编写一个解析器或 API,它接受字符串列表并返回与这些字符串匹配的成分(如果存在),但这是第一步,我希望如果我能做到这一点,我可以创建它的批处理版本。

解析器:

{
"version": "2017-02-28",
"operation": "GetItem",
"key": {
"name": $util.dynamodb.toDynamoDBJson($util.transform.toDynamoDBFilterExpression($ctx.args.filter))
}
}

响应映射模板

$util.toJson($ctx.result)

架构:

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
}

当我运行此查询时:

query getIt {
getIngredientByName(name: "demerara") {
id
name
vegan
gf
}
}

我收到回复:

{
"data": {
"getIngredientByName": null
},
"errors": [
{
"path": [
"getIngredientByName"
],
"data": null,
"errorType": "DynamoDB:AmazonDynamoDBException",
"errorInfo": null,
"locations": [
{
"line": 2,
"column": 3,
"sourceName": null
}
],
"message": "The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 58EKL6IO63VL44Q1DTG9JFNJB7VV4KQNSO5AEMVJF66Q9ASUAAJG)"
}
]
}

尽管 demerara 绝对是我数据库中的一个成分。

最佳答案

我想出了一个办法,尽管有点作弊。

当我在 AppSync 架构中创建资源时,有一个“附加索引”下拉列表,如果我按 ID 创建初始索引,然后创建第二个索引“名称”,它将创建一个带有解析器的查询你。就我而言

queryIngredientsByNameIndex(name: String!, first: Int, after: String): IngredientConnection

查询,解析器为

{
"version": "2017-02-28",
"operation": "Query",
"query": {
"expression": "#name = :name",
"expressionNames": {
"#name": "name",
},
"expressionValues": {
":name": $util.dynamodb.toDynamoDBJson($ctx.args.name),
},
},
"index": "name-index",
"limit": $util.defaultIfNull($ctx.args.first, 20),
"nextToken": $util.toJson($util.defaultIfNullOrEmpty($ctx.args.after, null)),
"scanIndexForward": true,
"select": "ALL_ATTRIBUTES",
}

关于GraphQL Resolver 用于按名称而不是 ID 获取项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55435951/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com