Skip to content

Instantly share code, notes, and snippets.

@Sceat
Last active June 14, 2020 17:33
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 Sceat/c16fbe164fb263f2553a0a0f67a11f02 to your computer and use it in GitHub Desktop.
Save Sceat/c16fbe164fb263f2553a0a0f67a11f02 to your computer and use it in GitHub Desktop.
Some extraction utility for Graphql.js
import { isListType } from 'graphql/type/definition.mjs'
const extract_is_list = type =>
isListType(type) ||
isListType(type?.ofType)
const extract_type = type => type.ofType
? extract_type(type.ofType)
: type
const node_selections = node => node
?.selectionSet
?.selections
?? []
const extract_fields = infos => node_selections(infos?.fieldNodes?.[0])
.flatMap(({ kind, name: { value } }) => kind === 'FragmentSpread'
? node_selections(infos?.fragments?.[value])
: value)
export { extract_is_list, extract_type, extract_fields }
{
"name": "graphql-extract",
"version": "0.1.0",
"type": "module",
"main": "graphql-extract.js"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment