Graphql结果不使用mongoose填充关联类型中的结果
我尝试在Mongoose中创建带有ObjectId引用的模式,并能够使用Apollo Server通过GraphQL查询它们 我已经定义了一本非常基本的书,作者ObjectId ref在mongoose中就是这样定义的Graphql结果不使用mongoose填充关联类型中的结果,mongoose,graphql,Mongoose,Graphql,我尝试在Mongoose中创建带有ObjectId引用的模式,并能够使用Apollo Server通过GraphQL查询它们 我已经定义了一本非常基本的书,作者ObjectId ref在mongoose中就是这样定义的 const { ObjectId } = mongoose.Schema.Types; const AuthorSchema = new mongoose.Schema({ name: String }); const BookSchema = new mongoose.S
const { ObjectId } = mongoose.Schema.Types;
const AuthorSchema = new mongoose.Schema({
name: String
});
const BookSchema = new mongoose.Schema({
name: String,
author: [{ type: ObjectId, ref: 'Author' }]
});
type Author {
id: ID!
name: String
}
type Book {
id: ID!
name: String
author: Author
}
type Query {
authors: [Author]
author(id: ID!): Author
books: [Book]
book(id: ID!): Book
}
input AddAuthorInput {
name: String!
}
input AddBookInput {
name: String!
author: ID!
}
type Mutation {
addAuthor(input: AddAuthorInput): Author
addBook(input: AddBookInput): Book
}
addBook: async (_, args) => {
try {
const {
input
} = args;
return Book.create(input);
} catch (e) {
return e.message
}
}
book: async (_, args) => {
const { id } = args;
const result = await Book.findById(id).populate('author').exec();
console.warn('====== Book query result ======');
console.log(JSON.stringify(result, null, 2));
console.warn('====== End Book query result ======');
return result;
有这样一个graphql模式
const { ObjectId } = mongoose.Schema.Types;
const AuthorSchema = new mongoose.Schema({
name: String
});
const BookSchema = new mongoose.Schema({
name: String,
author: [{ type: ObjectId, ref: 'Author' }]
});
type Author {
id: ID!
name: String
}
type Book {
id: ID!
name: String
author: Author
}
type Query {
authors: [Author]
author(id: ID!): Author
books: [Book]
book(id: ID!): Book
}
input AddAuthorInput {
name: String!
}
input AddBookInput {
name: String!
author: ID!
}
type Mutation {
addAuthor(input: AddAuthorInput): Author
addBook(input: AddBookInput): Book
}
addBook: async (_, args) => {
try {
const {
input
} = args;
return Book.create(input);
} catch (e) {
return e.message
}
}
book: async (_, args) => {
const { id } = args;
const result = await Book.findById(id).populate('author').exec();
console.warn('====== Book query result ======');
console.log(JSON.stringify(result, null, 2));
console.warn('====== End Book query result ======');
return result;
addBook和book查询的解析器中的变异部分如下所示
const { ObjectId } = mongoose.Schema.Types;
const AuthorSchema = new mongoose.Schema({
name: String
});
const BookSchema = new mongoose.Schema({
name: String,
author: [{ type: ObjectId, ref: 'Author' }]
});
type Author {
id: ID!
name: String
}
type Book {
id: ID!
name: String
author: Author
}
type Query {
authors: [Author]
author(id: ID!): Author
books: [Book]
book(id: ID!): Book
}
input AddAuthorInput {
name: String!
}
input AddBookInput {
name: String!
author: ID!
}
type Mutation {
addAuthor(input: AddAuthorInput): Author
addBook(input: AddBookInput): Book
}
addBook: async (_, args) => {
try {
const {
input
} = args;
return Book.create(input);
} catch (e) {
return e.message
}
}
book: async (_, args) => {
const { id } = args;
const result = await Book.findById(id).populate('author').exec();
console.warn('====== Book query result ======');
console.log(JSON.stringify(result, null, 2));
console.warn('====== End Book query result ======');
return result;
当我问这个问题时
query book {
book(id: "xxxxxxx") {
id
name
author {
name
}
}
}
我在author.name中得到null,同时我可以从控制台输出中看到.populate()能够从Authors集合中获得正确的结果
这包括我创建的示例代码您的mongoose模式设置为每本书返回多个作者:
author: [{ type: ObjectId, ref: 'Author' }]
如果只有一位作者,那么只需执行以下操作:
author: { type: ObjectId, ref: 'Author' }
不能返回GraphQL需要单个对象的数组,反之亦然。如果希望保持mongoose模式不变,则需要将作者
字段返回的类型更改为作者列表
:
author: [Author]
你是对的,事实上,在mongoose schemaHi中设置作者数组是一个错误!我有一个小问题:如果我也需要获得作者的身份证怎么办?你的问题救了我一天