I have these two tables, the owner field in NFT table has a belongsTo relationship with the User, but the query is returning null even tho the User exists in the DB. What am I doing wrong here
type User
#model
#auth(
rules: [
{allow: public, provider: apiKey, operations: [read]}
{allow: public, provider: iam, operations: [read]}
{allow: private, operations: [read]}
{allow: owner}
]
) {
id: ID!
username: String
#index(
name: "usersByUsername",
queryField: "usersByUsername",
sortKeyFields: ["createdAt"]
)
email: String
#index(
name: "usersByEmail",
queryField: "usersByEmail",
sortKeyFields: ["createdAt"]
)
address: String
gender: String
interest: [String]
profileImage: String
firstName: String
lastName: String
OwnedNfts: [Nft]
#hasMany(
indexName: "nftsByOwnerId",
fields: "id"
)
createdAt: AWSDateTime!
updatedAt: AWSDateTime!
}
type Nft
#model
#auth(
rules: [
{allow: public, provider: apiKey, operations: [read]}
{allow: public, provider: iam, operations: [read, update]}
{allow: private, operations: [read, update]}
{allow: owner}
]
) {
id: ID!
private: Boolean
mediaUrl: String!
mediaUrlCompressed: String
thumbnail: String
category: String!
#index(
name: "nftsByCategoryCreatedAt",
queryField: "nftsByCategoryCreatedAt",
sortKeyFields: ["createdAt"]
)
ownerId: ID
#index(
name: "nftsByOwnerId",
queryField: "nftsByOwnerId"
sortKeyFields: ["createdAt"]
)
#index(
name: "nftsByOwnerIdRating",
queryField: "nftsByOwnerIdRating",
sortKeyFields: ["rating"]
)
#index(
name: "nftsByOwnerIdLikes",
queryField: "nftsByOwnerIdLikes",
sortKeyFields: ["likes"]
)
#index(
name: "nftsByOwnerIdReported",
queryField: "nftsByOwnerIdReported",
sortKeyFields: ["reportedCount"]
)
owner: User
#belongsTo(
fields: ["ownerId"]
)
createdAt: AWSDateTime!
updatedAt: AWSDateTime!
}
Related
I was working on an existing project using amplify with GraphQl API.
The query shows issues with #Key as Your GraphQL Schema is using "#key" directive from an older version of the GraphQL Transformer . Since I am new to graphQL, what should I change to make this work?
Thanks
Error:
✖ An error occurred when pushing the resources to the cloud
🛑 An error occurred during the push operation: Your GraphQL Schema is using "#key" directive from an older version of the GraphQL Transformer. Visit https://docs.amplify.aws/cli/migration/transformer-migration/ to learn how to migrate your GraphQL schema.
Query:
type KeyValuePair {
key: String!
value: String
}
type Template
#model
#auth(rules: [ {allow: owner } ]) {
id: ID!
name: String!
description: String
provision_style: String!
status: String!
params: [KeyValuePair]
resources: [KeyValuePair]
}
type Asset
#model
#auth(rules: [ {allow: owner } ])
#key(name: "categoryTypeIndex", fields: ["category", "type"], queryField: "assetsByCategoryAndType")
#key(name: "categoryNameIndex", fields: ["category", "name"], queryField: "assetsByCategoryAndName")
#key(name: "categoryStatusIndex", fields: ["category", "status"], queryField: "assetsByCategoryAndStatus") {
id: ID!
parent: ID
category: String!
type: String!
name: String!
description: String
status: String
template: String
details: [KeyValuePair]
}
type Contract
#model
#auth(rules: [ {allow: owner } ])
#key(name: "tenantAssetIndex", fields: ["tenantId", "assetId"], queryField: "contractsByTenantAndAsset")
#key(name: "tenantNameIndex", fields: ["tenantId", "name"], queryField: "contractsByTenantAndName")
#key(name: "tenantStatusIndex", fields: ["tenantId", "status"], queryField: "contractsByTenantAndStatus") {
id: ID!
type: String!
tenantId: String!
assetId: String!
name: String!
description: String
status: String
details: [KeyValuePair]
resources: [KeyValuePair]
}
type PlatformEvent
#model
#auth(rules: [ {allow: owner } ])
#key(name: "contractTypeIndex", fields: ["contractId", "type"], queryField: "eventsByContractAndType")
#key(name: "contractSourceIndex", fields: ["contractId", "sourceId"], queryField: "eventsByContractAndSource")
#key(name: "sourceTypeIndex", fields: ["sourceId", "type"], queryField: "eventsBySourceAndType") {
id: ID!
type: String!
contractId: String!
sourceId: String!
content: String!
details: [KeyValuePair]
}
AWS provides a migration guide: https://docs.amplify.aws/cli/migration/transformer-migration/ but unfortunately they don't specify how to actually set your API to use the new/old version.
If you look in your amplify folder you will find a cli.json file, to set your backend to use version 1 of the GraphQLTransformer you simply need to edit the file and set the following two properties:
"features": {
"graphqltransformer": {
...
"useexperimentalpipelinedtransformer": false,
"transformerversion": 1,
...
},
From there you simply amplify push and assuming you've catered for all of the changes the migration guide describes it will update your endpoint correctly.
EDIT: For those looking to upgrade to V2 instead of downgrading to V1 you will need to set the properties as follows:
"features": {
"graphqltransformer": {
...
"useexperimentalpipelinedtransformer": true,
"transformerversion": 2,
...
},
This should make your life easy.
Working with amplify datastore and edited schema.graphql as following and when I enter "amplify codegen models" I get this error "Error: Expected scalar and got User" However the code was generated successfully. How would I be able to fix this error?
type User
#model
#key(name: "ByEmail", fields: ["email"], queryField: "userByEmail")
#auth(rules: [{ allow: owner }]) {
id: ID!
email: String!
gender: String!
birthdate: String!
faceImage: String
favoriteAvatar: [Avatar] #connection(fields: ["avatarId"])
totalVideoTransform: Int
registerAt: AWSDateTime!
}
type Avatar
#model
#key(name: "ByAvatrId", fields: ["avatarId"], queryField: "avatarByAvatarId")
#auth(rules: [{ allow: owner, ownerField: "favoriteUser", operations: [create, update, read] }]) {
id: ID!
avatarId: String!
gender: String!
favoriteUser: [User] #connection(fields: ["email"])
totalVideoTransform: Int
}
type UserLoginLog
#model
#key(name: "ByUser", fields: ["user"], queryField: "userLoginLogByUser")
#auth(rules: [{ allow: owner, operations: [create] }]) {
id: ID!
user: User #connection(fields: ["email"])
loginAt: AWSDateTime!
}
type VideoTransformLog
#model
#key(name: "ByUser", fields: ["user"], queryField: "videoTransformLogByUser")
#auth(rules: [{ allow: owner, operations: [create] }]) {
id: ID!
user: User #connection(fields: ["email"])
avatar: Avatar #connection(fields: ["avatarId"])
videoTransformAt: AWSDateTime!
}
In your types "UserLoginLog" and "VideoTransformLog", remove "user" from the #key(... fields).
So don't place names that correspond to a type in fields[].
So I have 3 collections in my schema
type User #model(subscriptions: null) {
id: ID!
name: String
email: String
events: [EventUser] #connection(keyName: "byUser", fields: ["id"])
}
type Event #model
#key(name: "byApiKey", fields: ["apiKey"])
#key(name: "byOrganization", fields: ["organizationID"], queryField: "eventByOrganization")
{
id: ID!
apiKey: ID!
title: String
participants: [EventUser] #connection(keyName: "byEvent", fields: ["id"])
}
type EventUser #model
#key(name: "byUser", fields: ["userID"], queryField: "eventByUser")
#key(name: "byEvent", fields: ["eventID"], queryField: "userByEvent") {
id: ID!
userID: ID!
eventID: ID!
user: User! #connection(fields: ["userID"])
event: Event! #connection(fields: ["eventID"])
}
And I am trying to search the EventUser collection by eventID and user.name. Is there some piece of amplify that can enable me search by the user.name and return the EventUser collection?
I'm trying to learn GraphQL (& node.js & MongoDB etc.). I cant get this simple nested query to return results :
query getLocationByPerson {
People {
firstName
lastName
service {
location
}
}
}
I only get the first level fields, like:
{
"data": {
"People": [
{
"firstName": "xxx",
"lastName": "XXXXX",
"service": null
}
}
Here's my schema:
type Query {
People: [PeopleObject]!
PeopleByName(lastName: String!): [PeopleObject]
PeopleByID(id:ID!): [PeopleObject]
Service: [ServiceObject]
ServiceByID(id:ID!): [ServiceObject]
}
type PeopleObject {
id: ID!
Xid: String!
firstName: String!
lastName: String!
email: String!
serviceId: String
apps: [String]
service: [ServiceObject]
}
type ServiceObject {
id: ID!
name: String!
location: String!
}
And my resolver:
const queries = {
People: () => People.find({}),
PeopleByName: (root,args,context,info) => People.find({lastName: args.lastName}),
PeopleByID: (root,args,context,info) => People.find({_id: args.id}),
Service: () => Service.find({}),
ServiceByID: (root,args,context,info) => Service.find({_id: args.id})
};
Any idea what am I doing wrong?
You need to pass the reference for the location object in service which is same as you mongoose model(i assume you are using mongoose). After passing the reference you can add populate(refernce).exec() in your service query Refer this
Your syntax are dame good for getting any data from graphql
type Query {
People: [PeopleObject]!
}
type PeopleObject {
id: ID!
Xid: String!
firstName: String!
lastName: String!
email: String!
serviceId: String
apps: [String]
service: [ServiceObject]
}
type ServiceObject {
id: ID!
name: String!
location: String!
}
for Simplicity
type Query {
People: [{
id: ID!
Xid: String!
firstName: String!
lastName: String!
email: String!
serviceId: String
apps: [String]
service: [{
id: ID!
name: String!
location: String!
}]
}]!
}
But you have return only people
People: () => People.find({}),
You should return service array object to get that return object with People
People: () => return something Like below result.
[{
id: '5c5ac87e5e4d85ae77de9c50'
Xid: 'xx'
firstName: 'yyy'
lastName: 'zzz'
email: 'xxx#gmail.com'
serviceId: '5c5ac87e5e4d85ae77de9c51'
apps: ['testing', 'google']
service: [{
id: 5c5ac78d5e4d85ae77de9c4f,
name: 'Tester',
location: 'US America',
}, {}]
}, {}]
If you using mongoose use aggregation to get data from mongoBD.
I have the following GraphQL schema. It has Books and Authors with a many to many relationship. How do I create a new Book mutation for an existing Author?
schema
type Author implements Node {
books: [Book!]! #relation(name: "BookAuthors")
createdAt: DateTime!
id: ID! #isUnique
name: String!
updatedAt: DateTime!
}
type Book implements Node {
authors: [Author!]! #relation(name: "BookAuthors")
createdAt: DateTime!
id: ID! #isUnique
title: String!
updatedAt: DateTime!
}
mutation
mutation CreateBook($title: String!, $authors: [Author!]) {
createBook(
title: $title,
authors: $authors,
) {
id
title
}
}
variables
{
"title": "Ryans Book",
"authors": ["cj5xti3kk0v080160yhwrbdw1"]
}
This is apparently called a 'connect mutation'.
https://www.graph.cool/docs/reference/simple-api/nested-connect-mutations-tu9ohwa1ui/
mutation CreateBook($title: String!, $authorIds: [ID!]) {
createBook(
title: $title,
authorIds: $authorIds,
) {
id
title
}
}