如何在类型定义中为GraphQL字段定义数组数据类型

时间:2019-01-04 04:12:29

标签: javascript node.js graphql

我正在开发Node.js应用程序,并且试图为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;

1 个答案:

答案 0 :(得分:0)

使用GraphQLList定义列表/数组:

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