Я подключаю GraphQL к MongoDB, но результат равен null
. Помоги мне !
Файл index.js:
const { graphql } = require('graphql');
const { MongoClient } = require('mongodb');
const assert = require('assert');
const readline = require('readline');
const mySchema = require('./schema/main.js');
const rli = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const MONGO_URL = 'mongodb://127.0.0.1:27017/test';
MongoClient.connect(MONGO_URL, { useNewUrlParser: true }, (err, db) => {
assert.equal(null, err);
console.log('Connected to MongoDB server');
rli.question('Client Request: ', inputQuery => {
graphql(mySchema, inputQuery, {}, { db }).then(result => {
console.log('Server Answer: ', result.data);
db.close(() => rli.close());
});
});
});
Файл main.js:
const {
GraphQLSchema,
GraphQLObjectType,
GraphQLInt
} = require('graphql');
const queryType = new GraphQLObjectType({
name: 'RootQuery',
fields: {
usersCount: {
type: GraphQLInt,
resolve: (_, args, { db }) => db.collection('users').count()
}
}
});
const mySchema = new GraphQLSchema({
query: queryType
});
module.exports = mySchema;
И это результат:
Connected to MongoDB server
Client Request: { usersCount }
Server Answer: { usersCount: null }