Приветствую) Ни как не могу понять в чём дело....
Помогите плиз)
Связь с MongoDB есть... Но
почему при запросе возвращается null ?
Буду очень благодарна за любую помощь)
app.js
const express = require('express');
const graphqlHTTP = require('express-graphql');
const schema = require('../schema/schema');
const mongoose = require('mongoose');
const app = express();
const PORT = 8000;
mongoose.connect("mongodb+srv://new_0:pass123@cluster0.vpnlp.mongodb.net/base1?retryWrites=true&w=majority", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
app.use('/graphql', graphqlHTTP({
schema,
graphiql: true,
}))
const dbConnection = mongoose.connection;
dbConnection.on('error', err => console.log(`Connection error ${err}`));
dbConnection.once('open', () => console.log('Connected to DB !'))
app.listen(PORT, (err) => {
err ? console.log(error) : console.log('Server STARTED');
});
schema.js
const garphql = require('graphql');
const {GraphQLObjectType, GraphQLString, GraphQLID, GraphQLSchema, GraphQLList} = garphql;
const Workers = require('../models/worker');
const Works = require('../models/work');
const WorkTupe = new GraphQLObjectType({
name: 'Work',
fields: () => ({
id: {type: GraphQLID},
name: {type: GraphQLString},
description: {type: GraphQLString},
authorID: {
type: WorkerTupe,
resolve(parent, args){
return Workers.findById(parent.authorID);
}
},
}),
});
const WorkerTupe = new GraphQLObjectType({
name: 'Worker',
fields: () => ({
id: {type: GraphQLID},
name: {type: GraphQLString},
services: {type: GraphQLString},
works: {
type: GraphQLList(WorkTupe),
resolve(parent, args){
return Works.find({authorID: parent.id});
},
},
}),
});
const Query = new GraphQLObjectType({
name: "Query",
fields: {
worker: {
type: WorkerTupe,
args: {
id: { type: GraphQLID }
},
resolve: (parent, args) => {
return Workers.findById(args.id);
},
},
work: {
type: WorkTupe,
args: {
id: { type: GraphQLID }
},
resolve: (parent, args) => {
return Works.findById(args.id)
},
},
},
});
module.exports = new GraphQLSchema({
query: Query,
});
worker.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const workerSchema = new Schema({
name: String,
services: String
})
module.exports = mongoose.model('Worker', workerSchema);
work.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const workSchema = new Schema({
name: String,
description: String
})
module.exports = mongoose.model('Work', workSchema);
MongoDB
GraphQL
Terminal