Hello all. Having an issue, hopefully someone here...
# orm-help
j
Hello all. Having an issue, hopefully someone here can help. I have a prisma server with a mutation where I’m trying to pass an ID to make a connection. Here is what it looks like.. createNotification(title: String!, message: String!, seen: Boolean, type: NotificationType, userId: ID!): Notification! But I get an error saying “userId is not defined. Did you mean user?”
p
Can you paste your mutation schema?
j
async createNotification(parent, args, ctx, info) { const { userId } = ctx.request; if (!userId) { throw new Error(‘You must be logged in’); } const notification = await ctx.db.mutation.createNotification({ data: { user: { connect: { id: args.userId } }, ...args } }, info); return notification; },
@Priyank Patel
p
in your schema.graphql, do you have userId defined for createNotification mutation?
j
This is what the mutation looks like in my schema.graphql
createNotification(title: String!, message: String!, seen: Boolean, type: NotificationType, userId: ID!): Notification!
userId is there
p
oh I know what the problem is, ...args also passes in userId
j
I changed it to “personId” and still no luck
p
you should manually specify the field, OR structure the object and then pass it on
async createNotification(parent, args, ctx, info) { const { userId } = ctx.request; const { title, message, seen, type } = args; if (!userId) { throw new Error(‘You must be logged in’); } const notification = await ctx.db.mutation.createNotification({ data: { user: { connect: { id: args.userId } }, title, * message,* * seen,* * type* } }, info); return notification; },
Try this out
j
That works but I there are situations where I need to create a notification for a user that is not the one making the request. Although that would work
So I’m able to do this.. createNotification(title: String!, message: String!, belongsTo: UserCreateOneInput!): Notification! And then the mutation looks like this.. mutation { createNotification( title: “Test”, message: “This is a test notification”, belongsTo: { connect: { id: “******” } } ){ id } } But that’s annoying to pass that object to the request every time. I’d rather just pass the id
p
mutation { createNotification( title: “Test”, message: “This is a test notification”, userId: "USER_ID" ){ id } }
It should be something like this. I don't ever remember passing belongTo in the mutation request
j
Sorry I changed user in my datamodel to belongsTo.
Are you saying pass it as a string instead of an ID?
p
the id should be form of a string, within double quotation
j
Ok I’ll try that, thanks for the help
I do have another mutation that follows this same approach though where I pass ID and not a string. Odd that it works for that and not this. Feels like a bug with the generated prisma file.