'Prisma: what does Create Nested One Without Input mean?

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.



Solution 1:[1]

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! :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rinogo