Keystone 下一个自定义 graphql 变异和外部前端

Keystone next custom graphql mutation and external frontend

嘿,我正在尝试让自定义突变在 keystone-next 中使用“外部”反应、next 和 apollo 前端,但是我在从前端调用突变时遇到问题,但不是从 Keystone api 资源管理器调用它时。突变真的很简单。

架构扩展:

export const extendedGqlSchema = graphQLSchemaExtension({
  typeDefs: graphql`
    type Mutation {
      testAvailability(name: String): String
    }
  `,
  resolvers: {
    Mutation: {
      testAvailability: testAvailability,
    },
  },
});

解析函数(不同文件):

export const testAvailability = (
  root: any, 
  { name }: { name: string },
  context: KeystoneContext
): string => {
  if (context) {
    return new Date().toUTCString() + ": Hi " + name + ", I'm here!";
  }
  return new Date().toUTCString() + ": No context";
};

有效的原始 http 请求 body 是:

{
    "operationName": null,
    "variables": {},
    "query": "mutation {\n  testAvailability(name: \"Eve\")\n}\n"
}

而原始 http 前端生成的请求 body 如下所示:

{
    "operationName": "testAvailability",
    "variables": {
        "name": "Eve"
    },
    "query": "mutation testAvailability($name: String) {\n  testAvailability(name: $name) {\n    name\n    __typename\n  }\n}\n"
}

当我 运行 来自 Postman 的这些请求具有完全相同的 headers 时,第一个仍然有效,而第二个仍然无效(无身份验证等)。 400 错误请求响应中返回的错误如下:

{
    "errors": [
        {
            "message": "Cannot assign to read only property 'path' of object '#<Object>'",
            "extensions": {
                "code": "INTERNAL_SERVER_ERROR",
                "exception": {
                    "stacktrace": [
                        "TypeError: Cannot assign to read only property 'path' of object '#<Object>'",
                        "    at formatError (%ProjectPath%\backend\node_modules\@keystone-next\keystone\dist\createApolloServer-231615cf.cjs.dev.js:82:24)",
                        "    at %ProjectPath%\backend\node_modules\apollo-server-errors\src\index.ts:287:28",
                        "    at Array.map (<anonymous>)",
                        "    at Object.formatApolloErrors (%ProjectPath%\backend\node_modules\apollo-server-errors\src\index.ts:285:25)",
                        "    at formatErrors (%ProjectPath%\backend\node_modules\apollo-server-core\src\requestPipeline.ts:665:12)",
                        "    at %ProjectPath%\backend\node_modules\apollo-server-core\src\requestPipeline.ts:649:15",
                        "    at Generator.next (<anonymous>)",
                        "    at fulfilled (%ProjectPath%\backend\node_modules\apollo-server-core\dist\requestPipeline.js:5:58)",
                        "    at runMicrotasks (<anonymous>)",
                        "    at processTicksAndRejections (internal/process/task_queues.js:93:5)"
                    ]
                }
            }
        }
    ]
}

我错过了什么?

如果您查看来自前端的第二个查询,基本上是这样的:

mutation testAvailability($name: String) {
  testAvailability(name: $name) {
    name
    __typename
  }
}

但是testAvailabilityreturns一个String,不是对象吧?正确的查询可能更接近于此...

mutation testAvailability($name: String) {
  testAvailability(name: $name)
}

基本上,检查您的前端查询。

我不确定 Cannot assign to read only property 'path' of object '#<Object>'" 错误是从哪里来的...如果我重现此错误,我会收到 GRAPHQL_VALIDATION_FAILED 错误:

Field "testAvailability" must not have a selection since type "String" has no subfields.

哪个更有帮助!