hey guys, I'm trying since hours to get the contex...
# orm-help
m
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
What is the issue?
m
Hey, I was able to resolve it. Thank you
I have another question tho:
Copy code
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:
Copy code
return context.prisma.createReview({
        url,
        title,
        author: {
          connect: {
            id: userId
          },
        }
      });
I got the error:
Copy code
"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
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:
Copy code
query {
     reviews {
          id
          url
          author {
               id
               name
          }
     }
}
m
ahh ok, good to know how connect works
Copy code
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
Can you post your mutation request?
m
Copy code
createReview: async (parent, { url, title }, context, info) => {
      const userId = await getUserId(context, info);
      return context.prisma.createReview({
        url,
        title,
        author: {
          connect: {
            id: userId
          }
        }
      });
    },
p
Ah, there is your problem.
m
aja
p
wait, nevermind. This is the resolver.
Can you try this?
m
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
Copy code
mutation {
     createReview(
          url: "<http://google.com>"
          title: "Test Title"
     ) {
          id
          url
          author {
               id
               name
          }
     }
}
Are you executing the mutation like the request above?
m
Copy code
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
Copy code
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
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
try changing the reviews: [Review!] to reviews: [Review!]!. As far I know, everything else seems to be correct.
m
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
Did you also also clear all the review id's from user?
m
users didn't yet have reviews
so I deleted all the users as well
;_)
let's see
p
I'm creating an example repo for you once I test it myself.
m
"Variable '$data' expected value of type 'UserCreateInput!'
why does this show up again...
I have a normal createUser function
Copy code
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
Your server is hosted locally, so I won't be able to access it via graphqlbin.
m
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
Copy code
Cannot return null for non-nullable field Review.author.
and yet my mutations show up in the database successfully
p
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
thanks for setting these u
p
wait
Copy code
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
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
ok that makes a bit more sense now