首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在我的GraphQL中为对象中的对象列表定义类型

如何在我的GraphQL中为对象中的对象列表定义类型
EN

Stack Overflow用户
提问于 2020-06-10 13:11:39
回答 1查看 25关注 0票数 0

我是graphql的新手,不知道如何在对象中定义我的对象列表的类型,我可以在互联网上找到关于数组中的对象列表的教程,而不是在对象中。我附上了我的数据,在这里我试图在Schema中写一个对象类型,但它似乎不起作用,如果有任何帮助,我将不胜感激,提前谢谢!

代码语言:javascript
复制
    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;
EN

回答 1

Stack Overflow用户

发布于 2020-06-10 14:52:59

我稍微修改了您的代码,这应该可以工作。更改如下: 1: topicCards函数需要响应数组而不是dict。2.在解析器中,您需要执行相同的操作。

代码语言:javascript
复制
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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62296302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档