tac*_*cos 6 plumatic-schema graphql prisma prisma-graphql
我按照Prisma 提供的GraphQL Prisma Typescript示例创建了一个简单的数据模型,为 Prisma 客户端和解析器生成了代码等。
我的数据模型包括以下节点:
type User {
id: ID! @unique
displayName: String!
}
type SystemUserLogin {
id: ID! @unique
username: String! @unique
passwordEnvironmentVariable: String!
user: User!
}
Run Code Online (Sandbox Code Playgroud)
我已经播种了系统用户和用户。
mutation {
systemUserLogin: createSystemUserLogin({
data: {
username: "SYSTEM",
passwordEnvironmentVariable: "SYSTEM_PASSWORD",
user: {
create: {
displayName: "System User"
}
}
}
})
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个示例突变login:
login: async (_parent, { username, password }, ctx) => {
let user
const systemUser = await ctx.db.systemUserLogin({ username })
const valid = systemUser && systemUser.passwordEnvironmentVariable && process.env[systemUser.passwordEnvironmentVariable] &&(process.env[systemUser.passwordEnvironmentVariable] === password)
if (valid) {
user = systemUser.user // this is always undefined!
}
if (!valid || !user) {
throw new Error('Invalid Credentials')
}
const token = jwt.sign({ userId: user.id }, process.env.APP_SECRET)
return {
token,
user: ctx.db.user({ id: user.id }),
}
},
Run Code Online (Sandbox Code Playgroud)
但无论我做什么,systemUser.user总是未定义的!
这是有道理的——在我不告诉它的情况下,客户端包装器如何知道递归到图中的“深度”?
但我怎样才能告诉它我想包含这种User关系呢?
prisma-client.但我的解析器似乎都没有被调用过......
export const SystemUserLogin: SystemUserLoginResolvers.Type<TypeMap> = {
id: parent => parent.id,
user: (parent, args, ctx: any) => {
console.log('resolving')
return ctx.db.systemUserLogin({id: parent.id}).user()
},
environmentVariable: parent => parent.environmentVariable,
systemUsername: parent => parent.systemUsername,
createdAt: parent => parent.createdAt,
updatedAt: parent => parent.updatedAt
};
Run Code Online (Sandbox Code Playgroud)
和...
let identity: UserParent;
const systemUserLogins = await context.db.systemUserLogins({
where: {
systemUsername: user,
}
});
const systemUserLogin = (systemUserLogins) ? systemUserLogins[0] : null ;
if (systemUserLogin && systemUserLogin.environmentVariable && process.env[systemUserLogin.environmentVariable] && process.env[systemUserLogin.environmentVariable] === password) {
console.log('should login!')
identity = systemUserLogin.user; // still null
}
Run Code Online (Sandbox Code Playgroud)
目前有两种方法可以解决这个问题:
您可以在此论坛帖子中了解有关 Prisma 客户端和 Prisma 绑定之间差异的更多信息。
由于 OP 目前正在使用 Prisma 客户端,我也将使用它来回答这个问题!
让我们看一下OP在问题中所做的陈述:
这是有道理的——在我不告诉它的情况下,客户端包装器如何知道递归到图中的“深度”?
OP 正确地指出,Prisma 客户端不知道如何深入了解图表以及要获取哪些关系。事实上,除非明确告知(例如使用$fragmentAPI),否则客户端将永远不会获取任何关系,并且始终只获取标量值。来自Prisma 文档:
每当使用 Prisma 客户端查询模型时,都会获取该模型的所有标量字段。无论查询单个对象还是对象列表都是如此。
那么,出现这种情况该如何妥善解决呢?事实上,解决方案不是改变Prisma 客户端的使用方式,而是实现一个额外的 GraphQL 解析器功能!
关于解析器的一点是,它们正在获取架构中特定字段的数据 。在OP的情况下,当前没有解析器可以“解析”类型user上定义的关系SystemUserLogin:
type SystemUserLogin {
id: ID! @unique
username: String! @unique
passwordEnvironmentVariable: String!
user: User! # GraphQL doesn't know how to resolve this
}
Run Code Online (Sandbox Code Playgroud)
要解决这种情况,您需要为其实现专用的“类型解析器”,如下所示:
const resolvers = {
SystemUserLogin: {
user(parent, args, ctx) {
return ctx.db.systemUserLogin({id: parent.id}).user()
}
}
}
Run Code Online (Sandbox Code Playgroud)
全面披露:我在 Prisma 工作,我们正在努力为该用例添加更好的文档和资源。另请查看此示例,其中出于相同原因需要author和关系字段的显式解析器。posts
希望有帮助!
编辑:我们还在 Prisma 教程中添加了关于常见解析器模式的稍微更彻底的解释。
| 归档时间: |
|
| 查看次数: |
6689 次 |
| 最近记录: |