例如我有连接类型:
let usersType = new GraphQLObjectType({
name: 'Users',
description: 'users array',
fields: () => ({
array: {
type: userConnection,
description: 'all users',
args: connectionArgs,
searchFor: {
type: GraphQLString
},
resolve: (root, args) => {
return connectionFromArray(get(), args);
}
}
})
});
在这种情况下,在查询中我只能指定(first,last,after,before)参数,但如果我需要传递一些额外的参数如userName等,那该怎么办呢?
基本上我需要这样的东西:
query {
array (first: 1, userName: "name")
}
在用户类型中我可以处理如下的请求:
resolve: (root, args) => connectionFromArray(get(args.userName), args.args)
答案 0 :(得分:5)
是的,有可能,您只需要使用新参数扩展中继助手connectionArgs
,如下所示:
args: {
...connectionArgs,
searchFor: { type: GraphQLString }
}
然后在resolve
函数中访问它:
resolve: (root, args) => {
// if the field argument 'searchFor' exists
if (args.searchFor) {
...
}
...
}