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

Posted

技术标签:

【中文标题】如何在本地使用带有 express 的 graphql 和带有 mongoose 的 mongodb 来检索文档?【英文标题】:How do I use graphql with express and mongodb with mongoose locally to retrieve documents? 【发布时间】: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 对话以返回我要查找的内容?

【问题讨论】:

【参考方案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
));

【讨论】:

以上是关于如何在本地使用带有 express 的 graphql 和带有 mongoose 的 mongodb 来检索文档?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 plotly.graph_objects 绘制 3D 线?

如何在带有 nunjucks 的 Express 框架中使用 HTML - 没有玉

使用带有地图附件的 Facebook Open Graph Story (GeoPoint)

如何使用 Node、Express、Axios 在 ReactJS 前端设置带有 JWT 令牌的 cookie

如何在 Express 中使用带有 Firebase 功能的 webpack-hot-server-middleware

如何在本地存储中使用 JWT 来保持用户登录?