【发布时间】:2021-07-09 16:57:06
【问题描述】:
我在 MongoDB 中有以下字段,我无法更改 DB 的架构
phoneNumbers: [
{ phoneNumber: '15141234567', type: 'WORK' },
{ phoneNumber: '15142233667', type: 'CELL' },
{ phoneNumber: '1517654321', type: 'HOME' }
],
架构是
const schemaPhoneNumber = new Schema({
type: {
type: String,
enum: ["WORK", "CELL", "HOME"],
alias: "phoneType", /////////////////////////////////// I have alias the field name
},
phoneNumber: String,
});
const GraphQLPhoneType = new GraphQLEnumType({
name: "PhoneType",
values: {
WORK: {
value: "WORK",
},
CELL: {
value: "CELL",
},
HOME: {
value: "HOME",
},
},
});
const GraphQLPhoneNumber = new GraphQLObjectType({
name: "PhoneNumber",
fields: () => ({
phoneType: { ////////////////////////////////////// it is not finding by this name
type: GraphQLNonNull(GraphQLPhoneType),
},
phoneNumber: {
type: GraphQLNonNull(GraphQLString),
},
}),
});
问题:当我运行查询时,它显示“phoneNumber”但“phoneType”返回 null。
如果我将“phoneType”更改为“type”,查询会显示正确的类型(WORK、CELL 等),但我必须使用“phoneType” " 在 graphQL 中,"type" 是 DB。
问题是什么,当我通过别名进行更改时,它在“GraphQLPhoneNumber”的字段中不可用,是嵌套 JSON 的问题还是我应该以不同的方式调用别名?
【问题讨论】:
标签: mongoose graphql mongoose-schema