我正在try 扩展Keystone 6中的一个Mutations ,但仅仅是让标准的DB更新在一个定制的Mutations 解析器中工作就有很大的困难;使用标准的Keystone样板并添加了一个新的集合/列表.

在示例here之后,我将custom-schema.ts与生成的schema.graphql进行了匹配

Schema.graph ql(简体):

type Dog {
  id: ID!
  name: String
}

input DogWhereUniqueInput {
  id: ID
}

input DogUpdateInput {
  name: String
}

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

自定义-架构.ts:

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 Error处理程序反复收到此错误.同样的事情也发生在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)

为什么会发生这种情况,如何解决?我是不是漏掉了什么明显的东西?

推荐答案

这是因为context.dbcontext.query在内部仍然使用用于CRUD的GraphQL API.由于您的自定义Mutations updateDog也与从模式updateDog生成的Mutations 具有相同的名称,因此这两个Mutations 都在重复调用对方,因此出现错误RangeErr或: Maximum call stack size exceeded.

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

  1. 将您的自定义Mutations 的名称更改为其他名称.例.updateDogCustom

  2. (Practice caution) Instead of context.db.Dog.updateOne, use the prisma client to skip keystone's data layer and CRUD the database directly. Be warned, this means if you have hooks, access control 或 validation logic in place they won't be invoked.

exp或t 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 (updateErr或: any) {
          throw updateErr或;
        }
      },
      updateDogCustom: async (
        root,
        { where: { id }, data: { name } },
        context
      ) => {
        try {
          const response = await context.db.Dog.updateOne({
            where: { id },
            data: { name },
          });

          return response;
        } catch (updateErr或: any) {
          throw updateErr或;
        }
      },
    },
  },
});

Codesandbox here — https://codesandbox.io/s/winter-shadow-fz689e?file=/src/custom-schema.ts

你可以从/api/graphql路径的codesandbox直接运行GraphQLplayground .例.https://fz689e.sse.codesandbox.io/api/graphql

Javascript相关问答推荐

鼠标移动时更新画布

使用Apps Script缩短谷歌表中的URL?

Angular material 表多个标题行映射

为什么使用MAX_SAFE_INTEGER生成随机整数时数字分布不准确?

是什么原因导致此Angular 16应用程序中类型错误时属性结果不存在?

在贝塞尔曲线的直线上找不到交叉点:(使用@Pomax的bezier.js)

使用Java脚本根据按下的按钮更改S文本

不能将空字符串传递给cy.containes()

如何在使用rhandsontable生成表时扩展数字输入验证?

如何在ASP.NET项目中使用Google Chart API JavaScript将二次轴线值格式化为百分比

如何在不影响隐式类型的情况下将类型分配给对象?

Angular 形式,从DOM中删除不会删除指定索引处的内容,但会删除最后一项

为什么我的按钮没有从&q;1更改为&q;X&q;?

触发异步函数后不能显示数据

JWT Cookie安全性

在HTML5画布上下文中使用putImageData时,重载解析失败

在没有任何悬停或其他触发的情况下连续交换图像

TabNavigator和StackNavigator之间的Reaction Native中的导航问题

使用Library chart.js在一个带有两个y轴的图表中绘制两个数据集

JSX/React -如何在组件props 中循环遍历数组