服务器返回的“__typename”与 graphql 模式不匹配

`__typename` returned by the server isn't match to graphql schema

当我使用 type-graphql 构建架构并使用 apollo-server-lambda 创建服务器并将其部署到 netlify functions 时,发生了错误。

type-graphql

生成的架构
# -----------------------------------------------
# !!! THIS FILE WAS GENERATED BY TYPE-GRAPHQL !!!
# !!!   DO NOT MODIFY THIS FILE BY YOURSELF   !!!
# -----------------------------------------------

type Project {
  name: String!
  summary: String!
  titleAndSummary: String!
}

type Query {
  projects: [Project!]!
}

问题

我想当我查询到 API

{
  __schema {
    types {
      name
    }
  }
}

API必须回复

{
    "data": {
        "__schema": {
            "types": [
                {
                    "name": "Query"
                },
                {
                    "name": "Project"
                },
                // ...
            ]
        }
    }
}

但是我得到了

{
    "data": {
        "__schema": {
            "types": [
                {
                    "name": "Query"
                },
                {
                    "name": "a"
                },
                // ...
            ]
        }
    }
}

我打算将 graphql 与 Elm-Graphql 一起使用,使用它们的 __typename 属性 自动生成模块。这就是为什么我必须解决上述问题。

我努力解决了还是找不到问题出在哪里。似乎在 type-graphqlapollo-server 甚至 netlify functions 中(我尝试了 netlify 开发服务器和实际部署的真实服务器)

代码

graphql.ts

import 'reflect-metadata';
import { ApolloServer } from 'apollo-server-lambda';
import { buildSchemaSync } from 'type-graphql';
import { QueryResolver, ProjectResolver } from './resolver';

const schema = buildSchemaSync({
  resolvers: [QueryResolver, ProjectResolver],
});

const server = new ApolloServer({
  schema,
  debug: false,
});

export const handler = server.createHandler();

types.ts

import { ObjectType, Field } from 'type-graphql';

@ObjectType()
export class Query {
  @Field((type) => [Project!]!)
  projects!: Project[];
}

@ObjectType()
export class Project {
  @Field((type) => String!)
  name!: string;

  @Field((type) => String!)
  summary!: string;
}

resolver.ts

import { Resolver, Query, FieldResolver, Root } from 'type-graphql';
import { Project } from './types';
import { projectList } from '../../db/fakeDB';

@Resolver()
export class QueryResolver {
  @Query((returns) => [Project])
  projects() {
    return projectList;
  }
}

@Resolver((of) => Project)
export class ProjectResolver {
  @FieldResolver((returns) => String!)
  titleAndSummary(@Root() project: Project) {
    return project.name + ' ++ ' + project.summary;
  }
}

试用

__typename直接添加到对象类型,如下所示

 @Resolver((of) => Project)
export class ProjectResolver {
  @FieldResolver((returns) => String!)
  titleAndSummary(@Root() project: Project) {
    return project.name + ' ++ ' + project.summary;
  }

  @FieldResolver((returns) => String!)
  __typename() {
    return 'Project';
  }
}

但是它在请求时抛出错误

◈ Error during invocation:  {
  errorMessage: 'Name "__typename" must not begin with "__", which is reserved by GraphQL introspection.',
  errorType: 'Error',
...

我该如何解决?

因为我英文不好,担心万一写错了。请让我知道并更正。谢谢。

看起来你已经打开了包缩小,所以所有 class 名称都被破坏了。

我们在生产中 运行 时使用 SST 和 TypeGraphQL 看到了这一点。 ObjectType() 和 InputType() classes 的“__typename”属性正在转换为两个字母代码。 See photo.

Michael,您能否详细说明 TypeGraphQL 中的包缩小以及如何配置它以维护源代码 ObjectType() 和 InputType() 声明的 class 名称?

"@serverless-stack/cli": "0.54.4",
"@serverless-stack/resources": "0.54.4",    
"apollo-server-lambda": "^2.21.1",
"type-graphql": "^1.1.1",

const server = new ApolloServer({
  schema: buildSchemaSync({
    resolvers: [getWebPaths],
    scalarsMap: [],
    validate: false,
  }),

  playground: true, //IS_LOCAL,
  introspection: true, // IS_LOCAL,

  context: async ({
    context,
    event,
  }: {
    event: APIGatewayEvent
    context: Context
  }): Promise<LambdaRoot> => {
    context.callbackWaitsForEmptyEventLoop = false

    return {
      event,
      context,
    }
  },
})