2015-08-26 9 views
7

मैं रिले और ग्राफक्यूएल का प्रयास कर रहा हूं। जब मैं स्कीमा कर रहा हूँ मैं यह कर रहा हूं:रिले/ग्राफक्यूएल 'संकल्प' कैसे काम करता है?

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

तो, जब मैं इस तरह एक लेख के लिए पूछना:

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

यह 3 प्रश्नों डेटाबेस के लिए क्या करेंगे? मेरा मतलब है, प्रत्येक फ़ील्ड में एक हल करने की विधि है (getTitle, getAuthor ...) जो डेटाबेस के लिए अनुरोध करता है। क्या मैं यह गलत कर रहा हूँ?

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

उत्तर

4

resolve विधि article पारित हो जाता है, तो आप सिर्फ संपत्ति का उपयोग नहीं कर सकते हैं:

यह getAuthor (मैं नेवला का उपयोग करें) का एक उदाहरण है?

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

नेवला में Schema.methods के बाद से मॉडल पर को परिभाषित करता है तरीकों, यह एक आईडी लेख के लिए नहीं ले जाएगा (क्योंकि आप इसे एक लेख उदाहरण पर कहते हैं)। इसलिए, यदि आप विधि को रखना चाहते हैं, तो आप बस करेंगे:

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

यदि ऐसा कुछ था जिसे आपको देखने की आवश्यकता है उदा। एक और संग्रह में, फिर आपको एक अलग क्वेरी करने की आवश्यकता होगी (मान लीजिए कि आप रेफरी का उपयोग नहीं कर रहे हैं):

articleSchema.methods.getAuthor = function(callback) { 
    return this.model('Author').find({ _id: this.author_id }, cb); 
} 
संबंधित मुद्दे