AWS Amplify GraphQL Schema is causing an error - graphql

I am new to Amplify and I am trying to set up a fairly complex schema. When I run amplify push, the only response that I get is ...
An error occurred when pushing the resources to the cloud
This happens after the notice that it will create the resources and I am asked if I want to continue. I deleted most of the schema and tried again with just one model and it worked, so there is a problem somewhere in my schema ... I guess? I don't see anywhere to check it.
Here is the schema
type Article
#model
#key(name: "bySource", fields: ["sourceId", "dateWritten"]) {
id: ID!
link: AWSURL!
title: String!
dateWritten: String!
createdAt: String!
data: AWSJSON!
approved: Boolean!
admin: Boolean!
creatorId: ID!
creator: User #connection(fields: ["creatorId"])
sourceId: ID!
source: Source #connection(fields: ["sourceId"])
tagArtCons: [TagArtCon]
#connection(keyName: "byArticle", fields: ["articleId"])
}
type Tag #model #key(name: "byFrontPage", fields: ["frontpage"]) {
id: ID!
name: String!
createdAt: String!
creatorId: ID!
data: AWSJSON!
frontpage: String
official: Boolean!
tagArtConns: [TagArtCon] #connection(keyName: "byTag", fields: ["tagId"])
}
type TagArtCon
#model
#key(name: "byTag", fields: ["tagId"])
#key(name: "byArticle", fields: ["articleId"]) {
id: ID!
tagId: ID!
articleId: ID!
creatorId: ID!
createdAt: String!
article: Article #connection(fields: ["articleId"])
tag: Tag #connection(fields: ["tagId"])
parentRelations: [TagRelation]
#connection(keyName: "byParent", fields: ["tagId"])
childRelations: [TagRelation]
#connection(keyName: "byChild", fields: ["tagId"])
}
type TagRelation
#model
#key(name: "byParent", fields: ["parentId"])
#key(name: "byChild", fields: ["childId"]) {
id: ID!
parentId: ID!
childId: ID!
creatorId: ID!
createdAt: String!
parentTag: Tag #connection(fields: ["parentId"])
childTag: Tag #connection(fields: ["childId"])
}
type Source #model {
id: ID!
sourceName: String!
sourceUrl: String!
sourceImage: String!
creatorId: ID!
articles: [Article] #connection(keyName: "bySource", fields: ["id"])
}
type User #model {
id: ID!
userName: String!
userImage: String!
admin: Boolean!
createdAt: String!
data: AWSJSON
}
What I am trying to do is have a bunch of articles and a bunch of tags. The tags represent categories, people, etc. I have the following tables
articles,
tags,
a table where each entry ties an article to a tag
a table of relations between tags where parent/child relationships are held
a source table that just holds data for the sources of the articles
a user table
I've made a mistake somewhere and the schema isn't working and it isn't telling me why. I'm gonna keep simplifying this until I can hope to nail down the problem, but I would really appreciate any help as I am new to this. Thanks.

Finally found the problem(s). There is a place where I try to use "articleId" and a place where I try to use "tagId" and both should be "id".

Related

How to model Many to Many relationship for Amplify GraphQL Schema on Same Table

I am using GraphQl API in an Amplify project and having some difficulty understanding how to model a many to many relationship between users. I get how to add a join table between two other tables. But now sure how to do it for the same table.
This is what I have but I'm almost certain it's not correct. I want each user to be able to add other Users as 'relations':
type User
#model
#auth(rules: [{ allow: owner, operations: [create, delete, update] }]) {
id: ID!
cognitoId: ID!
username: String!
registered: Boolean
contacts: [UserContacts] #connection(keyName: "byContact", fields: ["id"])
contactOfOtherUsers: [UserContacts] #connection(keyName: "byContact2", fields: ["id"])
}
type UserContacts
#model
#auth(rules: [{ allow: owner, operations: [create, delete, update] }])
#key(name: "byContact", fields: ["userID", "contactID"])
#key(name: "byContact2", fields: ["contactID", "userID"]) {
id: ID!
userID: ID!
contactID: ID!
user: User! #connection(fields: ["userID"])
contact: User! #connection(fields: ["contactID"])
}
I'm pretty new to Amplify and not really sure what approach to take.
What seems very wrong to me is the contactOfOtherUsers field in User. It is redundant but not sure how else to link the join table.
Maybe too late but this might work. Disclaimer: This would apply to Amplify GraphQL Transformer v1, which still works but it is deprecated as it was replaced by GraphQL Transformer v2
type User
#model
#auth(rules: [{ allow: owner}]) {
id: ID!
cognitoId: ID!
username: String!
registered: Boolean
contacts: [UserContact] #connection(keyName: "byContact", fields: ["id"])
}
type UserContact
#model
#auth(
rules: [
{ allow: owner }
{
allow: owner
ownerField: "contacts"
operations: [update, delete, read]
}
]
) #key(name: "byContact", fields: ["contactOneID", "contactTwoID"]){
id: ID!
contactOneID: ID!
contactTwoID: ID!
contactOne: User! #connection(fields: ["contactOneID"])
contactTwo: User! #connection(fields: ["contactTwoID"])
contacts: [ID]
}

