UNPKG

@neo4j/graphql

Version:

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

118 lines 5.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.augmentObjectOrInterfaceTypeWithRelationshipField = augmentObjectOrInterfaceTypeWithRelationshipField; exports.augmentObjectOrInterfaceTypeWithConnectionField = augmentObjectOrInterfaceTypeWithConnectionField; /* * 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 constants_1 = require("../../constants"); const InterfaceEntityAdapter_1 = require("../../schema-model/entity/model-adapters/InterfaceEntityAdapter"); const UnionEntityAdapter_1 = require("../../schema-model/entity/model-adapters/UnionEntityAdapter"); const RelationshipAdapter_1 = require("../../schema-model/relationship/model-adapters/RelationshipAdapter"); const pagination_1 = require("../pagination"); const to_compose_1 = require("../to-compose"); const connection_object_type_1 = require("./connection-object-type"); const connection_where_input_1 = require("./connection-where-input"); const sort_and_options_input_1 = require("./sort-and-options-input"); function augmentObjectOrInterfaceTypeWithRelationshipField({ relationshipAdapter, userDefinedFieldDirectives, subgraph, composer, features, }) { const fields = {}; const relationshipField = { type: relationshipAdapter.operations.getTargetTypePrettyName(), description: relationshipAdapter.description, directives: (0, to_compose_1.graphqlDirectivesToCompose)(userDefinedFieldDirectives.get(relationshipAdapter.name) || []), }; let generateRelFieldArgs = true; // Subgraph schemas do not support arguments on relationship fields (singular) if (subgraph) { if (!relationshipAdapter.isList) { generateRelFieldArgs = false; } } if (generateRelFieldArgs) { const relationshipTarget = relationshipAdapter instanceof RelationshipAdapter_1.RelationshipAdapter && relationshipAdapter.originalTarget ? relationshipAdapter.originalTarget : relationshipAdapter.target; const whereTypeName = relationshipTarget.operations.whereInputTypeName; const nodeFieldsArgs = { where: whereTypeName, limit: features?.limitRequired ? new graphql_1.GraphQLNonNull(graphql_1.GraphQLInt) : graphql_1.GraphQLInt, offset: graphql_1.GraphQLInt, }; if (!(relationshipTarget instanceof UnionEntityAdapter_1.UnionEntityAdapter)) { const sortConfig = (0, sort_and_options_input_1.makeSortInput)({ entityAdapter: relationshipTarget, userDefinedFieldDirectives: new Map(), composer, }); if (sortConfig) { nodeFieldsArgs["sort"] = sortConfig.NonNull.List; } } relationshipField.args = nodeFieldsArgs; } if (relationshipAdapter.isReadable()) { fields[relationshipAdapter.name] = relationshipField; } return fields; } function augmentObjectOrInterfaceTypeWithConnectionField(relationshipAdapter, userDefinedFieldDirectives, schemaComposer, features) { const fields = {}; const deprecatedDirectives = (0, to_compose_1.graphqlDirectivesToCompose)((userDefinedFieldDirectives.get(relationshipAdapter.name) || []).filter((directive) => directive.name.value === constants_1.DEPRECATED)); const composeNodeArgs = { where: (0, connection_where_input_1.makeConnectionWhereInputType)({ relationshipAdapter, composer: schemaComposer, }), first: { type: features?.limitRequired ? new graphql_1.GraphQLNonNull(graphql_1.GraphQLInt) : graphql_1.GraphQLInt, }, after: { type: graphql_1.GraphQLString, }, }; const connectionSortITC = (0, connection_where_input_1.withConnectionSortInputType)({ relationshipAdapter, composer: schemaComposer, }); if (connectionSortITC) { composeNodeArgs.sort = connectionSortITC.NonNull.List; } const isTargetUnion = relationshipAdapter.target instanceof UnionEntityAdapter_1.UnionEntityAdapter; const isSourceInterface = relationshipAdapter.source instanceof InterfaceEntityAdapter_1.InterfaceEntityAdapter; if (relationshipAdapter.isReadable() || (relationshipAdapter.aggregate && !isTargetUnion && !isSourceInterface)) { fields[relationshipAdapter.operations.connectionFieldName] = { type: (0, connection_object_type_1.withConnectionObjectType)({ relationshipAdapter, composer: schemaComposer, }).NonNull, args: composeNodeArgs, directives: deprecatedDirectives, resolve: (source, args, _ctx, info) => { return (0, pagination_1.connectionFieldResolver)({ connectionFieldName: relationshipAdapter.operations.connectionFieldName, args, info, source, }); }, }; } return fields; } //# sourceMappingURL=augment-object-or-interface.js.map