the first Kind field is the name of element - we don’t need that as we already have that previously in fieldName.

the arguments array describe the arguments of the query - we already have those in the second parameter of the resolve method (see previous post)

the selectionSet defines the attributes we may be interested in - can be used to define a projection for the fields we want from our backend. We see that we can easily extract ['id','name'] from
that structure and those are the fields we actually want to retrieve from user.

Query with an Inline fragment

We could also do a request using an inline fragment (doesn’t really make sense here as there is no ambiguity regarding the nature of the object returned, but still):

The good news is that we can parse the selectionSet of this object to get to the fields we need (['webUrl','rssUrl'])!

Code to parse

The code to parse the GraphQLResolveInfo to retrieve the list of fields that the request wants to fetch is finally relatively simple.

functionastValueFromName(ast){if(ast.name&&ast.name.kind&&ast.name.value){if(ast.name.kind==='Name'){returnast.name.value;}}thrownewError('unknown ast structure to extract name');}// this method will return the list of fields// from a selection setfunctionlistOfFieldsFromSelectionSet(selectionSet,fragments){returnselectionSet.selections.reduce((arr,selection)=>{if(selection.kind==='Field'){// we can directly get the field namearr.push(astValueFromName(selection));}elseif(selection.kind==='InlineFragment'){// we need to recurse into the definition of the inline fragmentarr=arr.concat(listOfFieldsFromSelectionSet(selection.selectionSet,fragments));}elseif(selection.kind==='FragmentSpread'){// let's find the ast for this spread fragmentvarspreadName=astValueFromName(selection);varastSpread=fragments[spreadName];if(!astSpread){thrownewError('can\'t find spread with name:'+spreadName);}// we need to recurse into the definition of the spread fragmentarr=arr.concat(listOfFieldsFromSelectionSet(astSpread.selectionSet,fragments));}returnarr;},[]);}

Implementation note

GraphQL being pretty young and still much in flux, it is important to check the expectation as much as possible in order to catch any change
in the ast structure as quickly as possible.

Conclusion

This GraphQLResolveInfo is complex but it contains all the information we can dream of in order to extract the intent of the query
and then to perform all sort of optimizations to query the actual data.