Graphql Prisma2:如何用Paljs解决n+1问题
谢谢你的帮助 我在前端使用apollo客户端,在后端使用prisma2和graphql服务器 我想用@paljs/plugins解决n+1问题 在前端,我有一个查询帖子,如:Graphql Prisma2:如何用Paljs解决n+1问题,graphql,apollo-client,prisma2,Graphql,Apollo Client,Prisma2,谢谢你的帮助 我在前端使用apollo客户端,在后端使用prisma2和graphql服务器 我想用@paljs/plugins解决n+1问题 在前端,我有一个查询帖子,如: query posts{ posts { id favoritedBy(where: { id: { equals: $currentUserId } }) { id } author { id
query posts{
posts {
id
favoritedBy(where: { id: { equals: $currentUserId } }) {
id
}
author {
id
avatar {
id
}
}
link {
id
}
games {
id
}
tags {
id
}
likes(where: { user: { id: { equals: $currentUserId } } }) {
id
}
}
}
冲突解决程序:
import { PrismaSelect } from '@paljs/plugins'
export const posts = queryField('posts', {
type: 'Post',
list: true,
args: {
...
},
resolve: async (_parent, args, { prisma, request }, info) => {
const select = new PrismaSelect(info).value
let opArgs: FindManyPostArgs = {
take: 10,
orderBy: {
[args.orderBy]: 'desc',
},
...select
}
const post = await prisma.post.findMany(opArgs)
//The result I want to return with the "sub-models" like likes, author tags...
console.log(JSON.stringify(post, undefined, 2))
return post
},
})
我正在记录查询
const prisma = new PrismaClient({
log: ['query'],
})
我的问题是:使用PrismaSelect时,我比不使用PrismaSelect时多了5个查询,如果我在前端检查请求时间,我需要使用PrismaSelect延长300-400ms。那我做错了什么?
我在文档中看到在上下文中选择。也许那是我的错误。如何在上下文中使用select
以下是我的背景:
import { PrismaClient, PrismaClientOptions } from '@prisma/client'
import { PubSub } from 'graphql-yoga'
import { PrismaDelete, onDeleteArgs } from '@paljs/plugins'
class Prisma extends PrismaClient {
constructor(options?: PrismaClientOptions) {
super(options)
}
async onDelete(args: onDeleteArgs) {
const prismaDelete = new PrismaDelete(this)
await prismaDelete.onDelete(args)
}
}
export const prisma = new PrismaClient({
log: ['query'],
})
export const pubsub = new PubSub()
export interface Context {
prisma: PrismaClient
request: any
pubsub: PubSub
}
export function createContext(request: any): Context {
return { prisma, request, pubsub }
}
您需要知道,要使用my PrismaSelect插件,您需要删除nexus prisma插件包,并使用my Pal.js CLI为nexus创建CRUD和ObjectType,并使用@paljs/nexus plugin添加到mackSchema函数中 从'@nexus/schema'导入{makeSchema}; 从“/graphql”导入*作为类型; 从“@paljs/nexus”导入{paljs};//导入我们的插件 export const schema=makeSchema{ 类型, 插件:[paljs],//这里我们的插件不使用nexus prisma插件 产出:{ 架构:_dirname+'/generated/schema.graphql', typegen:uu dirname+'/generated/nexus.ts', }, typegenAutoConfig:{ 资料来源:[ { 来源:require.resolve'/context', 别名:“上下文”, }, ], contextType:'Context.Context', }, }; 现在将此类型添加到上下文中 导出接口上下文{ prisma:PrismaClient 请求:任何 pubsub:pubsub 选择:任意//这里是我们的选择类型 } 导出函数createContextrequest:any:Context{ //我们的paljs插件将在解析器之前添加select对象 返回{prisma,request,pubsub,select:{} } 添加我们的插件后,您的查询将如下记录 扩展类型{ 键入:“查询”, 定义{ t、 字段“findOneUser”{ 键入:“用户”, 可为空:是的, args:{ 地点:arg{ 键入:“UserWhereUniqueInput”, 可为空:false, }, }, 解析{where},{prisma,select}{ //我们的插件为您将选择对象添加到上下文中 返回prisma.user.findOne{ 哪里 选择 }; }, }; }, };
你能试着使用我的pal c命令从我的列表中开始一个例子,并尝试你的模式并用它进行测试吗?它正在工作,thx Ahmed你的插件太棒了 我将Post对象从
const Post = objectType({
name: 'Post',
definition(t) {
t.model.id()
t.model.authorId()
t.model.tags()
t.model.games()
t.model.link()
t.model.report()
t.model.notifications()
t.model.author()
t.model.favoritedBy({
filtering: {
id: true,
},
})
t.model.likes({
filtering: {
user: true,
},
})
}
})
到
我还同时使用了nexus prisma插件和paljs插件但是我能知道你在哪里使用nexus prisma插件吗?不要自己转换类型。你可以使用my@paljs/cli pal g命令自动生成所有类型。我使用了nexus prisma插件。现在我用的是paljs。palg命令非常棒。使它变得非常简单和高效。又是Thx。我对我的帖子的回答没有@paljs/cli。但是现在我将@paljs/cli与generate命令一起使用。我很高兴能帮助您
const Post = objectType({
name: 'Post',
definition(t) {
t.string('id')
t.field('tags', {
nullable: false,
list: [true],
type: 'Tag',
resolve(parent: any) {
return parent['tags']
},
})
t.field('games', {
list: [true],
type: 'Game',
resolve(parent: any) {
return parent['games']
},
})
t.field('link', {
type: 'Link',
nullable: true,
resolve(parent: any) {
return parent['link']
},
})
t.field('notifications', {
list: [true],
type: 'Notification',
resolve(parent: any) {
return parent['notifications']
},
})
t.field('author', {
nullable: false,
type: 'User',
resolve(parent: any) {
return parent['author']
},
})
t.field('favoritedBy', {
nullable: false,
list: [true],
type: 'User',
args: {
where: 'UserWhereInput',
},
resolve(parent: any) {
return parent['favoritedBy']
},
})
t.field('likes', {
list: [true],
type: 'Like',
args: {
where: 'LikeWhereInput',
},
resolve(parent: any) {
return parent['likes']
},
})
},
})