0
votes

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
}
1
And is this not working? You haven't actually asked a question so far. If it is broken, what is the error? - loganfsmyth
Sorry, I forgot to put in the error, I have added the error to the original post. - Sumchans
How are you defining your prisma models? That is a Prisma error, not a GraphQL error. - loganfsmyth
I haven't made any change to the prisma model, If I remove the uuid from the id and ask the user to input the id with the mutation it works. I am attaching the prisma model to the original post - Sumchans

1 Answers

2
votes

A UUID is not an Int, so your Prisma schema of id Int @id @default(autoincrement()) does not make sense if you want to use a UUID. Same for your parseInt call. If you want to use a UUID, then you would need

model user {
  id String @id
  name String
  email String @unique
}

or probably even better, delete your id: uuidv4(), line and do

model user {
  id String @id @default(uuid())
  name String
  email String @unique
}