Skip to content

Instantly share code, notes, and snippets.

@rodrigopr
Last active January 18, 2016 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodrigopr/2ec8053e781ab21be675 to your computer and use it in GitHub Desktop.
Save rodrigopr/2ec8053e781ab21be675 to your computer and use it in GitHub Desktop.
function getFieldList(context, asts = context.fieldASTs) {
//for recursion...Fragments doesn't have many sets...
if (!Array.isArray(asts)) asts = [asts]
//get all selectionSets
var selections = asts.reduce((selections, source) => {
if(source.selectionSet) {
selections.push(...source.selectionSet.selections);
}
return selections;
}, []);
//return fields
return selections.reduce((list, ast) => {
switch (ast.kind) {
case 'Field' :
if (ast.selectionSet) {
list[ast.name.value] = getFieldList(context, ast);
} else {
list[ast.name.value] = true
}
return list;
case 'InlineFragment':
return {
...list,
...getFieldList(context, ast)
};
case 'FragmentSpread':
return {
...list,
...getFieldList(context, context.fragments[ast.name.value])
};
default:
throw new Error('Unsuported query selection')
}
}, {})
}
{
edges: {
node: {
id: true,
nome: true,
idUsuario: true
},
edgeInfo: {
status: true,
data: true
}
},
pageInfo: {
hasNextPage: true,
endCursor: true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment