@neo4j/graphql
Version:
A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations
123 lines • 5.21 kB
JavaScript
;
/*
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeConnectionWhereInputType = makeConnectionWhereInputType;
exports.withConnectionWhereInputType = withConnectionWhereInputType;
exports.withConnectionSortInputType = withConnectionSortInputType;
const ConcreteEntityAdapter_1 = require("../../schema-model/entity/model-adapters/ConcreteEntityAdapter");
const InterfaceEntityAdapter_1 = require("../../schema-model/entity/model-adapters/InterfaceEntityAdapter");
const UnionEntityAdapter_1 = require("../../schema-model/entity/model-adapters/UnionEntityAdapter");
// tODO: refactor into smaller fns for unions, like disconnect-input
function makeConnectionWhereInputType({ relationshipAdapter, composer, }) {
const typeName = relationshipAdapter.operations.getConnectionWhereTypename();
if (composer.has(typeName)) {
return composer.getITC(typeName);
}
const targetEntityAdapter = relationshipAdapter.target;
if (targetEntityAdapter instanceof UnionEntityAdapter_1.UnionEntityAdapter) {
const connectionWhereITC = composer.createInputTC(typeName);
targetEntityAdapter.concreteEntities.forEach((concreteEntity) => {
const unionWhereITC = withConnectionWhereInputType({
relationshipAdapter,
memberEntity: concreteEntity,
composer,
});
connectionWhereITC.addFields({
[concreteEntity.name]: unionWhereITC,
});
});
return connectionWhereITC;
}
return withConnectionWhereInputType({
relationshipAdapter,
composer,
});
}
function withConnectionWhereInputType({ relationshipAdapter, memberEntity, composer, }) {
const typeName = relationshipAdapter.operations.getConnectionWhereTypename(memberEntity);
if (composer.has(typeName)) {
return composer.getITC(typeName);
}
const targetEntity = memberEntity || relationshipAdapter.target;
const connectionWhereInputType = composer.createInputTC({
name: typeName,
fields: {},
});
connectionWhereInputType.addFields({
AND: connectionWhereInputType.NonNull.List,
OR: connectionWhereInputType.NonNull.List,
NOT: connectionWhereInputType,
node: targetEntity.operations.whereInputTypeName,
});
if (relationshipAdapter.hasAnyProperties) {
connectionWhereInputType.addFields({
edge: relationshipAdapter.operations.whereInputTypeName,
});
}
return connectionWhereInputType;
}
function shouldGenerateConnectionSortInput(relationshipAdapter) {
if (relationshipAdapter.hasAnyProperties) {
return true;
}
if (!(relationshipAdapter.target instanceof UnionEntityAdapter_1.UnionEntityAdapter)) {
return true;
}
return false;
}
function withConnectionSortInputType({ relationshipAdapter, composer, }) {
if (!shouldGenerateConnectionSortInput(relationshipAdapter)) {
return;
}
const typeName = relationshipAdapter.operations.connectionSortInputTypename;
if (composer.has(typeName)) {
return composer.getITC(typeName);
}
const fields = makeConnectionSortInputTypeFields({ relationshipAdapter });
if (!fields) {
return;
}
const connectionSortITC = composer.createInputTC({
name: typeName,
fields,
});
return connectionSortITC;
}
function makeConnectionSortInputTypeFields({ relationshipAdapter, }) {
const targetIsInterfaceWithSortableFields = relationshipAdapter.target instanceof InterfaceEntityAdapter_1.InterfaceEntityAdapter &&
relationshipAdapter.target.sortableFields.length;
const targetIsConcreteWithSortableFields = relationshipAdapter.target instanceof ConcreteEntityAdapter_1.ConcreteEntityAdapter && relationshipAdapter.target.sortableFields.length;
const fields = {};
if (targetIsInterfaceWithSortableFields || targetIsConcreteWithSortableFields) {
fields["node"] = relationshipAdapter.target.operations.sortInputTypeName;
}
/*
We include all properties here to maintain existing behaviour.
In future sorting by arrays should become an aggregation sort because it sorts by the length of the array.
*/
if (relationshipAdapter.hasAnyProperties) {
fields["edge"] = relationshipAdapter.operations.sortInputTypeName;
}
if (Object.keys(fields).length === 0) {
return undefined;
}
return fields;
}
//# sourceMappingURL=connection-where-input.js.map