Sha256: f5bab4fbc763c1e27bf654089b1e55577281d1a217070e3866d151cbecfabaa1

Contents?: true

Size: 1.49 KB

Versions: 26

Compression:

Stored size: 1.49 KB

Contents

'use strict';

Object.defineProperty(exports, '__esModule', {
  value: true,
});
exports.NoUnusedFragmentsRule = NoUnusedFragmentsRule;

var _GraphQLError = require('../../error/GraphQLError.js');

/**
 * No unused fragments
 *
 * A GraphQL document is only valid if all fragment definitions are spread
 * within operations, or spread within other fragments spread within operations.
 *
 * See https://spec.graphql.org/draft/#sec-Fragments-Must-Be-Used
 */
function NoUnusedFragmentsRule(context) {
  const operationDefs = [];
  const fragmentDefs = [];
  return {
    OperationDefinition(node) {
      operationDefs.push(node);
      return false;
    },

    FragmentDefinition(node) {
      fragmentDefs.push(node);
      return false;
    },

    Document: {
      leave() {
        const fragmentNameUsed = Object.create(null);

        for (const operation of operationDefs) {
          for (const fragment of context.getRecursivelyReferencedFragments(
            operation,
          )) {
            fragmentNameUsed[fragment.name.value] = true;
          }
        }

        for (const fragmentDef of fragmentDefs) {
          const fragName = fragmentDef.name.value;

          if (fragmentNameUsed[fragName] !== true) {
            context.reportError(
              new _GraphQLError.GraphQLError(
                `Fragment "${fragName}" is never used.`,
                {
                  nodes: fragmentDef,
                },
              ),
            );
          }
        }
      },
    },
  };
}

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
clapton-0.0.26 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js
clapton-0.0.25 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js
clapton-0.0.24 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js
clapton-0.0.23 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js
clapton-0.0.22 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js
clapton-0.0.21 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js
clapton-0.0.20 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js
clapton-0.0.19 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js
clapton-0.0.18 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js
clapton-0.0.17 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js
clapton-0.0.16 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js
clapton-0.0.15 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js
clapton-0.0.14 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js
clapton-0.0.13 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js
clapton-0.0.12 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js
clapton-0.0.11 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js
clapton-0.0.10 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js
clapton-0.0.9 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js
clapton-0.0.8 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js
clapton-0.0.7 lib/clapton/javascripts/node_modules/graphql/validation/rules/NoUnusedFragmentsRule.js