2015-08-26 21 views
7

Relay ve GraphQL'i deniyorum. Ben şema yapıyorum zaman yapıyorum: Böyle bir makale için sorduğunuzdaRöle/GraphQL 'çözümü' nasıl çalışır?

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], 
}) 

Yani,:

{ 
    article(id: 1) { 
    id, 
    title, 
    author 
    } 
} 

o veritabanına 3 sorguları yapacak mı? Demek istediğim, her alanın veritabanına bir istek yapan bir çözüm yöntemi (getTitle, getAuthor ...) var. Bunu yanlış mı yapıyorum? resolve yöntem article aktarılırsa

articleSchema.methods.getAuthor = function(id){ 
    let article = this.model('Article').findOne({_id: id}) 
    return article.author 
} 

cevap

4

, sadece mülk erişemez:

Bu getAuthor (ı mongoose kullanın) bir örnektir? (Eğer bir makale örneği diyoruz çünkü)

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.title, 
    }, 
    author: { 
     type: userConnection, 
     description: 'The author of the article', 
     resolve: (article) => article.author, 
    }, 
    }), 
    interfaces: [nodeInterface], 
}) 

Gelincik içinde Schema.methods yana modele üzerinde yöntemleri tanımlar, bu makale için bir kimlik kabul etmedi. Eğer yöntemini tutmak istiyorsa Yani, tıpkı olacaktır:

articleSchema.methods.getAuthor = function() { 
    return article.author; 
} 

Idiyse şey sen mesela bakmak gerekir Başka bir koleksiyonda, sonra ayrı bir sorgu (refs kullanmayan varsayarak varsayalım) yapmanız gerekir (

articleSchema.methods.getAuthor = function(callback) { 
    return this.model('Author').find({ _id: this.author_id }, cb); 
}