如何在我的GraphQL中为对象中的对象列表定义类型
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在我的GraphQL中为对象中的对象列表定义类型相关的知识,希望对你有一定的参考价值。
我对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;
我对graphql还是很陌生,不知道如何为对象中的对象列表定义类型,我可以在网上找到有关数组而不是对象中对象列表的教程。我正在附加数据,...
答案
我已经稍微更改了您的代码,这应该可以。更改为:1:topicCards函数需要响应数组而不是字典。 2.在解析器中,您需要执行相同的操作。
以上是关于如何在我的GraphQL中为对象中的对象列表定义类型的主要内容,如果未能解决你的问题,请参考以下文章
如何在我的 schema.graphql 文件中为按 id 过滤的查询引用生成的 Prisma 模式
应该如何对列表中的项目进行重新排序,并更新多个对象,如何使用 GraphQL 突变进行持久化?