Sam Jackson
05/23/2018, 2:26 PMparent
. My resolver for creating a new post has become:
async function addPost (parent, args, context, info) {
const userId = getUserId(context)
const parentId = args.parentId ? args.parentId : null
return context.db.mutation.createPost({
data: {
body: args.body,
parent: {
connect: {
id: parentId
}
},
author: {
connect: {
id: userId
}
}
}
}, info)
}
However, I am getting an error when firing this mutation that reads: You provided an invalid argument for the where selector on Post.
I think this is because I am attempting to write a post without a parent. How can I handle cases where that value is not provided; should it just be an empty string?nilan
05/23/2018, 2:44 PMSam Jackson
05/23/2018, 2:45 PMRequest to <http://localhost:4466>:
query:
mutation ($_v0_data: PostCreateInput!) {
createPost(data: $_v0_data) {
id
author {
id
displayName
}
}
}
operationName: null
variables:
{
"_v0_data": {
"body": "Hello world!",
"parent": {
"connect": {
"id": null
}
},
"author": {
"connect": {
"id": "cjhj6kkhu012u09551cbhdb53"
}
}
}
}
[GraphQL error]: Message: You provided an invalid argument for the where selector on Post., Location: [object Object], Path: createPost
nilan
05/23/2018, 2:46 PMconst parent = parentId ? { connect: { id: parentId } } : {}
return context.db.mutation.createPost({
data: {
body: args.body,
parent,
author: {
connect: {
id: userId
}
}
}
}, info)
should workSam Jackson
05/23/2018, 2:47 PM@include
or @skip
to fix this somehow. Does Prisma support those directives?nilan
05/23/2018, 2:48 PMSam Jackson
05/23/2018, 2:51 PMparent
field. I have a separate field for replies
but it remains empty. Any thoughts?
type Post {
id: ID! @unique
body: String!
author: User! @relation(name: "UserPosts")
parent: Post @relation(name: "PostParent")
replies: [Post!]! @relation(name: "PostReplies")
}
nilan
05/23/2018, 3:47 PMSam Jackson
05/23/2018, 3:48 PMnilan
05/23/2018, 3:48 PMSam Jackson
05/23/2018, 3:48 PMnilan
05/23/2018, 3:48 PMSam Jackson
05/23/2018, 3:50 PMparent: null, children: []
. Then I create a child post B using A's ID. A's data should then be parent: null, children: [B]
, and B should be parent: A, children: []
parent: A, children: []
and parent: B, children: []
nilan
05/23/2018, 3:51 PMparent
and children
in your above Post
type?parent
and replies
?Sam Jackson
05/23/2018, 3:51 PMPost
and [Post!]!
respectivelynilan
05/23/2018, 3:51 PMSam Jackson
05/23/2018, 3:53 PMparent
and replies
nilan
05/23/2018, 3:53 PMSam Jackson
05/23/2018, 3:53 PMnilan
05/23/2018, 3:53 PMparent: Post @relation(name: "PostParent")
replies: [Post!]! @relation(name: "PostReplies")
becomes this
parent: Post @relation(name: "PostReplies")
replies: [Post!]! @relation(name: "PostReplies")
for exampleSam Jackson
05/23/2018, 3:54 PM{
"data": {
"getPosts": [
{
"id": "cjhjal46f014i0955eg6tc772",
"body": "This is a parent post.",
"parent": {
"id": "cjhjalft4014m0955kdlokis9",
"body": "This is a child post."
},
"replies": []
},
{
"id": "cjhjalft4014m0955kdlokis9",
"body": "This is a child post.",
"parent": {
"id": "cjhjal46f014i0955eg6tc772",
"body": "This is a parent post."
},
"replies": []
}
]
}
}
nilan
05/23/2018, 3:59 PMSam Jackson
05/23/2018, 4:00 PMtype Post {
id: ID! @unique
body: String!
author: User! @relation(name: "UserPosts")
parent: Post @relation(name: "PostParent")
replies: [Post!]! @relation(name: "PostParent")
}
prisma deploy
{
"data": {
"getPosts": [
{
"id": "cjhjas6s001560955z4u7b7xb",
"body": "This is a parent post.",
"parent": {
"id": "cjhjasdg6015a0955zps07hhr",
"body": "This is a child post."
},
"replies": [
{
"id": "cjhjasdg6015a0955zps07hhr",
"body": "This is a child post."
}
]
},
{
"id": "cjhjasdg6015a0955zps07hhr",
"body": "This is a child post.",
"parent": {
"id": "cjhjas6s001560955z4u7b7xb",
"body": "This is a parent post."
},
"replies": [
{
"id": "cjhjas6s001560955z4u7b7xb",
"body": "This is a parent post."
}
]
}
]
}
}
nilan
05/23/2018, 4:12 PMmutation {
createPost(data: {
body: "nilan"
parent: {
create: {
body: "nilan2"
}
}
}) {
parent {
id
replies {
id
}
}
}
}
Sam Jackson
05/23/2018, 4:21 PMmutation {
addPost(
body:"This is a parent post."
) {
id
body
}
}
mutation {
addPost(
body:"This is a child post."
parentId:"cjhjas6s001560955z4u7b7xb"
) {
id
body
}
}
nilan
05/23/2018, 4:23 PMSam Jackson
05/23/2018, 4:24 PMreturn context.db.mutation.createPost({
data: {
parent,
body: args.body,
author: {
connect: {
id: userId
}
}
}
}, info)
nilan
05/23/2018, 4:27 PMSam Jackson
05/23/2018, 4:28 PMnilan
05/23/2018, 4:28 PMSam Jackson
05/23/2018, 4:29 PM