Title
s

Sam Jackson

05/23/2018, 2:26 PM
I was trying to extend a Post model to include an optional attribute
parent
. 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?
n

nilan

05/23/2018, 2:44 PM
can you activate debug logging and share the raw request sent to Prisma?
the query will be logged to stdout
s

Sam Jackson

05/23/2018, 2:45 PM
Request 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
n

nilan

05/23/2018, 2:46 PM
aha, yea
const parent = parentId ? { connect: { id: parentId } } : {}
return context.db.mutation.createPost({
    data: {
      body: args.body,
      parent,
      author: {
        connect: {
          id: userId
        }
      }
    }
  }, info)
should work
s

Sam Jackson

05/23/2018, 2:47 PM
I was looking at using
@include
or
@skip
to fix this somehow. Does Prisma support those directives?
Ah, okay. That looks easier 🙂
n

nilan

05/23/2018, 2:48 PM
yes, they are standard GraphQL and are supported by Prisma.
s

Sam Jackson

05/23/2018, 2:51 PM
Okay. I got it working. Thanks (again!)
👍 1
@nilan Following up to this. I am able to create posts with parents now, but when I do, the parent post gets the child Post in its own
parent
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")
}
Seems like I might want to do an upsert.
n

nilan

05/23/2018, 3:47 PM
What do you expect to happen?
n

nilan

05/23/2018, 3:48 PM
the behaviour you describe is the expected behaviour
s

Sam Jackson

05/23/2018, 3:48 PM
My intended effect is like that example. I think I need to modify my resolver
n

nilan

05/23/2018, 3:48 PM
I don't understand 🙂
s

Sam Jackson

05/23/2018, 3:50 PM
Say I create post A with no parent. A's data should include
parent: 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: []
Right now, they are
parent: A, children: []
and
parent: B, children: []
n

nilan

05/23/2018, 3:51 PM
what is
parent
and
children
in your above
Post
type?
parent
and
replies
?
s

Sam Jackson

05/23/2018, 3:51 PM
Post
and
[Post!]!
respectively
n

nilan

05/23/2018, 3:51 PM
those are field types, not field names
I was asking about the field names 🙂
s

Sam Jackson

05/23/2018, 3:53 PM
Ah, then yes,
parent
and
replies
n

nilan

05/23/2018, 3:53 PM
you probably meant them to be two sides of one relation
s

Sam Jackson

05/23/2018, 3:53 PM
Correct. How do I define that?
n

nilan

05/23/2018, 3:53 PM
but they are actually two separate self-relations with one field
you need to choose the same relation name 🙂
parent: Post @relation(name: "PostParent")
  replies: [Post!]! @relation(name: "PostReplies")
becomes this
parent: Post @relation(name: "PostReplies")
  replies: [Post!]! @relation(name: "PostReplies")
for example
s

Sam Jackson

05/23/2018, 3:54 PM
Let me try it that way.
I am still getting this error even when the relations have the same name
{
  "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": []
      }
    ]
  }
}
n

nilan

05/23/2018, 3:59 PM
can you share your latest schema?
did you create these posts after you updated the schema?
s

Sam Jackson

05/23/2018, 4:00 PM
Yes. Schema:
type Post {
  id: ID! @unique
  body: String!
  author: User! @relation(name: "UserPosts")
  parent: Post @relation(name: "PostParent")
  replies: [Post!]! @relation(name: "PostParent")
}
Actually, you know what.. I don't think I did
prisma deploy
🙃
Now I get this:
{
  "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."
          }
        ]
      }
    ]
  }
}
n

nilan

05/23/2018, 4:12 PM
can you share a mutation directly against the Prisma API that results in this?
mutation {
  createPost(data: {
    body: "nilan"
    parent: {
      create: {
        body: "nilan2"
      }
    }
  }) {
    parent {
      id
      replies {
        id
      }
    }
  }
}
this mutation works as expected
s

Sam Jackson

05/23/2018, 4:21 PM
The queries I did: ``` ```
mutation {
  addPost(
    body:"This is a parent post."
  ) {
    id
    body
  }
}
mutation {
  addPost(
    body:"This is a child post."
    parentId:"cjhjas6s001560955z4u7b7xb"
  ) {
    id
    body
  }
}
Where the resolver for addPost is as we discussed above
n

nilan

05/23/2018, 4:23 PM
please provide the raw mutation against the Prisma API 🙂
s

Sam Jackson

05/23/2018, 4:24 PM
return context.db.mutation.createPost({
    data: {
      parent,
      body: args.body,
      author: {
        connect: {
          id: userId
        }
      }
    }
  }, info)
n

nilan

05/23/2018, 4:27 PM
well, I am not able to reproduce this behaviour in my service. If you manage to find a reproduction, please share it here: https://github.com/graphcool/prisma/
s

Sam Jackson

05/23/2018, 4:28 PM
You still got the expected result with that mutation?
n

nilan

05/23/2018, 4:28 PM
I tried the one I shared above and I don't see a difference to yours
s

Sam Jackson

05/23/2018, 4:29 PM
Hmm. I'll continue to play around with it. Thank you for your help though.
👍 1