Ich probiere Relay und GraphQL aus. Wenn ich das Schema mache ich das tue:Wie funktioniert Relay/GraphQL 'resolve'?
let articleQLO = new GraphQLObjectType({
name: 'Article',
description: 'An article',
fields:() => ({
_id: globalIdField('Article'),
title: {
type: GraphQLString,
description: 'The title of the article',
resolve: (article) => article.getTitle(),
},
author: {
type: userConnection,
description: 'The author of the article',
resolve: (article) => article.getAuthor(),
},
}),
interfaces: [nodeInterface],
})
Also, wenn ich für einen Artikel wie diesen fragen:
{
article(id: 1) {
id,
title,
author
}
}
Wird es tun 3 Anfragen an die Datenbank? Ich meine, jedes Feld hat eine resolve-Methode (getTitle, getAuthor ...), die eine Anfrage an die Datenbank stellt. Mache ich das falsch?
Dies ist ein Beispiel für getAuthor (ich benutze Mungo):
articleSchema.methods.getAuthor = function(id){
let article = this.model('Article').findOne({_id: id})
return article.author
}