5

I am trying to figure out how to use prisma with graphql (and apollo server express/typescript).

I am getting stuck because I can't find definitions of terms used in the prisma documentation.

Currently, I am trying to figure out how to make a form, to create a record in the db.

I have an input file with the fields I want the user to input. When I run the prisma generator, it has added fields to the prisma client which I can't make any sense of. An example of one of these fields is: UserCreateNestedOneWithoutRequestInput.

export type RequestCreateInput = { title: string createdAt?: Date | string updatedAt?: Date | string user: UserCreateNestedOneWithoutRequestInput }

What does Create Nested One Without Input mean and do and how can I avoid it in my workflow?

My resolver currently has:

// CREATE REQUEST @UseAuth() @Mutation(() => Request) async createRequest(@ContextUser() user:User, @Arg("data") data: CreateRequest): Promise { return await prisma.request.create({ where: { userId: user.id }, data }) }

When I try this, I get an error on the data argument. The error message says:

Type 'CreateRequest' is not assignable to type '(Without<RequestCreateInput, RequestUncheckedCreateInput> & RequestUncheckedCreateInput) | (Without<...> & RequestCreateInput)'.
Type 'CreateRequest' is not assignable to type 'Without<RequestUncheckedCreateInput, RequestCreateInput> & RequestCreateInput'. Property 'user' is missing in type 'CreateRequest' but required in type 'RequestCreateInput'.ts(2322) index.d.ts(19184, 5): 'user' is declared here. index.d.ts(8926, 5): The expected type comes from property 'data' which is declared here on type '{ select?: RequestSelect | null | undefined; include?: RequestInclude | null | undefined; data: (Without<...> & RequestUncheckedCreateInput) | (Without<...> & RequestCreateInput); }'

That is a long garbled message, but I think it means that I can't create a record without adding a user. It clearly doesn't like the way I have tried to add it in the fragment preceding data. I can't find an example in the prisma documentation of why prisma would add this field to the input record and can't figure out a way to satisfy prisma that a user has been identified in order to create the record.

Does anyone know where Create Nested One Without Input might be defined so that I can figure out what is forcing its inclusion in the input record? Does anyone know how to create a record when prisma has imposed this attribute?

I have seen this post and adding a user field to my input file:

@Field()
 user: User 

and tried to follow the syntax it recommends as follows:

 // CREATE REQUEST
 @UseAuth()
 @Mutation(() => Request)
 async createRequest(@ContextUser() user:User, @Arg("data") data: CreateRequest): Promise<Request> {
 return await prisma.Request.create({ connect: { userId: user.id }, data })
 
 }

When I try this, I get an error on the userId field that says:

Type 'any' is not assignable to type 'never'.

When I try to research what that means, there is a microsoft note explaining that some fields dont have type safety. I'm way above my head trying to relate that back to what's happening here.

When I try it as follows, I get another variation on the same error message. I'm just guessing for syntax that might address the requirement at this point.

 // CREATE REQUEST
 @UseAuth()
 @Mutation(() => Request)
 async createRequest(@ContextUser() ContextUser: User, @Arg("data") data: CreateRequest): Promise<Request> {
 return await prisma.request.create({ where: { user: User }, data })
 
 }
 
}

With this format, the error says:

Type 'typeof User' is not assignable to type 'never'

When I try to make a mutation in Apollo Studio Explorer, I get an error message that says:

user: {\n + create?: UserCreateWithoutRequestInput | UserUncheckedCreateWithoutRequestInput,\n +
connectOrCreate?: UserCreateOrConnectWithoutRequestInput,\n +
connect?: UserWhereUniqueInput\n + },\n

I have no idea what this garbage means - but I think the starting point is to figure out how the extra field got added to the prisma client by the generator. If I can figure out what needs to be done to satisfy that field then maybe all of these errors are down stream of that.

asked Feb 14, 2022 at 20:32
1
  • 1
    NB: Related reading on Generated type names Commented Apr 21, 2022 at 19:34

1 Answer 1

5

I'm not sure exactly how to answer your question, but I'll tell you what I was doing wrong, which I think may help you.

I think we may have been confusing part of the API on create() - when providing the data object to create(), I thought I could provide a User object like:

data: {
 user: user
}

When I do so, I get an error similar to what you've mentioned. I think that particular "API" is for actually creating a nested User. (See docs on Nested writes)

The appropriate way to do what I was (and presumably you were) trying to do is:

data: {
 userId: user.id
}

Hope this helps! :)

answered Apr 21, 2022 at 19:40
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I gave up on trying to learn prisma. I have moved on without ever getting a grasp of its expectations. Thank you for sharing what worked for you. I'm not going back to try to learn it at this point.
I'm getting really tired of trying to figure out a simple create() as well. TRPC seems to throw another wrench into the mix and making it an ever bigger pain in the ass.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.