graphql-fields
Advanced tools
Comparing version 1.2.0 to 1.2.1
'use strict'; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | ||
@@ -8,64 +8,71 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var options = {}; | ||
function getSelections(ast) { | ||
if (ast && ast.selectionSet && ast.selectionSet.selections && ast.selectionSet.selections.length) { | ||
return ast.selectionSet.selections; | ||
} | ||
if (ast && ast.selectionSet && ast.selectionSet.selections && ast.selectionSet.selections.length) { | ||
return ast.selectionSet.selections; | ||
} | ||
return []; | ||
return []; | ||
} | ||
function isFragment(ast) { | ||
return ast.kind === 'InlineFragment' || ast.kind === 'FragmentSpread'; | ||
return ast.kind === 'InlineFragment' || ast.kind === 'FragmentSpread'; | ||
} | ||
function getAST(ast, info) { | ||
if (ast.kind === 'FragmentSpread') { | ||
var fragmentName = ast.name.value; | ||
return info.fragments[fragmentName]; | ||
} | ||
return ast; | ||
if (ast.kind === 'FragmentSpread') { | ||
var fragmentName = ast.name.value; | ||
return info.fragments[fragmentName]; | ||
} | ||
return ast; | ||
} | ||
function getArguments(ast) { | ||
return ast.arguments.map(function (argument) { | ||
return _defineProperty({}, argument.name.value, { | ||
kind: argument.value.kind, | ||
value: argument.value.value | ||
}); | ||
return ast.arguments.map(function (argument) { | ||
return _defineProperty({}, argument.name.value, { | ||
kind: argument.value.kind, | ||
value: argument.value.value | ||
}); | ||
}); | ||
} | ||
function flattenAST(ast, info, obj) { | ||
obj = obj || {}; | ||
return getSelections(ast).reduce(function (flattened, a) { | ||
if (isFragment(a)) { | ||
flattened = flattenAST(getAST(a, info), info, flattened); | ||
} else { | ||
var name = a.name.value; | ||
if (flattened[name] && flattened[name] !== '__arguments') { | ||
_extends(flattened[name], flattenAST(a, info, flattened[name])); | ||
} else { | ||
flattened[name] = flattenAST(a, info); | ||
} | ||
if (options.processArguments) { | ||
// check if the current field has arguments | ||
if (a.arguments && a.arguments.length) { | ||
_extends(flattened[name], { __arguments: getArguments(a) }); | ||
} | ||
} | ||
obj = obj || {}; | ||
return getSelections(ast).reduce(function (flattened, a) { | ||
if (isFragment(a)) { | ||
flattened = flattenAST(getAST(a, info), info, flattened); | ||
} else { | ||
var name = a.name.value; | ||
if (flattened[name] && flattened[name] !== '__arguments') { | ||
_extends(flattened[name], flattenAST(a, info, flattened[name])); | ||
} else { | ||
flattened[name] = flattenAST(a, info); | ||
} | ||
if (options.processArguments) { | ||
// check if the current field has arguments | ||
if (a.arguments && a.arguments.length) { | ||
_extends(flattened[name], { | ||
__arguments: getArguments(a) | ||
}); | ||
} | ||
} | ||
} | ||
return flattened; | ||
}, obj); | ||
return flattened; | ||
}, obj); | ||
} | ||
module.exports = function graphqlFields(info) { | ||
var obj = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { processArguments: false }; | ||
var fields = info.fieldNodes || info.fieldASTs; | ||
options.processArguments = opts.processArguments; | ||
return fields.reduce(function (o, ast) { | ||
return flattenAST(ast, info, o); | ||
}, obj) || {}; | ||
var obj = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { | ||
processArguments: false | ||
}; | ||
var fields = info.fieldNodes || info.fieldASTs; | ||
options.processArguments = opts.processArguments; | ||
return fields.reduce(function (o, ast) { | ||
return flattenAST(ast, info, o); | ||
}, obj) || {}; | ||
}; |
{ | ||
"name": "graphql-fields", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Turns GraphQLResolveInfo into a map of the requested fields", | ||
@@ -33,8 +33,9 @@ "main": "build/index.js", | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-plugin-transform-object-assign": "^6.22.0", | ||
"babel-preset-env": "^1.7.0", | ||
"@babel/cli": "^7.1.2", | ||
"@babel/core": "^7.1.2", | ||
"@babel/plugin-transform-object-assign": "^7.0.0", | ||
"@babel/preset-env": "^7.1.0", | ||
"graphql": "^14.0.2", | ||
"mocha": "^2.4.5" | ||
"mocha": "^5.2.0" | ||
} | ||
} |
7755
5
63
6