问题描述
所以我创建了数据库模型并在我的 Node.js 应用程序中测试了我的服务,它们似乎工作正常。我遇到的问题是当我尝试将我从数据库返回的文档连接到我的 GraphQL 服务时。 我通常得到这个,不是所有的时间
Type 'string | null | undefined' is not assignable to type 'string | undefined'.
Type 'null' is not assignable to type 'string | undefined'.
我明白为什么会发生这个错误,这是有道理的。在 graphql-codegen
生成的代码中,我得到 Maybe
,我认为这假设您可以获得 undefined
或 null
。
我的文档界面如下
export interface IExperience extends Document {
userId: string;
title: string;
description: string;
type?: string;
category?: ICategory;
state: ExperienceState;
requests?: IRequest[];
images?: string;
originDate: Date;
finalDate?: Date;
origin?: ILocation;
destination?: ILocation;
}
现在我可能可以通过添加“| null”来修复它,但必须有更好的解决方案,我只是觉得我可能以错误的方式使用这些工具。 奇怪的是我并没有一直收到这个错误。
所以当我创建一个 experience
时我没有遇到任何问题
以下是我的 .graphql 文件中的 create experience
。
createExperience(
title: String!
description: String!
type: String!
state: String
originDate: Date!
finalDate: Date
origin: LocationInput!
destination: LocationInput
): Experience
但是当我更新 experience
时,除了 ID
之外的所有字段都出现上述错误
以下是我的 .graphql 文件中的 updateExperience
updateExperience(
_id: ID!
title: String
description: String
type: String
state: String
originDate: String
finalDate: String
origin: LocationInput
destination: LocationInput
): Experience
下图您可以在 Visual Studio Code 上看到错误
以下是createExperience
和updateExperience
的接口
export interface ICreateExperience {
userId: IExperience['userId'];
title: IExperience['title'],description: IExperience['description'];
type?: IExperience['type'];
state: IExperience['state'];
category?: IExperience['category'];
requests?: IExperience['requests'];
images?: IExperience['images'];
originDate: IExperience['originDate'];
finalDate?: IExperience['finalDate'];
origin: IExperience['origin'];
destination?: IExperience['destination'];
}
interface IUpdateExperience {
[key: string]: string | undefined | IExperience['category'] | IExperience['requests'] | IExperience['originDate'] | IExperience['origin'],_id: IExperience['_id'];
title?:IExperience['title'] ;
description?: IExperience['description'];
type?: IExperience['type'];
category?: IExperience['category'];
state?: IExperience['state'];
requests?: IExperience['requests'];
images?: IExperience['images'];
originDate?: IExperience['originDate'];
finalDate?: IExperience['finalDate'];
origin?: IExperience['origin'];
destination?: IExperience['destination'];
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)