【问题标题】:How do I use graphql with express and mongodb with mongoose locally to retrieve documents?如何在本地使用带有 express 的 graphql 和带有 mongoose 的 mongodb 来检索文档?
【发布时间】:2018-07-12 05:47:53
【问题描述】:

这是我的 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 对话以返回我要查找的内容?

【问题讨论】:

    标签: node.js mongodb express mongoose graphql


    【解决方案1】:

    在猫鼬模式定义中,添加集合名称作为第二个参数:

    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
    }));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-04
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      • 2020-09-03
      • 2014-09-15
      • 1970-01-01
      • 2015-07-18
      相关资源
      最近更新 更多