AWS AppSync GraphQL one to many relation connection - amazon-web-services

I am using AWS Appsync with dynamoDB and trying to create a connection between two schema Course and Badges in which once course can have multiple badges list. While badges list are static (don't change it frequently). I tried this but it doesn't worked.
type Badge #model #auth(rules: [{allow: private}]) {
id: ID!
Name: String
}
type Course #model #auth(rules: [{allow: private}]) {
id: ID!
badges: [Badge] #connection
}

You can find the solution here as for the documentation.
Please make sure that you don't read the legacy docs, as #connection directive is deprecated.
Hope it helps!

Related

Can I implement fields as a set (non-duplicated list) in AWS Amplify Datastore?

I'm a beginner at AWS Amplify and GraphQL.
I came here because I couldn't find it no matter how hard I looked.
My GraphQL code is as follows:
type User #model #auth(rules: [{ allow: owner }]) {
id: ID!
assets: [Asset]! #hasMany
}
I don't want 'Asset' model to be duplicated.
So I thought to make 'assets' as a set (non-duplicated list).
But is that possible to implement?

AWS Amplify AppSync GraphQL #Auth directive: How can I forbid to list of elements for public user

I am creating an React App with Amplify backend. So far that is working great but I want to forbid that certain user can list some Elements. Let me give an example graphql definition:
type Customer #model
#auth(rules: [
{allow: public, , operations: [read,create,update]},
{allow: groups, groups: ["admin","partner"] }])
{
id: ID!
firstName: String
lastName: String
email: String
phone: String
}
We have Customers that are not logged in so they are public. They know their id (because it is in the url after they created the Customer Entry) and they should be able to update, read and create their own user. Thats working good.
Unfortunately they can also use the listCustomers query. So they can see all the other entries. Can I forbid this in some way? As I understand the operation "read" means "get" and "list".
The same should be for cognito groups. "admin" should be able to do everything including "list" and "partner" should only be able to "get".
Does anyone has an idea? I have read the docs and googled it but seems like I do not find an answer.
Best regards
To answer my question:
You can add { allow: public, queries: [get, list], mutations: [create,update]} for authorization rules. Unfortunately this is deprecated. I have no idea if there will be sth similar in the futur.
See: https://docs.amplify.aws/cli/graphql-transformer/auth/#definition
type Customer #model #auth(rules: [
{ allow: public, queries: [get], mutations: [create,update]},
{ allow: groups, groups: ["admin"] },
{ allow: groups, groups: ["partner"], queries: [get], mutations: []}])
{
id: ID!
firstName: String
lastName: String
email: String
phone: String
request: [Request] #connection(keyName: "byCustomer", fields: ["id"])
}

aws mock api able to query but unable to delete entries

shown below is the graphql Schema.
#auth(rules: [{ allow: owner,operations: [create, delete ] ,ownerField: "user"}])
{
id: ID!
videoKey: String!
videoThumbnailKey :String!
videoTitle:String!
videoDescription:String!
channelName:String!
videoLikes: Int
videoDislikes: Int
comments: [Comment] #connection(keyName: "byVideo", fields: ["id"])
user: String
}
type Comment #model
#auth(rules: [{ allow: owner,operations: [create,delete] ,ownerField: "user"}])
#key(name: "byVideo", fields: ["videoID", "comment"]) {
id: ID!
videoID: ID!
comment: String!
video: Video #connection(fields: ["videoID"])
user: String
}
type LikedVideos #model
{
id:ID!
video:[Video]#connection
}
this is the query for listing videos
and these are the results
this is the query for getting a video
and these are the results
all of these works fine, but when I try to delete it I get error.
this is the mutation query
and this is the error response i got
Seems like you have not added sort key in dynamo db table and the other items have the same primary key as of first item. I faced similar issue and worked after adding sort key.
This seems to be another bug in AWS Amplify or local DynamoDB.
I am using the same schema - when I add even just a space to schema.grapql, first delete operation works. The delete operations executed after are not working.
Also, it seems like something is throttling query response (I am using amplify mock command):
E.g. I have 7 items in the database and when I am trying to list all of them, I got responses with 1, 2, or even zero items, multiple times - when I would expect, that they will be loaded in 1 request, at the same time.
I am using AWS Amplify
"aws-amplify": "^3.1.1",
"aws-amplify-react-native": "^4.2.5",
Even if you modify schema.graphql, it's still unreliable.

In an AppSync GraphQL transform, can I set an #auth rule AuthStrategy based on a field in the table?

Is there a way to conditionally set more than one AuthStrategy depending on a field in the table?
Let's say I have a blog Post type:
type Post
#model
#auth(rules: [
{ allow: owner, operations: [read, update, delete] },
])
{
id: ID!
owner: String!
visibility: Visibility!
title: String!
body: String!
whitelist: [String!]
}
enum Visibility {
PUBLIC,
PRIVATE,
PROTECTED,
SECRET
}
I want the creator to be able to set whether the Post is:
Public: anyone can see it, logged in or not.
Private: any logged-in user can see it.
Protected: this array of users can see it.
Secret: only the creator can see it.
I know how to hardcode each of these. But programmatically...
Is this even possible, or are AppSync transforms just too basic, and I need to use a custom resolver?
It turns out, this does require a custom resolver—or custom, nested resolver—which is pretty easy.
The official docs on this authorization scenario: https://docs.aws.amazon.com/appsync/latest/devguide/security-authorization-use-cases.html.
And a great Hacker Noon article on this authorization scenario: https://hackernoon.com/graphql-authorization-with-multiple-data-sources-using-aws-appsync-dfae2e350bf2.

AWS Amplify removes "id" field after uploading GraphQL Schema

I am using AWS Amplify to configure Appsync in my project. In my schema.graphql, I have the following types:
type Post #model {
id: ID!
tweet: Tweet
}
type Tweet {
id: ID
text: String!
}
Tweet is a nested object under Post, it is intentionally not a #model.
When I upload to AWS via the cli (amplify push), it removes the Tweet's id field from TweetInput on the generated schema. This is the generated schema from the AWS console:
input CreatePostInput {
id: ID
tweet: TweetInput
}
type Tweet {
id: ID
text: String!
}
input TweetInput {
text: String!
}
I have searched the Appsync documentation, but I cannot find anywhere that says I can't use an id field on an object type.
Is there any way around this? I'd like to avoid renaming the field if I can.
Turns out that this is a bug in the CLI https://github.com/aws-amplify/amplify-cli/issues/1984
Hopefully it gets resolved soon.