@neo4j/graphql
Version:
A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations
142 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.AggregationTypesMapper = void 0;
const graphql_1 = require("graphql");
const numerical_1 = require("../resolvers/field/numerical");
class AggregationTypesMapper {
constructor(composer, subgraph) {
this.subgraph = subgraph;
this.aggregationSelectionTypes = this.getOrCreateAggregationSelectionTypes(composer);
this.composer = composer;
}
getAggregationType(typeName) {
return this.aggregationSelectionTypes[typeName];
}
/** Top level count */
getCountType() {
const countFieldName = "Count";
const directives = this.subgraph ? [this.subgraph.getFullyQualifiedDirectiveName("shareable")] : [];
return this.composer.getOrCreateOTC(countFieldName, (countField) => {
countField.addFields({
nodes: {
type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLInt),
resolve: numerical_1.numericalResolver,
},
});
for (const directiveName of directives) {
countField.setDirectiveByName(directiveName);
}
});
}
/** Nested count */
getCountConnectionType() {
const countFieldName = "CountConnection";
const directives = this.subgraph ? [this.subgraph.getFullyQualifiedDirectiveName("shareable")] : [];
return this.composer.getOrCreateOTC(countFieldName, (countField) => {
countField.addFields({
nodes: {
type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLInt),
resolve: numerical_1.numericalResolver,
},
edges: {
type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLInt),
resolve: numerical_1.numericalResolver,
},
});
for (const directiveName of directives) {
countField.setDirectiveByName(directiveName);
}
});
}
getOrCreateAggregationSelectionTypes(composer) {
const composeInt = {
type: "Int",
resolve: numerical_1.numericalResolver,
args: {},
};
const composeFloat = {
type: "Float",
resolve: numerical_1.numericalResolver,
args: {},
};
const directives = this.subgraph ? [this.subgraph.getFullyQualifiedDirectiveName("shareable")] : [];
const aggregationSelectionTypeMatrix = [
{
name: "String",
fields: {
shortest: "String",
longest: "String",
},
directives,
},
{
name: "Float",
fields: {
max: composeFloat,
min: composeFloat,
average: composeFloat,
sum: composeFloat,
},
directives,
},
{
name: "Int",
fields: {
max: composeInt,
min: composeInt,
average: composeFloat,
sum: composeInt,
},
directives,
},
{
name: "BigInt",
fields: {
max: "BigInt",
min: "BigInt",
average: "BigInt",
sum: "BigInt",
},
directives,
},
{ name: "DateTime" },
{ name: "LocalDateTime" },
{ name: "LocalTime" },
{ name: "Time" },
{ name: "Duration" },
];
const aggregationSelectionTypes = aggregationSelectionTypeMatrix.reduce((res, { name, fields, directives }) => {
res[name] = this.createType({ composer, name, fields, directives });
return res;
}, {});
return aggregationSelectionTypes;
}
createType({ composer, name, fields, directives = [], }) {
return composer.getOrCreateOTC(`${name}AggregateSelection`, (tc) => {
tc.addFields(fields ?? { min: name, max: name });
for (const directive of directives) {
tc.setDirectiveByName(directive);
}
});
}
}
exports.AggregationTypesMapper = AggregationTypesMapper;
//# sourceMappingURL=aggregation-types-mapper.js.map