[英]How to return an array of strings in GraphQL
So I'm trying to figure out how to return an array of strings in GraphQL. 因此,我试图找出如何在GraphQL中返回字符串数组。 Basically I have a to-do list with dependencies, so for each item there's a list of things that other item relies on by ID.
基本上,我有一个具有依赖性的待办事项列表,因此对于每个项目,都有一个列表,该列表列出了其他项目所依赖的ID。 I'm a little new to all this, but I've done a lot of searching on the GraphQL docs and on here and haven't found anything yet, so apologies in advance if this is a dumb question.
我对这一切有些陌生,但是我在GraphQL文档和此处进行了大量搜索,但还没有发现任何内容,因此如果这是一个愚蠢的问题,请提前道歉。 I'm using lodash in the resolve function.
我在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 });
}
}
}
})
GraphQL provides a class that makes a lists of a given object type new GraphQLList(objectType)
. GraphQL提供了一个类,使给定对象类型的列表成为
new GraphQLList(objectType)
。
You can use it like: 您可以像这样使用它:
const TodoType = new GraphQLObjectType({
name: 'Todo',
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
deadline: { type: GraphQLDateTime },
dependencies: { type: new GraphQLList(GraphQLString) }
})
});
Try this: 尝试这个:
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.
})
});
Hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.