如何在GraphQL中返回字符串数组

时间:2018-07-10 15:45:22

标签: javascript node.js lodash graphql

因此,我试图弄清楚如何在GraphQL中返回字符串数组。基本上,我有一个具有依赖性的待办事项列表,因此对于每个项目,都有一个列表,该列表列出了其他项目所依赖的ID。我对这一切有些陌生,但是我在GraphQL文档和此处进行了大量搜索,但还没有发现任何内容,因此如果这是一个愚蠢的问题,请提前道歉。我在resolve函数中使用lodash。

// dummy data
var todos = [
  { name: 'Repair Driveway', dependencies: ['2' ,'3'], id: '1' },
  { name: 'Research driveway repair', dependencies: [], id: '2' },
  { name: 'Buy driveway repair tools', dependencies: ['2'], id: '3' },
  { name: 'Get groceries', dependencies: [], id: '4'}
];

const TodoType = new GraphQLObjectType({
  name: 'Todo',
  fields: () => ({
    id: { type: GraphQLID },
    name: { type: GraphQLString },
    deadline: { type: GraphQLDateTime },
    dependencies: { type:   } // This is where I'm not sure what to do.
  })
});

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    todo: {
      type: TodoType,
      args: { id: { type: GraphQLID } },
      resolve(parent, args){
        console.log("Queried:" );
        return _.find(todos, { id: args.id });
      }
    }
  }
})

2 个答案:

答案 0 :(得分:1)

GraphQL提供了一个类,该类列出了给定对象类型new GraphQLList(objectType)的列表。

您可以像这样使用它:

const TodoType = new GraphQLObjectType({
  name: 'Todo',
  fields: () => ({
    id: { type: GraphQLID },
    name: { type: GraphQLString },
    deadline: { type: GraphQLDateTime },
    dependencies: { type: new GraphQLList(GraphQLString)  }
  })
});

答案 1 :(得分:0)

尝试一下:

ContainerB

希望有帮助

相关问题