graphql - 在 gql 上下文中获取下一个身份验证用户会话时遇到问题
问题描述
这篇文章中提供的答案对我不起作用,并且没有更新。 在 Apollo Server 上下文中获取 NextAuth.js 用户会话
// ./graphql/context.ts
/**
* Populates a context object for use in resolvers.
* If there is a valid session from next-auth, find the user and add them to context
* @param context context from apollo server
*/
type ApolloApiContext = ApolloContext < {
req: IncomingMessage
} > ;
export async function createContext(context: ApolloApiContext): Promise < Context > {
const session = await getSession(context); // ALWAYS null
let user: User | null = null;
if (session?.user?.email) {
user = await prisma.user.findUnique({ where: { email: session.user.email } });
}
return {
db: prisma,
prisma,
user,
};
}
我猜 Next v Apollo 对请求的期望不匹配。任何想法如何将有效的下一个身份验证会话放入 gql 上下文?
解决方案
const session = await getSession({ req: context.req });
推荐阅读
- javascript - 如何将开槽输入字段的表单属性设置为 shadow dom 内的表单?
- dart - 如何检查 Sqflite DB 是否包含特定的 id?-扑
- macos - 不同SVN GUI的兼容性
- sql - 来自同一个 SQL Server 表的水平和垂直输出
- objective-c - 如何在 Swift 中更改覆盖函数返回类型
- qlikview - 设置具有多个排除项的分析表达式不同计数
- python - Netmiko FileTransfer 与 python
- html - 边界问题
- python - 平均数组中的值对应于另一个数组的值
- objective-c - 使用 Swift 从父 pod 导入 Objective-C 供应商框架(用于混合项目)