在解析器中,我需要检测参数的类型 . 我可以有一个深层嵌套的参数对象,它是多种输入类型的组合 . 像这样:
# where is of type DocumentWhereInput
# and nested objects in OR operator are DocumentWhereInput types too
documents(where: {OR: [{id_gt: 0}, {id_not: 1}]}, first: 2) {
id
}
}
我尝试过使用TypeInfo:
const typeInfo = new TypeInfo(info.schema);
const filteredSelections = visit(info.operation, {
leave: (node, key, parent, path, ancestors) => typeInfo.leave(node),
enter: (node, key, parent, path, ancestors) => {
typeInfo.enter(node);
const type = typeInfo.getInputType();
}});
但是这个:
-
匹配操作中的所有输入,不仅适用于给定的解析器(可以过滤)
-
不适用于变量,仅适用于内联参数
我可以使用 graphiql
中的函数获取变量类型,如下所示:
export function collectVariables(schema, operationDefinition) {
const variableToType = Object.create(null);
const variableDefinitions = operationDefinition.variableDefinitions;
if (variableDefinitions) {
variableDefinitions.forEach(({variable, type}) => {
const inputType = typeFromAST(schema, type);
if (inputType) {
variableToType[variable.name.value] = inputType;
}
});
}
return variableToType;
}
collectVariables(info.schema, info.operation)
但是这只给出了变量的类型,而不是它的子类型 .
所以
解析器函数接受args作为扩展对象,并将内联参数和变量组合在一起 . 我怎样才能检测出这个args包含的类型?或者对于整个操作,但包括变量?这是可能的,graphql在内部进行验证,graphiql也做到了,但我无法弄清楚如何 .
例如,在开头查询我想要这样的东西:
{ where: DocumentWhereInput { OR: [ DocumentWhereInput, DocumentWhereInput ] }, first: Int }
或者将类型映射到值的任何其他结构