Created
May 22, 2020 21:27
-
-
Save giacomorebonato/1d2670e0374e48219ea3e2c24038a768 to your computer and use it in GitHub Desktop.
GraphQL utilities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { FieldNode } from 'graphql' | |
export const doesPathExists = ( | |
nodes: readonly FieldNode[], | |
path: string[] | |
): boolean => { | |
if (!nodes) { | |
return false | |
} | |
const node = nodes.find((x) => x.name.value === path[0]) | |
if (!node) return false | |
if (path.length === 1) return true | |
if (!node.selectionSet) return false | |
return doesPathExists( | |
node.selectionSet.selections as FieldNode[], | |
path.slice(1) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment