'Model relationship returning NULL
I,m crafting out a modal where i can add/fetch data of post/posts in aws datastore. But for some reason i cant fetch the user data while fetching Post (returns null) even in appsync although i can fetch the remaining data (store, storeItems, & comments).
Here is my schema
type User @model @auth(rules: [{allow: private, operations: [create, read]}, {allow: owner}]) {
id: ID!
username: String
avatar: String
followers: String
following: String
Posts: [Post] @hasMany(indexName: "byUser", fields: ["id"])
}
type StoreItems @model @auth(rules: [{allow: private, operations: [create, read]}, {allow: owner}]) {
id: ID!
name: String
quantity: Int
price: Int
storeID: ID! @index(name: "byStore")
Store: Store @belongsTo
photoArray: [String]
}
type Comment @model @auth(rules: [{allow: private, operations: [create, read]}, {allow: owner}]) {
id: ID!
comment: String
likes: Int
dislikes: Int
replies: Int
postID: ID! @index(name: "byPost")
Post: Post @belongsTo
}
type Store @model @auth(rules: [{allow: private, operations: [create, read]}, {allow: owner}]) {
id: ID!
StoreItems: [StoreItems] @hasMany(indexName: "byStore", fields: ["id"])
Post: Post @belongsTo
storeName: String
}
type Post @model @auth(rules: [{allow: private, operations: [create, read]}, {allow: owner}]) {
id: ID!
title: String
thumbnail: String
media: [String]
caption: String
price: Int
duration: Float
views: Int
likes: Int
dislikes: Int
Comments: [Comment] @hasMany(indexName: "byPost", fields: ["id"])
Store: Store @hasOne
userID: ID! @index(name: "byUser")
User: User @belongsTo
}
my Post User resolver Request Mapping Template
#if( $ctx.source.deniedField )
#return($util.toJson(null))
#end
#if( $util.isNull($ctx.source.userPostsId) )
#return
#else
#set( $GetRequest = {
"version": "2018-05-29",
"operation": "Query"
} )
$util.qr($GetRequest.put("query", {
"expression": "#partitionKey = :partitionValue",
"expressionNames": {
"#partitionKey": "id"
},
"expressionValues": {
":partitionValue": $util.parseJson($util.dynamodb.toDynamoDBJson($util.defaultIfNullOrBlank($ctx.source.userPostsId, "___xamznone____")))
}
}))
#if( !$util.isNullOrEmpty($ctx.stash.authFilter) )
$util.qr($GetRequest.put("filter", $util.parseJson($util.transform.toDynamoDBFilterExpression($ctx.stash.authFilter))))
#end
$util.toJson($GetRequest)
#end
my Post User resolver response mapping template.
#if( $ctx.error )
$util.error($ctx.error.message, $ctx.error.type)
#else
#if( !$ctx.result.items.isEmpty() && $ctx.result.scannedCount == 1 )
$util.toJson($ctx.result.items[0])
#else
#if( $ctx.result.items.isEmpty() && $ctx.result.scannedCount == 1 )
$util.unauthorized()
#end
$util.toJson(null)
#end
#end
What could be the problem?
amazon-web-services">
amazon-web-servicesaws-appsyncgraphql-schemaaws-appsync-iosaws-appsync-resolver
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
