Cannot return null for non-nullable field Tag.links - graphql

I have data model like below
type Tag {
id: ID!
name: String!
icon: String
description: String!
links: [Link!]!
}
type Link {
id: ID!
createdAt: DateTime!
description: String!
url: String!
tags: [Tag!]!
}
I want to query all the tags based on names and i am able to do that by the below query
tags (name:"tag1"){
id
name
}
this query works
but for the same query i want to return all the links associated with the tag name like below
tags(name:"tag1") {
id
name
links {
id
description
}
}
i am getting following error when executing this
"message": "Cannot return null for non-nullable field Tag.links.",
Resolver for this query
const tags = await context.prisma
.tags({name: args.name
})

Related

cannot delete a product from db using graphql

Schema:
type Mutation {
removeProduct(id: String!): Product
}
type Product {
id: ID!
name: String!
slug: String!
description: String!
price: Float!
image: String!
ingredients: [String]
addOns: [String]
}
resolver:
exports.Mutation = {
removeProduct: async (parent, { id }, { Product }) => {
const deletedProduct = await Product.deleteOne({ id });
return deletedProduct;
},
};
query:
mutation{
removeProduct(id:"b55572b9-eb4d-46ea-a82f-f01cb0ba3993")
{
id
name
}
}
I need to remove a product from the db, but while deleting it, the response from graphql shows that "message": "Cannot return null for non-nullable field Product.name.",
It maybe because the product is deleted and nothing is returned because there is no product in db.
How do I write a query to delete a product ?
Note that it deletes the product from the db, but shows error in graphql response.

GraphQL Prisma - use orderBy on nested relationships

I am fairly new to GraphQL Prisma combo, I cannot seem to order nested relationships without error because the only available orderBy fields are regarding the ID.
My intended goal in the playground would be to create a query that looks similar to the following:
getPosts{
connections(orderBy: "text_asc"){
id
text
}
}
Resolver
getPosts(parent, args, ctx, info){
return ctx.db.query.posts({}, info)
}
Data Model
type Post {
id: ID! #id
isPublished: Boolean! #default(value: false)
title: String!
text: String!
connections: [Connection!]! #relation(name: "ClientConnect")
}
type Link {
id: ID! #id
text: String!
url: String!
}
type Connection {
id: ID! #unique #id
client: Link #relation(name: "LinkConnect")
}

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?

How to resolve subselections / relations in prisma (nested lists)

Let's take an example from the github repo of prisma:
We have a user, the user could have multiple posts, and one post could have multiple links.
My goal is, to retrieve all posts and all links.
This means, my response is a list (links) in a list (posts).
I want to map the values I get back as two nested lists.
datamodel.prisma
type User {
id: ID! #id
email: String! #unique
name: String
posts: [Post]!
}
type Post {
id: ID! #id
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
published: Boolean! #default(value: false)
title: String!
content: String
author: User!
links: [Link]!
}
type Link {
id: ID! #id
url: String
title: String
post: Post!
}
schema.graphql
type Query {
...
}
type Mutation {
...
}
type Link {
id: ID!
url: String
title: String
post: Post!
}
type Post {
id: ID!
createdAt: DateTime!
updatedAt: DateTime!
published: Boolean!
title: String!
content: String
author: User!
}
type User {
id: ID!
email: String!
name: String
posts: [Post]!
}
I want to query all posts of a user, and all of the links for every post in the response.
How would I query this request?
user {
id
posts {
id
links {
id
}
}
}
The above code snipper would not work.
EDIT
I want to use the following:
User: {
listPosts: (parent, args, context, info) {
return context.prisma.posts().links()
}
}
So in my response (data in front-end via react-apollo Query Component), I want to map over posts AND the links in each post.
BUT the links attribute in posts is null.
Is there another way to achieve this?!
According to the docs:
Prisma client has a fluent API to query relations in your database. Meaning you can simply chain your method calls to navigate the relation properties of the returned records. This is only possible when retrieving single records, not for lists. Meaning you can not query relation fields of records that are returned in a list.
In order to get around that limitation, you can use the $fragment method:
const fragment = `
fragment UserWithPostsAndLinks on User {
id
email
name
posts {
id
title
content
links {
id
url
title
}
}
}
`
const userWithPostsAndLinks = await prisma.user({ id: args.id }).$fragment(fragment)

Field on `User` type returns `null` for Query even though data exists

I have a profilePicture field on my User type that is being returned as null even though I can see the data is there in the database. I have the following setup:
// datamodel.prisma
enum ContentType {
IMAGE
VIDEO
}
type Content #embedded {
type: ContentType! #default(value: IMAGE)
url: String
publicId: String
}
type User {
id: ID! #id
name: String
username: String! #unique
profilePicture: Content
website: String
bio: String
email: String! #unique
phoneNumber: Int
gender: Gender! #default(value: NOTSPECIFIED)
following: [User!]! #relation(name: "Following", link: INLINE)
followers: [User!]! #relation(name: "Followers", link: INLINE)
likes: [Like!]! #relation(name: "UserLikes")
comments: [Comment!]! #relation(name: "UserComments")
password: String!
resetToken: String
resetTokenExpiry: String
posts: [Post!]! #relation(name: "Posts")
verified: Boolean! #default(value: false)
permissions: [Permission!]! #default(value: USER)
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
}
// schema.graphql
type User {
id: ID!
name: String!
username: String!
profilePicture: Content
website: String
bio: String
email: String!
phoneNumber: Int
gender: Gender!
following: [User!]!
followers: [User!]!
verified: Boolean
posts: [Post!]!
likes: [Like!]!
comments: [Comment!]!
permissions: [Permission!]!
}
Like I said there is data in the database but when I run the below query in Playground I get null:
// query
{
user(id: "5c8e5fb424aa9a000767c6c0") {
profilePicture {
url
}
}
}
// response
{
"data": {
"user": {
"profilePicture": null
}
}
}
Any idea why?
ctx.prisma.user(({ id }), info); doesn’t return profilePicture even though the field exists in generated/prisma.graphql
Fixed it. I had to add a field resolver for profilePicture under User. I've done this before for related fields like posts and comments and I think It's because profilePicture points to the #embedded Content type so is sort of a related field as well.
{
User: {
posts: parent => prisma.user({ id: parent.id }).posts(),
following: parent => prisma.user({ id: parent.id }).following(),
followers: parent => prisma.user({ id: parent.id }).followers(),
likes: parent => prisma.user({ id: parent.id }).likes(),
comments: parent => prisma.user({ id: parent.id }).comments(),
profilePicture: parent => prisma.user({ id: parent.id }).profilePicture()
}
}

Resources