Add auth to many to many Amplify GraphQL DataStore connection

I am beginning a project with Amplify DataStore and modeling data that has a many-to-many relationship that also requires authorization. I'm having a hard time finding examples of M2M with auth, there are none in the documentation.
Here is a MTM example from the DataStore docs, I want to prevent users that are not PostEditors from being able to load Posts. Currently the plan is to use Cognito, but open to other solutions. How would this be achieved?
type Post #model {
id: ID!
title: String!
editors: [PostEditor] #connection(keyName: "byPost", fields: ["id"])
}
type PostEditor
#model(queries: null)
#key(name: "byPost", fields: ["postID", "editorID"])
#key(name: "byEditor", fields: ["editorID", "postID"]) {
id: ID!
postID: ID!
editorID: ID!
post: Post! #connection(fields: ["postID"])
editor: User! #connection(fields: ["editorID"])
}
type User #model {
id: ID!
username: String!
posts: [PostEditor] #connection(keyName: "byEditor", fields: ["id"])
}

Filter by Email in prisma GraphQL

I'm trying to filter a user by email, I am using prisma and doing tests with the "playground", is there any way to filter the user's data by email?
My schema:
interface Model {
id: ID! #id
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
}
type User implements Model {
id: ID! #id
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
username: String
name: String!
surname: String!
email: String!
token_usuario: String! #unique
}
My attempts:
Using prisma 2.0, the db client exposes something like this:
ctx.prisma.user.findMany({
where: {
email: { contains: "something" }
}
});
And I expect the query would follow suit. So try passing in the where field for filtering. Would need to see the full schema to say for sure.

Error: Invalid AST Node: {"input":"** } on graphql mutation (Amplify client)

I tried to use example schema on api doc("https://aws-amplify.github.io/docs/cli-toolchain/graphql?sdk=js") like below on Many-To-Many Connections
type Post #model {
id: ID!
title: String!
editors: [PostEditor] #connection(keyName: "byPost", fields: ["id"])
}
# Create a join model and disable queries as you don't need them
# and can query through Post.editors and User.posts
type PostEditor
#model(queries: null)
#key(name: "byPost", fields: ["postID", "editorID"])
#key(name: "byEditor", fields: ["editorID", "postID"]) {
id: ID!
postID: ID!
editorID: ID!
post: Post! #connection(fields: ["postID"])
editor: User! #connection(fields: ["editorID"])
}
type User #model {
id: ID!
username: String!
posts: [PostEditor] #connection(keyName: "byEditor", fields: ["id"])
}
I created all items and then I tried to delete them but I failed especially on PostEditor.
There is a mutation to delete PostEditor so I called it like below
API.graphql(graphqlOperation((deletePostEditor, {input: {id},})))
It fails with below error message.
Error: Invalid AST Node: {"input":"b2f7064c-af32-49cd-8c87-*******"}
I think I provided right ID. I checked it on query.
You should pass your parameters as a second parameter of graphqlOperation. so , check your parentheses
API.graphql(graphqlOperation((deletePostEditor, {input: {id},}))),you have one pair extra parenthesis
below is correct one
API.graphql(graphqlOperation(deletePostEditor, { input: { id } }))
first param=deletePostEditor
second param={ input: { id } }
tiny mistake, Isn't It?

A relation in graphcool makes fail the deploy for no reason

I created this relation in my graphcool
type User #model {
id: ID! #isUnique
location: Location! #relation(name: "UserLocation")
}
type Location #model {
id: ID! #isUnique
lat: Int
lng: Int
User: User! #relation(name: "UserLocation”)
}
Before location was a String, but now I wanted it to be an object so I created this relation, so that I can then use nested mutations. When I deploy I get this error:
There are issues with the new service definition:
Global
✖ None.get
I googled, looked on the documentation but I cannot understand what I am doing wrong, is a simple relation.
Faced issues like this when changing the type of fields as well. Not sure what the reason for this behavior (looks like it's a graphcool issue) but to solve them you can split it into two steps:
1) remove this relation from graphcool schema:
type User #model {
id: ID! #isUnique
# location: Location! #relation(name: "UserLocation")
}
type Location #model {
id: ID! #isUnique
lat: Int
lng: Int
# User: User! #relation(name: "UserLocation”)
}
2) revert relation fields with the new type:
type User #model {
id: ID! #isUnique
location: Location! #relation(name: "UserLocation")
}
type Location #model {
id: ID! #isUnique
lat: Int
lng: Int
User: User! #relation(name: "UserLocation”)
}

Resources