Q
Q
qfrontend2020-07-10 17:39:50
MongoDB
qfrontend, 2020-07-10 17:39:50

Why is there a connection to MongoDB, but when querying through GraphQL it returns null?

Greetings) I can’t understand what’s wrong .... Please help)
There is a connection with MongoDB ... But why does the request return null ?
I would be very grateful for any help)
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:[email protected]/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
5f087b5a3f680605463715.jpeg
GraphQL
5f087d081f006568286512.jpeg
Terminal
5f087d252a2c3407552517.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlekseyOgorodnikov, 2020-09-16
@AlekseyOgorodnikov

1. const {graphqlHTTP} = require('express-graphql') - you are importing the entire object.
2. "mongodb+ srv://new_0:[email protected]/base1 " remove query parameters (try without numbers in the name).
3. Probably you are not creating your database correctly, judging by the screenshot - an example of creating a collection:
{
"_id": {
"$oid": "5f623dc949f688c934d6e323"
},
"name": "Oleg",
"service": "Waiter",
"age": 23
}
This should solve your problem and you won't see null or [] in your queries anymore.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question