用于按名称而不是 ID 获取项目的 GraphQL 解析器
GraphQL Resolver for getting item by name, not ID
我有一个带成分的 DynamoDB。 AWS Appsync 为我创建了一个解析器,因此我可以通过 ID 获取成分,但我需要能够通过名称获取成分。我试过为此编写一个解析器,但它不起作用。
最终我需要编写一个解析器或 API 获取字符串列表和 returns 匹配这些字符串的成分(如果它们存在),但这是第一步,我'我希望如果我能做到这一点,我可以创建它的批处理版本。
解析器:
{
"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 架构中创建资源时,"additional indexes" 有一个下拉列表,如果我按 ID 创建初始索引,然后是第二个索引 "name",它将创建一个为您查询解析器。在我的例子中
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",
}
我知道这已经过时了,但我最近遇到了一个类似的问题,我可以通过向架构 属性 添加一个 @key
指令来解决我想要搜索的问题:
type User @model @key(name: "getUserByCognitoId", fields: ["cognitoId"], queryField: "getUserByCognitoId") {
id: ID!
cognitoId: ID!
...
}
文档:https://docs.amplify.aws/cli/graphql-transformer/directives#key
通过这种方式,AppSync 会为您生成解析器并添加排序键,使查询 DynamoDB 更加高效
我有一个带成分的 DynamoDB。 AWS Appsync 为我创建了一个解析器,因此我可以通过 ID 获取成分,但我需要能够通过名称获取成分。我试过为此编写一个解析器,但它不起作用。
最终我需要编写一个解析器或 API 获取字符串列表和 returns 匹配这些字符串的成分(如果它们存在),但这是第一步,我'我希望如果我能做到这一点,我可以创建它的批处理版本。
解析器:
{
"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 架构中创建资源时,"additional indexes" 有一个下拉列表,如果我按 ID 创建初始索引,然后是第二个索引 "name",它将创建一个为您查询解析器。在我的例子中
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",
}
我知道这已经过时了,但我最近遇到了一个类似的问题,我可以通过向架构 属性 添加一个 @key
指令来解决我想要搜索的问题:
type User @model @key(name: "getUserByCognitoId", fields: ["cognitoId"], queryField: "getUserByCognitoId") {
id: ID!
cognitoId: ID!
...
}
文档:https://docs.amplify.aws/cli/graphql-transformer/directives#key
通过这种方式,AppSync 会为您生成解析器并添加排序键,使查询 DynamoDB 更加高效