2017-02-07 74 views
0

當客戶端發送請求時,我想知道客戶端請求數據的字段是什麼。例如,在GraphQL/Sangria-Graphql中知道服務器端指定的字段

{ 
    user { 
    name 
    address 
    } 
} 

在上述請求,客戶端已經請求name字段和用戶的address字段。在執行查詢時,我如何知道/獲取這些指定的字段,即sangria-graphql Server中的名稱和地址?

回答

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 */ 
+0

這是graphql-JS吧?你能告訴我如何實現與sangria-graphql相同嗎?無論如何,我會嘗試用桑格利亞汽酒來解決它。謝謝。 –

+0

是的它是graphql-js請放一些'代碼' –

+1

好吧,我只是想通了。我修改了我的代碼到'resolve = c => {c.astFields.head.selections.map(fld => println(fld.renderCompact));'在QueryType字段中。謝謝。 –

相關問題