graphql-fields
Advanced tools
Comparing version 1.3.0 to 2.0.0
@@ -5,2 +5,4 @@ 'use strict'; | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } | ||
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; } | ||
@@ -40,5 +42,50 @@ | ||
function getDirectiveValue(directive, info) { | ||
var arg = directive.arguments[0]; // only arg on an include or skip directive is "if" | ||
if (arg.value.kind !== "Variable") { | ||
return !!arg.value.value; | ||
} | ||
return info.variableValues[arg.value.name.value]; | ||
} | ||
function getDirectiveResults(ast, info) { | ||
var directiveResult = { | ||
shouldInclude: true, | ||
shouldSkip: false | ||
}; | ||
return ast.directives.reduce(function (result, directive) { | ||
switch (directive.name.value) { | ||
case "include": | ||
return _objectSpread({}, result, { | ||
shouldInclude: getDirectiveValue(directive, info) | ||
}); | ||
case "skip": | ||
return _objectSpread({}, result, { | ||
shouldSkip: getDirectiveValue(directive, info) | ||
}); | ||
default: | ||
return result; | ||
} | ||
}, directiveResult); | ||
} | ||
function flattenAST(ast, info, obj) { | ||
obj = obj || {}; | ||
return getSelections(ast).reduce(function (flattened, a) { | ||
if (a.directives && a.directives.length) { | ||
var _getDirectiveResults = getDirectiveResults(a, info), | ||
shouldInclude = _getDirectiveResults.shouldInclude, | ||
shouldSkip = _getDirectiveResults.shouldSkip; // field/fragment is not included if either the @skip condition is true or the @include condition is false | ||
// https://facebook.github.io/graphql/draft/#sec--include | ||
if (shouldSkip || !shouldInclude) { | ||
return flattened; | ||
} | ||
} | ||
if (isFragment(a)) { | ||
@@ -45,0 +92,0 @@ flattened = flattenAST(getAST(a, info), info, flattened); |
# Changelog | ||
## 2.0.0 (2018-11-29) | ||
### Added | ||
- @skip and @include directive parsing | ||
## 1.3.0 (2018-11-15) | ||
@@ -4,0 +10,0 @@ |
{ | ||
"name": "graphql-fields", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"description": "Turns GraphQLResolveInfo into a map of the requested fields", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
# graphql-fields | ||
Turns GraphQLResolveInfo into a map of the requested fields. Flattens all fragments and duplicated fields into a neat object to easily see which fields were requested at any level. | ||
Turns GraphQLResolveInfo into a map of the requested fields. Flattens all fragments and duplicated fields into a neat object to easily see which fields were requested at any level. Takes into account any `@include` or `@skip` directives, excluding fields/fragments which are `@include(if: $false)` or `@skip(if: $true)`. | ||
@@ -19,3 +19,5 @@ ## Usage | ||
lastName: {type: graphql.GraphQLString}, | ||
middleName: {type: graphql.GraphQLString} | ||
middleName: {type: graphql.GraphQLString}, | ||
nickName: {type: graphql.GraphQLString}, | ||
maidenName: {type: graphql.GraphQLString} | ||
} | ||
@@ -69,2 +71,3 @@ }), | ||
firstName | ||
nickName @skip(if: true) | ||
} | ||
@@ -76,2 +79,3 @@ | ||
middleName | ||
maidenName @include(if: false) | ||
} | ||
@@ -78,0 +82,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10500
105
167