我正在尝试使用Serverless和AWS部署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';这将导入到另一个模型的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发布于 2021-08-05 23:05:54
我不能确定这是你的问题,但我在使用nest时也犯了同样的错误,而且离线时没有服务器。
解决这个问题的方法是将--allowCache添加到无服务器脱机命令中。见Docs
npx serverless offline --allowCache没有这一点,我的server变量没有被缓存,但是不知怎么的,OrphanedReferenceRegistry是在函数调用之间缓存的。每次我访问我的URL时,我的服务器再次被引导,我的ObjectType就被添加到现有的注册表中,这会在创建模式时触发错误,因为有相同2+的ObjectType。
以下是我的备注,以防对有类似问题的人有帮助,https://github.com/pope-12/nest-graphql-serverless-error
https://stackoverflow.com/questions/68119333
复制相似问题