UNPKG

@neo4j/graphql

Version:

A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations

98 lines 4.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ComplexityEstimatorHelper = void 0; /* * Copyright (c) "Neo4j" * Neo4j Sweden AB [http://neo4j.com] * * This file is part of Neo4j. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const graphql_1 = require("graphql"); const graphql_query_complexity_1 = require("graphql-query-complexity"); class ComplexityEstimatorHelper { constructor(useComplexityEstimators) { this.useComplexityEstimators = useComplexityEstimators; this.objectTypeNameToFieldNamesMapForComplexityExtensions = new Map(); } registerField(parentObjectTypeNameOrInterfaceTypeName, fieldName) { if (this.useComplexityEstimators) { const existingFieldsForTypeName = this.objectTypeNameToFieldNamesMapForComplexityExtensions.get(parentObjectTypeNameOrInterfaceTypeName) ?? []; this.objectTypeNameToFieldNamesMapForComplexityExtensions.set(parentObjectTypeNameOrInterfaceTypeName, existingFieldsForTypeName.concat(fieldName)); } } hydrateDefinitionNodeWithComplexityExtensions(definition) { if (definition.kind !== graphql_1.Kind.OBJECT_TYPE_DEFINITION && definition.kind !== graphql_1.Kind.INTERFACE_TYPE_DEFINITION) { return definition; } if (!this.objectTypeNameToFieldNamesMapForComplexityExtensions.has(definition.name.value)) { return definition; } const fieldsWithComplexity = definition.fields?.map(f => { const hasFieldComplexityEstimator = this.getFieldsForParentTypeName(definition.name.value).find(fieldName => fieldName === f.name.value); if (!hasFieldComplexityEstimator) { return f; } return { ...f, extensions: { // COMPLEXITY FORMULA // c = c_child + lvl_limit * c_field, where // c_field = 1 // lvl_limit defaults to 1 // c_child comes from simpleEstimator complexity: ({ childComplexity, args }) => { const fieldDefaultComplexity = 1; const defaultLimitIfNotProvided = 1; if (args.limit ?? args.first) { return childComplexity + (args.limit ?? args.first) * fieldDefaultComplexity; } return childComplexity + defaultLimitIfNotProvided; }, }, }; }); return { ...definition, fields: fieldsWithComplexity, }; } hydrateSchemaFromSDLWithASTNodeExtensions(schema) { const types = schema.getTypeMap(); Object.values(types).forEach((type) => { if (type instanceof graphql_1.GraphQLObjectType || type instanceof graphql_1.GraphQLInterfaceType) { const fields = type.getFields(); Object.values(fields).forEach((field) => { if (field.astNode && 'extensions' in field.astNode) { field.extensions = field.astNode.extensions; } }); } }); } getComplexityEstimators() { if (!this.useComplexityEstimators) { return []; } return [ (0, graphql_query_complexity_1.fieldExtensionsEstimator)(), (0, graphql_query_complexity_1.simpleEstimator)({ defaultComplexity: 1 }), ]; } getFieldsForParentTypeName(parentObjectTypeNameOrInterfaceTypeName) { return this.objectTypeNameToFieldNamesMapForComplexityExtensions.get(parentObjectTypeNameOrInterfaceTypeName) || []; } } exports.ComplexityEstimatorHelper = ComplexityEstimatorHelper; //# sourceMappingURL=ComplexityEstimatorHelper.js.map