简体   繁体   中英

How to define an Array datatype for GraphQL field in Type definition

I'm working on a Nodejs application and I'm trying to define the type below for graphQL but I cannot figure out how to define a field with a Javascript array type.

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;

Use GraphQLList to define a list / array :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM