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

Posted

技术标签:

【中文标题】如何在我的 GraphQL 中为对象中的对象列表定义类型【英文标题】:How to define a type in my GraphQL for list of Objects in an object 【发布时间】:2020-09-29 10:32:28 【问题描述】:

我对 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;

【问题讨论】:

【参考方案1】:

我稍微更改了您的代码,这应该可以工作。变化是,1:topicCards 函数需要响应一个数组而不是一个字典。 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;

【讨论】:

是的,我会试试这个,这应该可以,如果我的客户端依赖于基于字典的数据,我应该只在客户端实现更改吗?基本上我正在做的是基于像“topic-1”这样的dict属性映射数据,所以我需要在我的数据中使用它。 是的,我完全理解。如果在客户端,您正在寻找基于 id 的对象,那么您必须在那里进行转换。但是,为此您可以使用单独的 api 根据 id 获取一个对象。 好的,但问题仍然不清楚,即使对于我字典中的一个对象,比如说 topic-1,我该如何发送它,就像我在 GraphQL 知识方面非常初学者一样,请帮助我:( 我已经用另一个函数更新了我的答案,该函数基于 id 作为参数获取单个对象。希望这会有所帮助

以上是关于如何在我的 GraphQL 中为对象中的对象列表定义类型的主要内容,如果未能解决你的问题,请参考以下文章

如何在我的 schema.graphql 文件中为按 id 过滤的查询引用生成的 Prisma 模式

应该如何对列表中的项目进行重新排序,并更新多个对象,如何使用 GraphQL 突变进行持久化?

在 AWS AppSync 中为 SQL 查询字符串化 JSON 对象

Firebase 数据库从子节点 B 中指定的子节点 A 检索对象

如何将对象列表或数组传递到旨在更新单个对象的 GraphQL 突变中?

Laravel 中的 Graphql 查询对象