【发布时间】: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 Constellation 和 Affix 不是唯一的。这些都是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
尝试
根据对面临类似问题的用户的研究,我尝试了以下方法:
- 仔细检查导入没有拼写错误,大小写相同
- 更改类名,在装饰器中指定名称(例如
ObjectType('Constellation') - 将导入更改为使用绝对路径 (
src/../..) - 使用类型/接口代替
ObjectType - 从架构中删除猫鼬默认导出
【问题讨论】:
-
添加实际错误可能会有所帮助。你能添加显示的日志吗?
标签: mongodb graphql nestjs serverless