UNPKG

react-apollo-testing-mock-gen

Version:
42 lines (31 loc) 1.27 kB
const { getImportPaths, fileFolderPath, processPathName } = require('../path-parser'); const { getFileSource } = require('../file-parser'); const { buildSelectionStructure } = require('../query-parser'); const { resolveTypename } = require('../type-resolver'); const getFragmentDefinitions = (definitions) => { const fragmentDefinitions = definitions.filter((definition) => definition.kind === 'FragmentDefinition'); const fragments = {}; fragmentDefinitions.forEach((definition) => { fragments[definition.name.value] = { ...buildSelectionStructure(definition.selectionSet, fragments), __typename: resolveTypename(definition.typeCondition) }; }); return fragments; }; const getImportedFragments = (fragmentPaths) => { let paths = {}; fragmentPaths.forEach((fragmentPath) => { const { sourceJson } = getFileSource(fragmentPath); paths = { ...paths, ...getFragmentDefinitions(sourceJson.definitions) }; }); return paths; }; const buildFragmentStuctures = (data, sourceJson) => { const fragmentPaths = getImportPaths(data, processPathName, fileFolderPath); return { ...getFragmentDefinitions(sourceJson.definitions), ...getImportedFragments(fragmentPaths) }; } module.exports = buildFragmentStuctures;