首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Keystone 6自定义模式-突变不能工作-超过最大调用堆栈大小

Keystone 6自定义模式-突变不能工作-超过最大调用堆栈大小
EN

Stack Overflow用户
提问于 2022-08-16 17:00:25
回答 1查看 41关注 0票数 2

我试图在Keystone 6中扩展一个突变,但是仅仅让标准的DB更新在自定义的突变解析器中工作就有了很大的困难;使用标准Keystone样板并添加了一个新的集合/列表。

按照示例这里,我已经将custom-schema.ts与生成的schema.graphql匹配

schema.graphql (简化):

代码语言:javascript
复制
type Dog {
  id: ID!
  name: String
}

input DogWhereUniqueInput {
  id: ID
}

input DogUpdateInput {
  name: String
}

type Mutation {
  updateDog(
    where: DogWhereUniqueInput!
    data: DogUpdateInput!
  ): Dog
}

习俗示意图:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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操场上。很难理解发生了什么,为什么解析器会被垃圾处理,并产生这个错误。

代码语言:javascript
复制
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)

为什么会发生这种事,怎么解决呢?我漏掉了什么明显的东西吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-17 02:02:19

这是因为context.dbcontext.query在内部仍然使用CRUD的GraphQL API。而且,由于您的自定义突变updateDog与模式updateDog生成的突变具有相同的名称,所以这两个突变都会重复调用对方,从而导致错误RangeError: Maximum call stack size exceeded

你可以用两种方法之一来解决你的问题-

  1. 将您的自定义突变的名称更改为其他内容。例如:updateDogCustom
  2. (请注意)不要使用context.db.Dog.updateOne,而是使用prisma客户端跳过keystone的数据层,直接对数据库进行CRUD。请注意,这意味着如果您有钩子、访问控制或验证逻辑,它们将不会被调用。
代码语言:javascript
复制
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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73377653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档