Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FD-702 add filter and order args to location query #473

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/helpers/location-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,40 @@ export function getCountryName(countryId: string | null): string | null {

return null;
}

type FilterInput = {
locationName?: string | null;
};

type FilterQuery = {
name?: Record<string, unknown>;
};

type LocationOrderingInput = {
sortKey: 'name';
direction: 'ASC' | 'DESC';
};

function getLocationNameQuery(locationName?: string | null): Record<string, unknown> | undefined {
return locationName ? { contains: locationName, mode: 'insensitive' } : undefined;
}

export function getLocationFilterQuery(filter?: FilterInput | null): FilterQuery | undefined {
if (!filter) {
return undefined;
}
const { locationName } = filter;
return {
name: getLocationNameQuery(locationName),
};
}

export function getLocationOrderingQuery(ordering?: LocationOrderingInput | null): Record<string, unknown> | undefined {
if (!ordering) {
return undefined;
}

return {
orderBy: [{ [ordering.sortKey]: ordering.direction.toLowerCase() }],
};
}
22 changes: 21 additions & 1 deletion src/schema/api.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ input FilterLabelsInput {
name: String!
}

input FilterLocationsInput {
name: String
}

input FilterNeighborInput {
deviceName: String!
topologyType: TopologyType!
Expand Down Expand Up @@ -512,6 +516,11 @@ type LocationEdge {
node: Location!
}

input LocationOrderByInput {
direction: SortDirection!
sortKey: SortLocationBy!
}

type LspPath {
metadata: LspPathMetadata
path: [String!]!
Expand Down Expand Up @@ -769,7 +778,14 @@ type Query {
): DeviceConnection!
kafkaHealthCheck: IsOkResponse
labels(after: String, before: String, filter: FilterLabelsInput, first: Int, last: Int): LabelConnection!
locations(after: String, before: String, first: Int, last: Int): LocationConnection!
locations(
after: String
before: String
filter: FilterLocationsInput
first: Int
last: Int
orderBy: LocationOrderByInput
): LocationConnection!
lspPath(deviceId: String!, lspId: String!): LspPath
mplsLspCount(deviceId: String!): MplsLspCount
mplsTopology: MplsTopology
Expand Down Expand Up @@ -831,6 +847,10 @@ enum SortDirection {
DESC
}

enum SortLocationBy {
name
}

enum SortStreamBy {
createdAt
deviceName
Expand Down
37 changes: 31 additions & 6 deletions src/schema/location.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { findManyCursorConnection } from '@devoxa/prisma-relay-cursor-connection';
import { connectionFromArray } from 'graphql-relay';
import countries from 'i18n-iso-countries';
import { arg, extendType, inputObjectType, nonNull, objectType, stringArg } from 'nexus';
import { arg, enumType, extendType, inputObjectType, nonNull, objectType, stringArg } from 'nexus';
import { fromGraphId, toGraphId } from '../helpers/id-helper';
import { Node, PageInfo, PaginationConnectionArgs } from './global-types';
import { getCountryName } from '../helpers/location-helpers';
import { Node, PageInfo, PaginationConnectionArgs, SortDirection } from './global-types';
import { getCountryName, getLocationFilterQuery, getLocationOrderingQuery } from '../helpers/location-helpers';

export const Location = objectType({
name: 'Location',
Expand Down Expand Up @@ -94,16 +94,40 @@ export const LocationConnection = objectType({
t.nonNull.int('totalCount');
},
});
export const FilterLocationsInput = inputObjectType({
name: 'FilterLocationsInput',
definition: (t) => {
t.string('name');
},
});
export const SortLocationBy = enumType({
name: 'SortLocationBy',
members: ['name'],
});
export const LocationOrderByInput = inputObjectType({
name: 'LocationOrderByInput',
definition: (t) => {
t.nonNull.field('sortKey', { type: SortLocationBy });
t.nonNull.field('direction', { type: SortDirection });
},
});
export const LocationQuery = extendType({
type: 'Query',
definition: (t) => {
t.nonNull.field('locations', {
type: LocationConnection,
args: PaginationConnectionArgs,
args: {
...PaginationConnectionArgs,
filter: FilterLocationsInput,
orderBy: LocationOrderByInput,
},
resolve: async (_, args, { prisma, tenantId }) => {
const baseArgs = { where: { tenantId } };
const { filter, orderBy } = args;
const filterQuery = getLocationFilterQuery({ locationName: filter?.name });
const orderingArgs = getLocationOrderingQuery(orderBy);
const baseArgs = { where: { tenantId, ...filterQuery } };
const result = await findManyCursorConnection(
(paginationArgs) => prisma.location.findMany({ ...baseArgs, ...paginationArgs }),
(paginationArgs) => prisma.location.findMany({ ...baseArgs, ...orderingArgs, ...paginationArgs }),
() => prisma.location.count(baseArgs),
args,
);
Expand Down Expand Up @@ -136,6 +160,7 @@ export const AddLocationInput = inputObjectType({
t.nonNull.field({ name: 'coordinates', type: Coordinates });
},
});

export const AddLocationMutation = extendType({
type: 'Mutation',
definition: (t) => {
Expand Down
12 changes: 12 additions & 0 deletions src/schema/nexus-typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ export interface NexusGenInputs {
// input type
name: string; // String!
};
FilterLocationsInput: {
// input type
name?: string | null; // String
};
FilterNeighborInput: {
// input type
deviceName: string; // String!
Expand All @@ -168,6 +172,11 @@ export interface NexusGenInputs {
x: number; // Float!
y: number; // Float!
};
LocationOrderByInput: {
// input type
direction: NexusGenEnums['SortDirection']; // SortDirection!
sortKey: NexusGenEnums['SortLocationBy']; // SortLocationBy!
};
PolygonInput: {
// input type
polygon?: number[][][] | null; // [[[Float!]!]!]
Expand Down Expand Up @@ -231,6 +240,7 @@ export interface NexusGenEnums {
Signalization: 'LDP' | 'RSVP';
SortDeviceBy: 'discoveredAt' | 'modelVersion' | 'name';
SortDirection: 'ASC' | 'DESC';
SortLocationBy: 'name';
SortStreamBy: 'createdAt' | 'deviceName' | 'streamName';
TopologyLayer: 'ETH_TOPOLOGY' | 'MPLS_TOPOLOGY' | 'PHYSICAL_TOPOLOGY' | 'PTP_TOPOLOGY';
TopologyType: 'ETH_TOPOLOGY' | 'MPLS_TOPOLOGY' | 'NETWORK_TOPOLOGY' | 'PHYSICAL_TOPOLOGY' | 'PTP_TOPOLOGY';
Expand Down Expand Up @@ -2691,8 +2701,10 @@ export interface NexusGenArgTypes {
// args
after?: string | null; // String
before?: string | null; // String
filter?: NexusGenInputs['FilterLocationsInput'] | null; // FilterLocationsInput
first?: number | null; // Int
last?: number | null; // Int
orderBy?: NexusGenInputs['LocationOrderByInput'] | null; // LocationOrderByInput
};
lspPath: {
// args
Expand Down
Loading