我想写一个变异解析器,它可以创建多个items.It,将一个项目列表作为参数。我尝试使用map函数遍历项目集,并在每个项目上应用创建单个项目突变。但它不起作用。我的代码:
datamodel:
type Item {
id: ID!@id
title: String!
}
type Mutation {
createItem(title:String):Item // creates a single item
createMultiple(batch:[ItemCreateInput]):AggregateItem // not working
}批处理的输入是{title:"abc"},{title:"cbe"}
prisma.graphql:
type AggregateItem {
count: Int!
}
input ItemCreateInput {
id: ID
title: String!
}
createItem(data: ItemCreateInput!): Item!我试着这样做:
async createMultiple(parent,args,info,ctx){
multipleItems = args.batch;
multipleItems.map(item =>
const Item = ctx.db.mutation.createItem({ // got error TypeError: Cannot read property 'mutation' of undefined
data:{...item}
},info)}发布于 2019-08-05 04:08:15
正如@DavidW所说,您没有提供足够的信息,例如GraphQL server配置。
根据我的猜测,您的GraphQL server配置不正确,或者您没有正确使用它。
要使用ctx.db.mutation,您需要按如下方式进行配置
// Use graphql-yoga in the demo,
// if you use other GraphQLServer,
// you need to configure according to the official documentation.
import { prisma } from '../generated/prisma-client'
const { GraphQLServer } = require('graphql-yoga')
const server = new GraphQLServer({
typeDefs: './schema.graphql',
resolvers,
context: {
// The context injected here corresponds to the fourth parameter in the resolvers ctx
db: prisma,
},
})发布于 2020-05-22 01:53:33
你的方法实际上非常接近。我通过在每次数据库写入后使用连接来实现此功能,而不是简单地将每个突变分配给一个变量。
尝试如下所示(假设args.batch是一个字符串列表)
async createMultiple(parent,args,info,ctx){
var items = []
multipleItems = args.batch;
multipleItems.map(item =>
items = items.concat(ctx.db.mutation.createItem({
data:{
item: item
}
}))
)
return itemshttps://stackoverflow.com/questions/57348376
复制相似问题