Last active
June 14, 2020 17:33
-
-
Save Sceat/c16fbe164fb263f2553a0a0f67a11f02 to your computer and use it in GitHub Desktop.
Some extraction utility for Graphql.js
This file contains 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 { 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 } |
This file contains 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
{ | |
"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