2016-12-28 5 views
0

私は既存のアプリケーションにgraphQLを追加しようとしています。 私は現在、psql db呼び出しでExpressエンドポイントを取り戻しています。 私は自分のデータにアクセスするためにpsqlを使用し、次にこれらのクエリの結果をgraphQLの '解決'に使用することを目指します。ここでgraphqlのpsqlから返されたデータを解決する

は、私はpsqlデシベルコールの例である:ここで

'use strict'; 

const config = require('../../config'); 
const PostgresDAO = require('core/src/server/db/dao/postgres'); 
const postgresRW = new PostgresDAO(config.postgresRW); 

function getById(id) { 
    postgresRW.queryOne(
    ` 
     SELECT id, email, create_date 
     FROM gamesDB.players 
     WHERE id = $1; 
    `, 
    [id], 
    function(err, result) { 
     console.log(result); 
     return result; 
    } 
); 
} 

module.exports = { 
    getById: getById 
} 

は私のgraphQLスキーマです:

'use strict'; 

const graphql = require('graphql'); 
const Player = require('./types/player'); 
const db = require('../db'); 

const RootQueryType = new graphql.GraphQLObjectType({ 
    name: 'RootQueryType', 
    fields: { 
    player: { 
     type: Player, 
     description: 'The current player identified by an ID.', 
     args: { 
     key: { 
      type: new graphql.GraphQLNonNull(graphql.GraphQLString) 
     } 
     }, 
     resolve: (obj, args) => { 
     return db.players.getById(args.key); 
     } 
    } 
    } 
}); 

const testSchema = new graphql.GraphQLSchema({ 
    query: RootQueryType 
}); 

module.exports = testSchema; 

問題は、私はからプレイヤーを照会するたびとしての私の決意にあるように思われますgraphiqlインターフェイス内では、私のサーバーに正しいプレーヤーの情報が正しく記録されていますが、graphiqlインターフェイスの結果はnullです。 ここで間違っていることは何ですか?

+0

そのタイプ 'PostgresDAO'は本当に何ですか?たぶん、あなたはおそらく約束を返すように、 'queryOne'メソッドを使用していませんか? –

答えて

2

Player.getByIdは、コールバックの結果を含む約束を返す必要があります。

(完全にテストされていないコード)だから、おそらく:

function getById(id) { 
    return new Promise(function(resolve, reject) { 
    postgresRW.queryOne(
     ` 
     SELECT id, email, create_date 
     FROM gamesDB.players 
     WHERE id = $1; 
     `, 
     [id], 
     function(err, result) { 
     if (err) reject(err); 
     else resolve(result); 
     } 
    ); 
    }); 
} 
関連する問題