首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Serverless时,GraphQL“架构必须包含唯一命名类型”

使用Serverless时,GraphQL“架构必须包含唯一命名类型”
EN

Stack Overflow用户
提问于 2021-06-24 16:16:13
回答 1查看 484关注 0票数 3

我正在尝试使用Serverless和AWS部署NestJS GraphQL API服务器。在本地运行应用程序时,我可以使用GraphQL操场而不会出现任何问题,但是当脱机运行Serverless时,我会得到以下错误:

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

错误声明ObjectTypes、ConstellationAffix并不是唯一的。这两种类型都是ObjectTypes,表示字段的类型:

模型模式

代码语言:javascript
复制
// 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);
代码语言:javascript
复制
// 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);

进口

由于我已经看到导入这些模型可能是问题的根源,所以我还包括了一个示例导入:

代码语言:javascript
复制
import { Affix, ArtifactSet } from '../artifact-set/artifact-set.model';

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

serverless.yml

代码语言:javascript
复制
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. 从架构中移除猫鼬默认导出
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-05 23:05:54

我不能确定这是你的问题,但我在使用nest时也犯了同样的错误,而且离线时没有服务器。

解决这个问题的方法是将--allowCache添加到无服务器脱机命令中。见Docs

代码语言:javascript
复制
npx serverless offline --allowCache

没有这一点,我的server变量没有被缓存,但是不知怎么的,OrphanedReferenceRegistry是在函数调用之间缓存的。每次我访问我的URL时,我的服务器再次被引导,我的ObjectType就被添加到现有的注册表中,这会在创建模式时触发错误,因为有相同2+的ObjectType。

以下是我的备注,以防对有类似问题的人有帮助,https://github.com/pope-12/nest-graphql-serverless-error

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68119333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档