基本的graphQL Mongoose设置

时间:2016-06-10 17:21:49

标签: mongodb mongoose graphql mongoose-schema graphql-js

我已关注http://docs.apollostack.com/apollo-server/guide.html /和此video,并希望尝试将其连接到www.mlab.com上的数据库。该项目正在连接到mlab和graphiql,但是当我在graphiql中运行查询时,我的查询得到了null / undefined值。

这是schema.js

const typeDefinitions = `
type Biz {
 name: String
 address: String
}

type Query {
 biz(name: String, address: String): Biz
}

schema {
 query: Query
}
`;

export default [typeDefinitions]; 

这是connectors.js:

// businesses in mongo DB
    const MONGOLAB_URI = 'mongodb://user:password@ds064278.mlab.com:64278/klik-mongodb';
    const mongo = Mongoose.connect(MONGOLAB_URI, function (err, res) {
     if (err) {
       console.log ('ERROR connecting to: ' + MONGOLAB_URI + '. ' + err);
     } else {
       console.log ( 'Succeeded connected to: ' + MONGOLAB_URI );
     }
    });

    const BizSchema = Mongoose.Schema({
     _id: Object,
     address: String,
     city: String,
     country: String,
     heading: String,
     headingid: Number,
     img_url: String,
     name: String,
     objectId: String,
     phonenumber1: String,
     website: String,
     latitude: Number,
     longitude: Number,
    });

    const Biz = Mongoose.model('bizs', BizSchema);

    export { Biz };

这是resolvers.js:

import { Biz } from './connectors';

    const resolvers = {
     Query: {
       biz(_, { name, address }) {
         return Biz.find({ where: name, address });
       },
     },
    };

    export default resolvers;   

这是我的mlab对象的一个​​例子:

{
               "_id": {
                   "$oid": "573e8c9b1379f0f2fad98290"
               },
               "accountid": 1404,
               "address": "737, Grand Rue,",
               "city": "Port-au-Prince",
               "country": "Haiti",
               "createdAt": "10/26/2015 7:27:42 PM",
               "heading": "Computer, Printers and Supplies",
               "headingid": 323,
               "img_url": "https://res.cloudinary.com/klik-io/image/upload/v1454850817/pages-jaunes-haiti-icon_sosoco.png",
               "name": "A & M Entreprises",
               "objectId": "0lw9lVl23j",
               "phonenumber1": "+509 3770 0303",
               "website": "http://868.ht"
            }

这是graphiql输出: enter image description here

1 个答案:

答案 0 :(得分:2)

看起来您的商家解析器使用add_word = ("INSERT INTO {table} " "(name, surname) " "VALUES (%s, %s)") atable = 'second' data_word = (name1, surname1) cursor.execute(add_word.format(table=atable), data_word) ,它将返回一个列表。你想要的是find()

PS:您通常可以通过在相应的解析函数中添加print语句(或运行节点调试器)来调试此类问题。