0

当客户端发送请求时,我想知道客户端请求数据的字段是什么。例如,

{
  user {
    name
    address
  }
}

在上述请求中,客户端请求了用户的name字段和address字段。在执行查询时,我如何知道/获取这些指定字段,即 sangria-graphql 服务器中的名称和地址?

4

1 回答 1

2

你必须使用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 */
于 2017-02-07T11:26:48.723 回答