[英]write field level resolver for a field in user type in graphql
I am trying to generate user id using uuid npm package for the id field in the user type.我正在尝试使用 uuid npm package 为用户类型中的 id 字段生成用户 ID。 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.在我的 createUser 突变中,我只要求用户输入名称和 email 的值,而不是 id,因为我希望它由 uuid package 生成,这是我相信我在 createUser 突变中正确完成的.
Here is what I have in my schema.graphql file这是我的 schema.graphql 文件中的内容
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 -这是我访问类型的 index.js 文件中的内容 -
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 -这是我执行 createUser 突变调用的方式 -
mutation {
createUser(name:"test test", email:"test@test.com") {
id
name
email
}
}
Error message -错误信息 -
Invalid `prisma.user.create()` invocation:
{
data: {
id: 'af8a177b-824f-466b-93a5-7f5a1968b3f2',
~~
name: 'test test',
email: 'test@test.com'
}
}
Unknown arg `id` in data.id for type userCreateInput. Available args:
type userCreateInput {
name: String
email: String
}
Prisma Model -棱镜 Model -
modelmodel user {
id Int @id @default(autoincrement())
name String
email String @unique
}
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. UUID 不是Int
,因此如果您想使用 UUID,则id Int @id @default(autoincrement())
的 Prisma 模式没有意义。 Same for your parseInt
call.您的parseInt
调用也是如此。 If you want to use a UUID, then you would need如果你想使用 UUID,那么你需要
model user {
id String @id
name String
email String @unique
}
or probably even better, delete your id: uuidv4(),
line and do或者甚至更好,删除你的id: uuidv4(),
line and do
model user {
id String @id @default(uuid())
name String
email String @unique
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.