我是graphql的新手,不知道如何在对象中定义我的对象列表的类型,我可以在互联网上找到关于数组中的对象列表的教程,而不是在对象中。我附上了我的数据,在这里我试图在Schema中写一个对象类型,但它似乎不起作用,如果有任何帮助,我将不胜感激,提前谢谢!
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser')
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var graphqlHTTP = require('express-graphql');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
const {buildSchema} = require('graphql');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/graphql', graphqlHTTP({
schema: buildSchema(`
type topicInfo{
id: String!
topicName: String!
topicDescription: String!
topicTags: [String!]!
}
type RootQuery {
topicCards: topicInfo
}
type RootMutation {
createEvent(name:String) : String
}
schema {
query: RootQuery
mutation: RootMutation
}
`),
rootValue: {
topicCards: () =>{
return (
{
"topic-1": {
id: "topic-1",
topicName: "Adding a feature: GSoC 1",
topicDescription:
"Some quick example text to build on the card title and make up the bulk of the card's content.",
topicTags: ["ReactJs", "NodeJS"],
},
"topic-2": {
id: "topic-2",
topicName: "Adding a feature: GSoC 2",
topicDescription:
"Some quick example text to build on the card title and make up the bulk of the card's content.",
topicTags: ["ReactJs", "NodeJS"],
},
"topic-3": {
id: "topic-3",
topicName: "Adding a feature: GSoC 3",
topicDescription:
"Some quick example text to build on the card title and make up the bulk of the card's content.",
topicTags: ["ReactJs", "NodeJS"],
},
"topic-4": {
id: "topic-4",
topicName: "Adding a feature: GSoC 4",
topicDescription:
"Some quick example text to build on the card title and make up the bulk of the card's content.",
topicTags: ["ReactJs", "NodeJS"],
},
"topic-5": {
id: "topic-5",
topicName: "Adding a feature: GSoC 5",
topicDescription:
"Some quick example text to build on the card title and make up the bulk of the card's content.",
topicTags: ["ReactJs", "NodeJS"],
},
"topic-6": {
id: "topic-6",
topicName: "Adding a feature: GSoC 6",
topicDescription:
"Some quick example text to build on the card title and make up the bulk of the card's content.",
topicTags: ["ReactJs", "NodeJS"],
},
"topic-7": {
id: "topic-7",
topicName: "Adding a feature: GSoC 7",
topicDescription:
"Some quick example text to build on the card title and make up the bulk of the card's content.",
topicTags: ["ReactJs", "NodeJS"],
},
"topic-8": {
id: "topic-8",
topicName: "Adding a feature: GSoC 8",
topicDescription:
"Some quick example text to build on the card title and make up the bulk of the card's content.",
topicTags: ["ReactJs", "NodeJS"],
},
"topic-9": {
id: "topic-9",
topicName: "Adding a feature: GSoC 9",
topicDescription:
"Some quick example text to build on the card title and make up the bulk of the card's content.",
topicTags: ["ReactJs", "NodeJS"],
},
"topic-10": {
id: "topic-10",
topicName: "Adding a feature: GSoC 10",
topicDescription:
"Some quick example text to build on the card title and make up the bulk of the card's content.",
topicTags: ["ReactJs", "NodeJS"],
},
}
)
}
},
graphiql:true
}));
module.exports = app;发布于 2020-06-10 14:52:59
我稍微修改了您的代码,这应该可以工作。更改如下: 1: topicCards函数需要响应数组而不是dict。2.在解析器中,您需要执行相同的操作。
app.use('/graphql', graphqlHTTP({
schema: buildSchema(`
type topicInfo{
id: String!
topicName: String!
topicDescription: String!
topicTags: [String!]!
}
type RootQuery {
topicCards: [topicInfo]
topicCardById (id: String!): topicInfo
}
type RootMutation {
createEvent(name:String) : String
}
schema {
query: RootQuery
mutation: RootMutation
}
`),
rootValue: {
topicCardById: (obj, args, context, info) => {
return ({
id: "topic-1",
topicName: "Adding a feature: GSoC 1",
topicDescription: "Some quick example text to build on the card title and make up the bulk of the card's content.",
topicTags: ["ReactJs", "NodeJS"],
});
}
topicCards: () =>{
return (
[
{
id: "topic-1",
topicName: "Adding a feature: GSoC 1",
topicDescription:
"Some quick example text to build on the card title and make up the bulk of the card's content.",
topicTags: ["ReactJs", "NodeJS"],
},
{
id: "topic-2",
topicName: "Adding a feature: GSoC 2",
topicDescription:
"Some quick example text to build on the card title and make up the bulk of the card's content.",
topicTags: ["ReactJs", "NodeJS"],
},
{
id: "topic-3",
topicName: "Adding a feature: GSoC 3",
topicDescription:
"Some quick example text to build on the card title and make up the bulk of the card's content.",
topicTags: ["ReactJs", "NodeJS"],
}
]
)
}
},
graphiql:true
}));
module.exports = app;https://stackoverflow.com/questions/62296302
复制相似问题