0

所以我试图弄清楚如何在 GraphQL 中返回一个字符串数组。基本上我有一个包含依赖项的待办事项列表,因此对于每个项目都有一个其他项目依赖 ID 的事物列表。我对这一切有点陌生,但我已经在 GraphQL 文档和此处进行了大量搜索,但还没有找到任何东西,所以如果这是一个愚蠢的问题,请提前道歉。我在解析函数中使用了 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 });
      }
    }
  }
})
4

2 回答 2

3

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)  }
  })
});
于 2018-07-10T18:55:05.540 回答
0

尝试这个:

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

希望能帮助到你

于 2018-07-10T16:27:49.797 回答