Title
m

markus_str

12/10/2018, 3:30 PM
hey guys, I'm trying since hours to get the context.prisma.user({id}) from the examples working in another project. Is this the right channel to ask for help?
p

Priyank Patel

12/10/2018, 4:29 PM
What is the issue?
m

markus_str

12/10/2018, 4:32 PM
Hey, I was able to resolve it. Thank you
I have another question tho:
type User {
  id: ID! @unique
  fibauid: String! @unique
  name: String! @unique
  email: String! @unique
  reviews: [Review!]

}

type Review {
  id: ID! @unique
  url: String! #check for URL type in frontend?
  author: User!
}
When I use the resolver:
return context.prisma.createReview({
        url,
        title,
        author: {
          connect: {
            id: userId
          },
        }
      });
I got the error:
"Field \"author\" of type \"User!\" must have a selection of subfields. Did you mean \"author { ... }\"?"
i couldn't figure out the syntax to make it work. Also is this 'connect' keyword going to push the Review to the user ?
review has a title etc...I just didn't paste everything to stay simple
p

Priyank Patel

12/10/2018, 5:27 PM
What does your request query look like? As for the connect question, yes. User will have list of review ids and the author field will have the user id it's associated to.
If you are trying to query the list of reviews, try:
query {
     reviews {
          id
          url
          author {
               id
               name
          }
     }
}
m

markus_str

12/10/2018, 6:47 PM
ahh ok, good to know how connect works
context.prisma.createReview({
        url,
        title,
        author: {
          connect: {
            id: userId
          },
        }
      });
this is the mutation
given my shema from above it should work, but it doesn't
dunno where to put the subfields there
p

Priyank Patel

12/10/2018, 6:51 PM
Can you post your mutation request?
m

markus_str

12/10/2018, 6:52 PM
createReview: async (parent, { url, title }, context, info) => {
      const userId = await getUserId(context, info);
      return context.prisma.createReview({
        url,
        title,
        author: {
          connect: {
            id: userId
          }
        }
      });
    },
p

Priyank Patel

12/10/2018, 6:52 PM
Ah, there is your problem.
m

markus_str

12/10/2018, 6:52 PM
aja
p

Priyank Patel

12/10/2018, 6:53 PM
wait, nevermind. This is the resolver.
Can you try this?
m

markus_str

12/10/2018, 6:54 PM
it's weird because if I remove the relation to reviews in my data schema it works
like if User doesn't have a review field
but just Review has an author field...but then it's kinda pointless
p

Priyank Patel

12/10/2018, 6:55 PM
mutation {
     createReview(
          url: "<http://google.com>"
          title: "Test Title"
     ) {
          id
          url
          author {
               id
               name
          }
     }
}
Are you executing the mutation like the request above?
m

markus_str

12/10/2018, 6:57 PM
const CREATE_REVIEW_MUTATION = gql`
  mutation createReview($title: String!, $url: String!) {
    createReview(title: $title, url: $url) {
      id
      title
      url
      createdAt
      author
    }
  }
`;
this is my apollo mutation
I then execute it through the UI
what you posted doesn't do the connect right?
p

Priyank Patel

12/10/2018, 6:57 PM
const CREATE_REVIEW_MUTATION = gql`
  mutation createReview($title: String!, $url: String!) {
    createReview(title: $title, url: $url) {
      id
      title
      url
      createdAt
      author {
        id
      }
    }
  }
`;
try that, and note the author field
you cannot just call author, you need to specify the fields within the author
Your resolver is correct, it's the apollo mutation that needed change to the author
m

markus_str

12/10/2018, 6:59 PM
mhh ahh ok let me try
You are creating a required field but there are already nodes present that would violate that constraint.
wanted to add reviews field to User.
I guess that means I have to delete manually all the review or user nodes idk
p

Priyank Patel

12/10/2018, 7:12 PM
try changing the reviews: [Review!] to reviews: [Review!]!. As far I know, everything else seems to be correct.
m

markus_str

12/10/2018, 7:17 PM
I deleted all the reviews and it still says there's nodes that violate that....
reviews:[Review!]! won't work, because a new User doesn't yet have reviews
p

Priyank Patel

12/10/2018, 7:18 PM
Did you also also clear all the review id's from user?
m

markus_str

12/10/2018, 7:18 PM
users didn't yet have reviews
so I deleted all the users as well
;_)
let's see
p

Priyank Patel

12/10/2018, 7:21 PM
I'm creating an example repo for you once I test it myself.
m

markus_str

12/10/2018, 7:22 PM
"Variable '$data' expected value of type 'UserCreateInput!'
why does this show up again...
I have a normal createUser function
createUser(name: String!, fibauid: String!, email: String!): User
now it's buggy again. I don't get why it's always asking for UserCreateInput
I put in a total of 25hours with prisma and graphql and I can't even get it working idk ... :=(
p

Priyank Patel

12/10/2018, 7:29 PM
Your server is hosted locally, so I won't be able to access it via graphqlbin.
m

markus_str

12/10/2018, 7:30 PM
yeah thought so
idk why on localhost I can't do a simple resolver
found one issue
thanks for helping me so far
somehow learning mongo + node was easier ;(
strangle it says
Cannot return null for non-nullable field Review.author.
and yet my mutations show up in the database successfully
p

Priyank Patel

12/10/2018, 7:54 PM
https://github.com/PR1YANKPAT3L/prisma-demo here you go, sorry took me some time as I am at work.
You will have to change the ids to appropriate one.
m

markus_str

12/10/2018, 7:59 PM
thanks for setting these u
p
wait
Review: {
    author: ({ id }, args, context) => {
      return context.prisma.review({ id }).author()
    },
  },
  User: {
    reviews: ({ id }, args, context) => {
      return context.prisma.user({ id }).reviews()
    },
  },
what is this? I didn't have those inside the resolvers...
didn't show up in the tutorials or?
I added this two resolvers and it works
damn
what the hell. My whole day I'm looking somewhere else and I didn't get this concept
😂 1
datamodel => schema => resolver types ...I mean it's a bit convoluted don't you think ?:)
anyways thank you soo much Priyank
gonna collapse into my bed now
p

Priyank Patel

12/10/2018, 8:10 PM
No worries 😀, feel free to message me if you have any issues.
There is a reason why the datamodel and schema are seperated, With datamodel, it creates a basic CRUD service. With schema, it allows you to build on top of the CRUD service with custom query and mutations (creating auth for example).
m

markus_str

12/11/2018, 8:56 AM
ok that makes a bit more sense now