I am trying to generate user id using uuid npm package for the id field in the user type. I am not sure where and how should I write it.
In my createUser mutation I am only asking the user to put in values for the name and email but not the id, as I wanted it to be generated by the uuid package, which is what I believe I have done it correctly in the createUser mutation.
Here is what I have in my schema.graphql file
type Query {
users: [User!]!
user(id: ID!): User!
}
type Mutation {
createUser(name: String!, email: String!): User!
}
type User {
id: ID!
name: String!
email: String!
}
and here is what I have in my index.js file where I am accessing the types -
const { ApolloServer } = require('apollo-server')
const fs = require('fs');
const path = require('path');
const { PrismaClient } = require('.prisma/client')
const { v4: uuidv4 } = require('uuid')
const prisma = new PrismaClient()
const resolvers = {
Query: {
//users: () => users
users: async (parent, args, context, info) => {
args.test = 'TestContext'
return await context.prisma.user.findMany()
},
user: async (parent, args, context, info) => {
return await context.prisma.user.findUnique({
where: {
id: parseInt(args.id)
},
})
}
},
Mutation: {
createUser: async (parent, args, context, info) => {
return await context.prisma.user.create({
data: {
id: uuidv4(),
name: args.name,
email: args.email
}
})
}
}
}
const server = new ApolloServer({
typeDefs: fs.readFileSync(//referencing a file that has the schema definition
path.join(__dirname, 'schema.graphql'),
'utf8'
),
//typeDefs: typeDefs,//Testing
resolvers,
context: {
prisma,
}
})
server.listen().then(({ url }) =>
console.log(`Server is running on ${url}`)
)
Here is how I am doing the createUser mutation call -
mutation {
createUser(name:"test test", email:"[email protected]") {
id
name
email
}
}
Error message -
Invalid `prisma.user.create()` invocation:
{
data: {
id: 'af8a177b-824f-466b-93a5-7f5a1968b3f2',
~~
name: 'test test',
email: '[email protected]'
}
}
Unknown arg `id` in data.id for type userCreateInput. Available args:
type userCreateInput {
name: String
email: String
}
Prisma Model -
modelmodel user {
id Int @id @default(autoincrement())
name String
email String @unique
}