当客户端发送请求时,我想知道客户端请求数据的字段是什么。例如,
{
user {
name
address
}
}
在上述请求中,客户端请求了用户的name
字段和address
字段。在执行查询时,我如何知道/获取这些指定字段,即 sangria-graphql 服务器中的名称和地址?
你必须使用4th
参数resolve()
resolve: (obj, args, auth, fieldASTs) => {
/* all your fields are present in selections
please check the fieldASTs JSON path it may vary if you are using relay
connections */
const requiredFields = fieldASTs.fieldNodes[0].selectionSet.selections.map(
(set) => set.name.value
);
// requiredFields will contain the name and address
}
/* fieldASTs contains fieldNames, fieldNodes and every details about your Schema
you can get your specified fields inside fieldNodes like */