如何在本地使用带有mongoose的express和mongodb的graphql来检索文档?

时间:2018-02-01 19:46:24

标签: node.js mongodb express mongoose graphql

这是我的server.js文件:

const express = require('express');
const graphqlHTTP = require('express-graphql');
const schema = require('./graphQL-schema/schema');

const app = express();

app.use('/', graphqlHTTP({
  schema,
  pretty: true,
  graphiql: true
}));


app.listen(3000, () => {
  console.log('Listening on port 3000');
});

这是我的schema.js文件:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const graphql = require('graphql');
var GraphQLObjectType = graphql.GraphQLObjectType;
var GraphQLBoolean = graphql.GraphQLBoolean;
var GraphQLID = graphql.GraphQLID;
var GraphQLString = graphql.GraphQLString;
var GraphQLList = graphql.GraphQLList;
var GraphQLNonNull = graphql.GraphQLNonNull;
var GraphQLSchema = graphql.GraphQLSchema;


// const memberModel = require('./../models/member-model');

const mongoDbUrl = 'mongodb://127.0.0.1/memberdb';

mongoose.Promise = global.Promise;

mongoose.connect(mongoDbUrl, (error) => {
  if (error) console.error(error)
  else console.log('mongo connected')
});


const MemberModelSchema = new Schema({
  coverImg: String,
  profileImg: String,
  name: String,
  bowModel: String,
  otherInterests: String,
  location: String,
  responseFrequency: String,
  memberType: String,
  bio: String,
  otherInterests: String
});

const memberModel = mongoose.model('MemberModel', MemberModelSchema);

const promiseFindOneMember = () => {
  return new Promise( (resolve, reject) => {
    memberModel.findOne((err, result) => {
      if (err) {
        console.log('err: ', err);
        reject(err)
      }
      else {
        resolve(result);
        console.log('member: ', result);
      }
    });
  });
};

const promiseFindOneMemberName = (name) => {
  return new Promise( (resolve, reject) => {
    console.log('name: ', name);
    memberModel.find({name: name}, (err, member) => {
      if (err) {
        console.log('err: ', err);
        reject(err)
      }
      else {
        console.log('member: ', member);
        resolve(member);
      }
    });
  });
};

const MemberProfileCardType = new GraphQLObjectType({
  name: 'MemberProfile',
  fields: () => ({
    id: { type: GraphQLString },
    coverImg: { type: GraphQLString },
    profileImg: { type: GraphQLString },
    name: { type: GraphQLString },
    otherInterests: { type: GraphQLString },
    location: { type: GraphQLString },
    bowModel: { type: GraphQLString },
    responseFrequency: { type: GraphQLString },
  })
});

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: () => ({
    member: {
      type: MemberProfileCardType,
      args: { name: { type: GraphQLString } },
      resolve(parentValue, args) {
        return promiseFindOneMember();
        // return promiseFindOneMemberName(args.name);
      }
    }
  })
});

const schema = new GraphQLSchema({
  query: RootQuery
});

module.exports = schema;

我无法获取任何数据,在我的graphiql 中总是返回null 查询:

{
  member(name: "Joe C.") {
    name
  }
}

结果:

{
  "data": {
    "member": null
  }
}

在mongo shell中,db.members.findOne显示:

db.members.findOne()
{
    "_id" : ObjectId("5a7366e01232142dd39d247e"),
    "coverImg" : "https://path-to-file",
    "profileImg" : "http://path-to-file",
    "name" : "Joe C.",
    "bowModel" : "Test",
    "otherInterests" : "Coding, Designing, Camping",
    "location" : "City, State",
    "responseFrequency" : "weekly"
}

因此该文档存在于db的集合中。如何让graphql与mongodb对话以返回我想要的内容?

1 个答案:

答案 0 :(得分:0)

在mongoose schema def中,添加集合名称作为第二个参数:

const MemberModelSchema = new Schema({
  coverImg: String,
  profileImg: String,
  name: String,
  bowModel: String,
  otherInterests: String,
  location: String,
  responseFrequency: String,
  memberType: String,
  bio: String,
  otherInterests: String
}, {collection: 'members'}); // < -- right here yo!

或者只是将模型命名为与集合相同的名称:

const memberModel = mongoose.model('members', new Schema({
  coverImg: String,
  profileImg: String,
  name: String,
  bowModel: String,
  otherInterests: String,
  location: String,
  responseFrequency: String,
  memberType: String,
  bio: String,
  otherInterests: String
}));