How to solve this GraphQL mutation error with Prisma ORM & NestJS

I am creating a graphql api using nestjs, prisma. When using the graphql playground to create a user I am getting an error as you can seen in the screenshot below. A row is created only with the id other fields of the user model are not transfered. Please have a look and any guidance would be appreciated. It's been a week I am trying to fix this to no avail.
No description
No description
No description
No description
No description
No description
No description
No description
No description
No description
11 Replies
theblessed
theblessedOP2y ago
user fields are returned null even though they are passed as variables for the create-input dto
No description
RaphaelEtim
RaphaelEtim2y ago
Hi @theblessed Can you please send the relevant part of the schema and query as text instead of images?
theblessed
theblessedOP2y ago
sure
RaphaelEtim
RaphaelEtim2y ago
Don't forget to add formatting to the code blocks for legibility.
theblessed
theblessedOP2y ago
# ------------------------------------------------------
# THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
# ------------------------------------------------------

input CreateUserInput {
email: String
firstName: String
password: String
role: Role
username: String
}

"""
A date-time string at UTC, such as 2019-12-03T09:54:33Z, compliant with the date-time format.
"""
scalar DateTime

type Mutation {
createUser(createUserInput: CreateUserInput!): User!
removeUser(id: String!): User!
updateUser(id: String!, updateUserInput: UpdateUserInput!): User!
}

type Query {
user(id: String!): User!
users: [User!]!
}

enum Role {
ADMIN
MODERATOR
USER
}

input UpdateUserInput {
email: String
firstName: String
password: String
role: Role
username: String
}

type User {
createdAt: DateTime!
email: String
firstName: String
id: String!
password: String
role: Float!
updatedAt: DateTime!
username: String
}
# ------------------------------------------------------
# THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
# ------------------------------------------------------

input CreateUserInput {
email: String
firstName: String
password: String
role: Role
username: String
}

"""
A date-time string at UTC, such as 2019-12-03T09:54:33Z, compliant with the date-time format.
"""
scalar DateTime

type Mutation {
createUser(createUserInput: CreateUserInput!): User!
removeUser(id: String!): User!
updateUser(id: String!, updateUserInput: UpdateUserInput!): User!
}

type Query {
user(id: String!): User!
users: [User!]!
}

enum Role {
ADMIN
MODERATOR
USER
}

input UpdateUserInput {
email: String
firstName: String
password: String
role: Role
username: String
}

type User {
createdAt: DateTime!
email: String
firstName: String
id: String!
password: String
role: Float!
updatedAt: DateTime!
username: String
}
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "mysql"
url = env("DATABASE_URL")

}


enum Role {
ADMIN
MODERATOR
USER

}
model User {
id String @id @default(cuid())
username String?
firstName String?
email String? @unique
password String?
role Role @default(USER)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

model Listing {
id String @id @default(cuid())
listingName String
listingDesc String
}
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "mysql"
url = env("DATABASE_URL")

}


enum Role {
ADMIN
MODERATOR
USER

}
model User {
id String @id @default(cuid())
username String?
firstName String?
email String? @unique
password String?
role Role @default(USER)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

model Listing {
id String @id @default(cuid())
listingName String
listingDesc String
}
import { Injectable } from '@nestjs/common';

import { PrismaService } from 'src/prisma/prisma.service';
import { Prisma } from '@prisma/client';

@Injectable()
export class UsersService {
constructor(private readonly prisma: PrismaService) {}

async create(createUserInput: Prisma.UserCreateInput) {
return this.prisma.user.create({
data: createUserInput,
});
}

async findAll() {
return this.prisma.user.findMany();
}

async findOne(id: string) {
return this.prisma.user.findUnique({
where: {
id,
},
});
}
async findByEmail(email: string) {
return this.prisma.user.findUnique({
where: {
email,
},
});
}

async update(id: string, updateUserInput: Prisma.UserUpdateInput) {
return this.prisma.user.update({
where: {
id,
},
data: updateUserInput,
});
}

async remove(id: string) {
return this.prisma.user.delete({
where: {
id,
},
});
}
}
import { Injectable } from '@nestjs/common';

import { PrismaService } from 'src/prisma/prisma.service';
import { Prisma } from '@prisma/client';

@Injectable()
export class UsersService {
constructor(private readonly prisma: PrismaService) {}

async create(createUserInput: Prisma.UserCreateInput) {
return this.prisma.user.create({
data: createUserInput,
});
}

async findAll() {
return this.prisma.user.findMany();
}

async findOne(id: string) {
return this.prisma.user.findUnique({
where: {
id,
},
});
}
async findByEmail(email: string) {
return this.prisma.user.findUnique({
where: {
email,
},
});
}

async update(id: string, updateUserInput: Prisma.UserUpdateInput) {
return this.prisma.user.update({
where: {
id,
},
data: updateUserInput,
});
}

async remove(id: string) {
return this.prisma.user.delete({
where: {
id,
},
});
}
}
you mean the mutation query in the playground as well?
RaphaelEtim
RaphaelEtim2y ago
This is fine for now. i'm taking a look Can you double-check the mapping of the incoming createUserInput to the Prisma UserCreateInput in your NestJS UsersService. Ensure that all fields from the CreateUserInput are correctly mapped to their corresponding fields in the UserCreateInput. Also you may want to share the repository link so i can run it locally and help debug this
theblessed
theblessedOP2y ago
it's the id,createdAt and updatedAt fields that are not included in the createUserInput class as they're generated by prisma automatically
theblessed
theblessedOP2y ago
here is the repo, thanks for checking for me, I really appreciate. https://github.com/dreamsachiever90/afhv_backend
GitHub
GitHub - dreamsachiever90/afhv_backend
Contribute to dreamsachiever90/afhv_backend development by creating an account on GitHub.
theblessed
theblessedOP2y ago
@RaphaelEtim Hi hope you're fine. Please, did you get some time to take a look at the repo?
RaphaelEtim
RaphaelEtim2y ago
Hi @theblessed, i took a look at it today. I can see that your data is not propagating to the Prisma service
async create(createUserInput: Prisma.UserCreateInput) {
console.log('>>> ', createUserInput);

return this.prisma.user.create({
data: createUserInput,
});
}
async create(createUserInput: Prisma.UserCreateInput) {
console.log('>>> ', createUserInput);

return this.prisma.user.create({
data: createUserInput,
});
}
The log of createUserInput is an empty object {}
theblessed
theblessedOP2y ago
Hi thanks for looking. But how can i fix this issue ? Is there something I did wrong? I followed nest js docs with prisma and everything looks fine to me. Any guidance will be appreciated.

Did you find this page helpful?