How to fetch mongodb result in graphql

后端 未结 2 993
误落风尘
误落风尘 2020-12-12 07:06
const axios = require(\'axios\');
const mongodb = require(\'mongodb\');

const MongoClient = mongodb.MongoClient;
const url = \"mongodb://localhost:27017/graphql\";
         


        
2条回答
  •  被撕碎了的回忆
    2020-12-12 07:44

    here how you connect express-graphql

    const express = require('express');
    const expressGraphQL = require('express-graphql');
    const schema = require('./schema');
    
    const app = express();
    
    
    app.use(
       '/graphql',
       expressGraphQL({
         schema,
         graphiql: true
        })
    );
    

    you return the connection in the resolve correct it to

     resolve(parent, args) {
             MongoClient.connect(url, (err, client) => {
                 return  client.db('graphql').collection('users').find()
             });
     }
    

提交回复
热议问题