1

嘿,我正在尝试通过“外部”反应、next 和 apollo 前端让自定义突变在 keystone-next 中工作,但是从前端调用突变时遇到问题,但从 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 请求正文是:

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

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

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

当我使用完全相同的标头从 Postman 运行这些请求时,第一个仍然有效,而第二个仍然无效(没有身份验证等)。400 bad request response 中返回的错误如下:

{
    "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)"
                    ]
                }
            }
        }
    ]
}

我错过了什么?

4

1 回答 1

1

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

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

testAvailability返回 a 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.

哪个更有帮助!

于 2021-06-24T04:24:50.913 回答