0
votes

What does my Resolver Query need to look like in order to return a list in Graphiql of the items in this MongoDB instance. This is the current resolver which logs all items to the console but does not show up in graphiql.

import { Biz } from './connectors';

const resolvers = {
  Query: {
    biz(_, { name, address }) {
      return Biz.find(function (err, biz) {
        if (err) return console.error(err);
        console.log(biz);
      });
    },
  },
};

export default resolvers;

The graphiql query and response are here:enter image description here

type definitions in schema.js

const typeDefinitions = `
type Biz {
 name: String
 address: String
}

type Query {
 biz(name: String, address: String): Biz
}

schema {
 query: Query
}
`;

export default [typeDefinitions]; 

This is the connector:

// businesses in mongo DB
    const MONGOLAB_URI = 'mongodb://user:[email protected]:88778/db';
    const mongo = Mongoose.connect(MONGOLAB_URI, function (err, res) {
     if (err) {
       console.log ('ERROR connecting to: ' + MONGOLAB_URI + '. ' + err);
     } else {
       console.log ( 'Succeeded connected to: ' + MONGOLAB_URI );
     }
    });

    const BizSchema = Mongoose.Schema({
     _id: Object,
     address: String,
     city: String,
     country: String,
     heading: String,
     headingid: Number,
     img_url: String,
     name: String,
     objectId: String,
     phonenumber1: String,
     website: String,
     latitude: Number,
     longitude: Number,
    });

    const Biz = Mongoose.model('bizs', BizSchema);

export { Biz };

sample object in db

{
               "_id": {
                   "$oid": "573e8c9b1379f0f2fad98290"
               },
               "accountid": 1404,
               "address": "737, Grand Rue,",
               "city": "Port-au-Prince",
               "country": "Haiti",
               "createdAt": "10/26/2015 7:27:42 PM",
               "heading": "Computer, Printers and Supplies",
               "headingid": 323,
               "img_url": "https://res.cloudinary.com/klik-io/image/upload/v1454850817/pages-jaunes-haiti-icon_sosoco.png",
               "name": "A & M Entreprises",
               "objectId": "0lw9lVl23j",
               "phonenumber1": "+509 3770 0303",
               "website": "http://868.ht"
            }

This is the server.js file.

import express from 'express';
import { apolloServer } from 'apollo-server';
import Schema from './src/data/schema';
// import Mocks from './data/mocks';
import Resolvers from './src/data/resolvers';

const GRAPHQL_PORT = 8080;

const graphQLServer = express();
graphQLServer.use('/graphql', apolloServer({
  graphiql: true,
  pretty: true,
  schema: Schema,
  resolvers: Resolvers
  // allowUndefinedInResolve: true,
  // printErrors: true,
  // mocks: Mocks,
}));
graphQLServer.listen(GRAPHQL_PORT, () => console.log(
  `GraphQL Server is now running on http://localhost:${GRAPHQL_PORT}/graphql`

));

1
I assume this is apollo server. If so, how are you invoking the express middleware? My guess is that the problem is there. - helfer
this example is trying to iterate on the the starter kit which i dont think is using the middleware. github.com/apollostack/apollo-starter-kit. I have not added middleware. Does trying to return a list require that the middle where be implemented? Is that an async thing? - armand
asked differently: did you modify server.js at all? That's where the server is defined, including the apolloServer middleware. - helfer
I disabled the mocks in order to use the mlab mongodb instance. commented out mocks import and mocks: Mocks - armand
@helfer i updated the question and provided more info. I hope its more clear. Thanks - armand

1 Answers

0
votes

I resolved the issue in the root query. I noted that posts in the docs returned a post array, [Post] and applied that concept.

In schema.js, I changed Query to an array with [Biz]:

type Query {
  biz(name: String, address: String): [Biz]
}

then in resolvers.js I changed the resolvers.Query return statement syntax to the following:

Query: {
        biz(_, {name, address}) {
            return Biz.find();
        }
    }