首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在本地使用graphql和mongodb检索文档?

如何在本地使用graphql和mongodb检索文档?
EN

Stack Overflow用户
提问于 2018-02-01 19:46:23
回答 1查看 443关注 0票数 0

这里是我的server.js文件:

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
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;

我无法得到任何数据,总是返回为null在我的石墨库的查询:

代码语言:javascript
复制
{
  member(name: "Joe C.") {
    name
  }
}

结果:

代码语言:javascript
复制
{
  "data": {
    "member": null
  }
}

mongo中的 db.members.findOne显示:

代码语言:javascript
复制
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"
}

因此,文档存在于数据库的集合中。如何让graphql与mongodb对话以返回我正在寻找的内容?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-01 20:18:03

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

代码语言:javascript
复制
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!

或者简单地将模型命名为与集合相同的名称:

代码语言:javascript
复制
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
}));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48570601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档