【问题标题】:GraphQL "Schema must contain uniquely named types" when using Serverless使用无服务器时,GraphQL“架构必须包含唯一命名的类型”
【发布时间】:2021-09-08 03:50:06
【问题描述】:

我正在尝试使用无服务器和 AWS Lambda 部署 NestJS GraphQL API 服务器。在本地运行应用程序时,我可以毫无问题地使用 GraphQL 游乐场,但在离线运行 Serverless 时,我收到以下错误:

Error: Schema must contain uniquely named types but contains multiple types named "Constellation".

错误指出 ObjectTypes ConstellationAffix 不是唯一的。这些都是ObjectTypes,代表字段的类型:

模型架构

// character.model.ts

import mongoose, { Document, Schema as MongooseSchema } from 'mongoose';

import { Field, ObjectType } from '@nestjs/graphql';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';

@ObjectType('Constellation')
class Constellation {
  @Field(() => String)
  effect: string;

  @Field(() => Number)
  oid: number;

  @Field(() => String)
  name: string;

  @Field(() => Number)
  pos: number;

  @Field(() => String)
  icon: string;
}

@ObjectType()
@Schema({ timestamps: true })
export class Character {
  @Field(() => String)
  _id: MongooseSchema.Types.ObjectId;

  @Field(() => Number)
  @Prop({ required: true, unique: true })
  oid: number;

  @Field(() => [Constellation])
  @Prop({ required: true })
  constellations: Constellation[];

  @Field(() => String)
  @Prop({ required: true })
  element: string;

  @Field(() => String)
  @Prop({ required: true })
  name: string;

  @Field(() => Number)
  @Prop({ required: true })
  rarity: number;

  @Field(() => String)
  @Prop({ required: true })
  icon: string;

  @Field(() => String)
  @Prop({ required: true })
  image: string;
}

export type CharacterDocument = Character & Document;
export const CharacterSchema = SchemaFactory.createForClass(Character);
export default mongoose.model<CharacterDocument>(Character.name, CharacterSchema);
// artifact-set.model.ts

import mongoose, { Document, Schema as MongooseSchema } from 'mongoose';

import { Field, ObjectType } from '@nestjs/graphql';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';

@ObjectType('Affix')
export class Affix {
  @Field(() => Number)
  activation_number: number;

  @Field(() => String)
  effect: string;
}

@ObjectType()
@Schema({ timestamps: true })
export class ArtifactSet {
  @Field(() => String)
  _id: MongooseSchema.Types.ObjectId;

  @Field(() => Number)
  @Prop({ required: true, unique: true })
  oid: number;

  @Field(() => [Affix])
  @Prop({ required: true })
  affixes: Affix[];

  @Field(() => String)
  @Prop({ required: true })
  name: string;
}

export type ArtifactSetDocument = ArtifactSet & Document;
export const ArtifactSetSchema = SchemaFactory.createForClass(ArtifactSet);
export default mongoose.model<ArtifactSetDocument>(ArtifactSet.name, ArtifactSetSchema);

进口

由于我发现导入这些模型可能是问题的根源,因此我还提供了一个示例导入:

import { Affix, ArtifactSet } from '../artifact-set/artifact-set.model';

这被导入到另一个模型的../character/character.service 中的service 文件中,而不是在其他地方导入。 Constellation ObjectType 没有在其他任何地方显式导入,但它是第一个引发错误的类型。

serverless.yml

app: server
service: server-api
useDotenv: true

package:
  patterns:
    - '!dist/**'
    - '!src/seeds/**'

plugins:
  - serverless-plugin-typescript
  - serverless-offline

# custom:
#   serverless-offline:
#     allowCache: true

provider:
  name: aws
  profile: serverless-admin
  runtime: nodejs12.x
  lambdaHashingVersion: 20201221

functions:
  main:
    handler: src/lambda.handler
    events:
      - http:
          path: graphql
          method: POST
          cors: true
          integration: LAMBDA
      - http:
          path: graphql
          method: GET
          cors: true
          integration: LAMBDA
      - http:
          path: playground
          method: ANY
          cors: true
          integration: LAMBDA

尝试

根据对面临类似问题的用户的研究,我尝试了以下方法:

  1. 仔细检查导入没有拼写错误,大小写相同
  2. 更改类名,在装饰器中指定名称(例如ObjectType('Constellation')
  3. 将导入更改为使用绝对路径 (src/../..)
  4. 使用类型/接口代替ObjectType
  5. 从架构中删除猫鼬默认导出

【问题讨论】:

  • 添加实际错误可能会有所帮助。你能添加显示的日志吗?

标签: mongodb graphql nestjs serverless


【解决方案1】:

我不能确定这是你的问题,但我在使用 nest 和 serverless-offline 时遇到了同样的错误。

为我解决的问题是将--allowCache 添加到无服务器离线命令中。 See Docs

npx serverless offline --allowCache

没有它,我的 server 变量没有被缓存,但不知何故 OrphanedReferenceRegistry 在函数调用之间被缓存。每次我点击我的 URL 时,我的服务器都会再次引导,我的 ObjectType 被添加到现有的注册表中,这在创建架构时触发了错误,因为有 2+ 个相同的 ObjectType。

以下是我的参考笔记,以帮助遇到类似问题的任何人 https://github.com/pope-12/nest-graphql-serverless-error

【讨论】:

    猜你喜欢
    • 2023-01-01
    • 2020-06-28
    • 2022-12-22
    • 2019-02-06
    • 2021-05-08
    • 2019-04-19
    • 2021-08-24
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多