简体   繁体   English

如何在本地将expressq的graphql和mongoose的mongodb一起使用来检索文档?

[英]How do I use graphql with express and mongodb with mongoose locally to retrieve documents?

Here is my server.js file: 这是我的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');
});

Here is my schema.js file: 这是我的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;

I can not get any data back, always comes back at null in my graphiql the query: 我无法获取任何数据,在我的graphiql查询中总是返回null

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

The result: 结果:

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

In the mongo shell db.members.findOne shows: 在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"
}

So the document exists in the collection of the db. 因此该文档存在于db的集合中。 How do I get graphql to speak with mongodb to return what i am looking for? 我如何让graphql与mongodb对话以返回我正在寻找的东西?

in the mongoose schema def, add the collection name as as 2nd parameter: 在猫鼬模式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!

Or simply name the model the same name as the collection: 或简单地将模型命名为与集合相同的名称:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何不使用sort检索mongodb-mongoose中最新添加的文档? - How can I retrieve the most recently added documents in mongodb-mongoose without using sort? MongoDB-Mongoose:如何使用Mongoose同时更新两个单独的嵌入式文档? - MongoDB - Mongoose: How Do I Update Simultaneously Two Separate Embedded Documents With Mongoose? 使用Express,MongoDB和Mongoose在本地进行开发 - Developing locally using express, mongodb and mongoose 如何在本地连接mongoose到mongodb - How to connect mongoose to mongodb locally 如何使用Mongoose find()方法在Mongodb数据库中找到与某些URI参数匹配的所有文档? - How do I find all the documents in a Mongodb database that match a certain URI params with the Mongoose find() method? 如何使用express / mongoose和客户端JS将HTML类发布到mongoDB集合中? - How do I post an HTML class into a mongoDB collection using express/mongoose and client-side JS? 如何使用Express JS和Mongoose和PUT方法更新MongoDB数据库? - How do I update MongoDB database, using Express JS and Mongoose with PUT method? 如何计算 mongodb/express 中的文档数 - How to count documents in mongodb/express 如何在GraphQL和DataLoader中使用Mongoose? - How to use Mongoose with GraphQL and DataLoader? 如何使用 graphql 和 mongoose 进行突变 - How to do a mutation with graphql and mongoose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM