我试图在Keystone 6中扩展一个突变,但是仅仅让标准的DB更新在自定义的突变解析器中工作就有了很大的困难;使用标准Keystone样板并添加了一个新的集合/列表。
按照示例这里,我已经将custom-schema.ts与生成的schema.graphql匹配
schema.graphql (简化):
type Dog {
id: ID!
name: String
}
input DogWhereUniqueInput {
id: ID
}
input DogUpdateInput {
name: String
}
type Mutation {
updateDog(
where: DogWhereUniqueInput!
data: DogUpdateInput!
): Dog
}习俗示意图:
import { graphQLSchemaExtension } from '@keystone-6/core';
import { Context } from '.keystone/types';
export const extendGraphqlSchema = graphQLSchemaExtension<Context>({
typeDefs: `
type Mutation {
""" update a dog """
updateDog(
where: DogWhereUniqueInput!
data: DogUpdateInput!
): Dog
}
`,
resolvers: {
Mutation: {
updateDog: async (root, { where, id }, context) => {
try {
const response = await context.db.Dog.updateOne({
where: { id },
data: { name: 'updated name'}
});
return response;
} catch (updateError: any) {
throw updateError;
}
}}
}
},
);keystone.ts:
import { extendGraphqlSchema } from './custom-schema';
// ...
export default withAuth(
config({
db: {
provider: 'sqlite',
url: 'file:./keystone.db',
},
ui: {
isAccessAllowed: (context) => !!context.session?.data,
},
lists,
session,
extendGraphqlSchema,
})
);当我从(样板) UI触发一个更新时,我会从catch错误处理程序中反复得到这个错误。同样的情况发生在graphQL操场上。很难理解发生了什么,为什么解析器会被垃圾处理,并产生这个错误。
RangeError: Maximum call stack size exceeded
at isLeafType (.../poc/node_modules/graphql/type/definition.js:247:20)
at coerceInputValueImpl (.../poc/node_modules/graphql/utilities/coerceInputValue.js:122:34)为什么会发生这种事,怎么解决呢?我漏掉了什么明显的东西吗?
发布于 2022-08-17 02:02:19
这是因为context.db和context.query在内部仍然使用CRUD的GraphQL API。而且,由于您的自定义突变updateDog与模式updateDog生成的突变具有相同的名称,所以这两个突变都会重复调用对方,从而导致错误RangeError: Maximum call stack size exceeded。
你可以用两种方法之一来解决你的问题-
updateDogCustom
或context.db.Dog.updateOne,而是使用prisma客户端跳过keystone的数据层,直接对数据库进行CRUD。请注意,这意味着如果您有钩子、访问控制或验证逻辑,它们将不会被调用。export const extendGraphqlSchema = graphQLSchemaExtension<Context>({
typeDefs: `
type Mutation {
""" update a dog """
updateDog(
where: DogWhereUniqueInput!
data: DogUpdateInput!
): Dog
""" update a dog custom """
updateDogCustom(
where: DogWhereUniqueInput!
data: DogUpdateInput!
): Dog
}
`,
resolvers: {
Mutation: {
updateDog: async (root, { where: { id }, data: { name } }, context) => {
try {
const response = await context.prisma.dog.update({
where: { id },
data: { name },
});
return response;
} catch (updateError: any) {
throw updateError;
}
},
updateDogCustom: async (
root,
{ where: { id }, data: { name } },
context
) => {
try {
const response = await context.db.Dog.updateOne({
where: { id },
data: { name },
});
return response;
} catch (updateError: any) {
throw updateError;
}
},
},
},
});这里的代码框- https://codesandbox.io/s/winter-shadow-fz689e?file=/src/custom-schema.ts
您可以从/api/graphql路径的codesandbox中直接运行graphql操场。例如:https://fz689e.sse.codesandbox.io/api/graphql
https://stackoverflow.com/questions/73377653
复制相似问题