为了在Graphiql中返回此MongoDB实例中项目的列表,我的解析器查询需要是什么样子? 这是当前解析程序,它将所有项目记录到控制台,但不会显示在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;
schema.js中的类型定义
const typeDefinitions = `
type Biz {
name: String
address: String
}
type Query {
biz(name: String, address: String): Biz
}
schema {
query: Query
}
`;
export default [typeDefinitions];
这是连接器:
// businesses in mongo DB
const MONGOLAB_URI = 'mongodb://user:password@ds888878.mlab.com: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 };
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"
}
这是server.js文件。
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`
));
答案 0 :(得分:0)
我在根查询中解决了这个问题。我注意到docs中的帖子返回了一个帖子数组,[Post]并应用了这个概念。
在schema.js中,我使用[Biz]:
将Query更改为数组type Query {
biz(name: String, address: String): [Biz]
}
然后在resolvers.js中我将resolvers.Query return语句更改为以下内容:
Query: {
biz(_, {name, address}) {
return Biz.find();
}
}