【问题标题】:How to define an Array datatype for GraphQL field in Type definition如何在类型定义中为 GraphQL 字段定义数组数据类型
【发布时间】:2019-05-30 16:13:50
【问题描述】:

我正在开发一个 Nodejs 应用程序,我正在尝试为 graphQL 定义以下类型,但我无法弄清楚如何使用 Javascript 数组类型定义字段。

const graphql = require('graphql');
const {
  GraphQLObjectType,
  GraphQLString,
  GraphQLID
 } = graphql;
 const { Question } = require('../../../../models');

const QuestionChoiceType = new GraphQLObjectType({
  name: 'QuestionChoiceType',
  fields: () => ({
    id: { type: GraphQLID },
    correctChoice: { type: GraphQLString },
    choices: [{ type: GraphQLString }]
  })
});

module.exports = QuestionChoiceType;

【问题讨论】:

标签: javascript node.js graphql


【解决方案1】:

使用GraphQLList 定义一个列表/数组:

const QuestionChoiceType = new GraphQLObjectType({
  name: 'QuestionChoiceType',
  fields: () => ({
    id: { type: GraphQLID },
    correctChoice: { type: GraphQLString },
    choices: { type: new GraphQLList(GraphQLString)}
  })
});

【讨论】:

    猜你喜欢
    • 2021-12-23
    • 2017-04-13
    • 2017-10-20
    • 2019-06-24
    • 2021-11-20
    • 2016-03-04
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    相关资源
    最近更新 更多