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

updateLocation, deleteLocation mutations added #460

Merged
merged 3 commits into from
Aug 7, 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
16 changes: 16 additions & 0 deletions src/schema/api.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ type DeleteLabelPayload {
label: Label
}

type DeleteLocationPayload {
location: Location!
}

input DeleteSnapshotInput {
deviceId: String!
name: String!
Expand Down Expand Up @@ -557,6 +561,7 @@ type Mutation {
deleteBlueprint(id: String!): DeleteBlueprintPayload!
deleteDevice(id: String!): DeleteDevicePayload!
deleteLabel(id: String!): DeleteLabelPayload!
deleteLocation(id: String!): DeleteLocationPayload!
deleteSnapshot(input: DeleteSnapshotInput!): DeleteSnapshotPayload
deleteStream(id: String!): DeleteStreamPayload!
importCSV(input: CSVImportInput!): CSVImport
Expand All @@ -571,6 +576,7 @@ type Mutation {
updateDevice(id: String!, input: UpdateDeviceInput!): UpdateDevicePayload!
updateDiscoveredAt(deviceIds: [String!]!): [DeviceDiscoveryPayload!]!
updateGraphNodeCoordinates(input: UpdateGraphNodeCoordinatesInput!): UpdateGraphNodeCoordinatesPayload!
updateLocation(id: String!, input: UpdateLocationInput!): UpdateLocationPayload!
updateStream(id: String!, input: UpdateStreamInput!): UpdateStreamPayload!
}

Expand Down Expand Up @@ -940,6 +946,16 @@ type UpdateGraphNodeCoordinatesPayload {
deviceNames: [String!]!
}

input UpdateLocationInput {
coordinates: Coordinates!
countryId: String
name: String!
}

type UpdateLocationPayload {
location: Location!
}

input UpdateStreamInput {
blueprintId: String
deviceName: String!
Expand Down
83 changes: 81 additions & 2 deletions src/schema/location.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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 } from 'nexus';
import { toGraphId } from '../helpers/id-helper';
import { arg, 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';

Expand Down Expand Up @@ -165,3 +165,82 @@ export const AddLocationMutation = extendType({
});
},
});

export const UpdateLocationInput = inputObjectType({
name: 'UpdateLocationInput',
definition: (t) => {
t.nonNull.string('name');
t.string('countryId');
t.nonNull.field({ name: 'coordinates', type: Coordinates });
},
});

export const UpdateLocationPayload = objectType({
name: 'UpdateLocationPayload',
definition: (t) => {
t.nonNull.field('location', { type: Location });
},
});

export const UpdateLocationMutation = extendType({
type: 'Mutation',
definition: (t) => {
t.nonNull.field('updateLocation', {
type: UpdateLocationPayload,
args: {
id: nonNull(stringArg()),
input: nonNull(arg({ type: UpdateLocationInput })),
},
resolve: async (_, args, { prisma, tenantId }) => {
const nativeId = fromGraphId('Location', args.id);
const { input } = args;

const countryName = getCountryName(input.countryId ?? null);

const location = await prisma.location.update({
where: { id: nativeId },
data: {
tenantId,
name: input.name,
country: countryName,
latitude: input.coordinates.latitude.toString(),
longitude: input.coordinates.latitude.toString(),
},
});
return {
location,
};
},
});
},
});

export const DeleteLocationPayload = objectType({
name: 'DeleteLocationPayload',
definition: (t) => {
t.nonNull.field('location', { type: Location });
},
});

export const DeleteLocationMutation = extendType({
type: 'Mutation',
definition: (t) => {
t.nonNull.field('deleteLocation', {
type: DeleteLocationPayload,
args: {
id: nonNull(stringArg()),
},
resolve: async (_, args, { prisma, tenantId }) => {
const nativeId = fromGraphId('Location', args.id);

const location = await prisma.location.delete({
where: { id: nativeId, AND: { tenantId } },
});

return {
location,
};
},
});
},
});
43 changes: 43 additions & 0 deletions src/schema/nexus-typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ export interface NexusGenInputs {
coordinates: NexusGenInputs['GraphNodeCoordinatesInput'][]; // [GraphNodeCoordinatesInput!]!
layer?: NexusGenEnums['TopologyLayer'] | null; // TopologyLayer
};
UpdateLocationInput: {
// input type
coordinates: NexusGenInputs['Coordinates']; // Coordinates!
countryId?: string | null; // String
name: string; // String!
};
UpdateStreamInput: {
// input type
blueprintId?: string | null; // String
Expand Down Expand Up @@ -355,6 +361,10 @@ export interface NexusGenObjects {
// root type
label?: NexusGenRootTypes['Label'] | null; // Label
};
DeleteLocationPayload: {
// root type
location: NexusGenRootTypes['Location']; // Location!
};
DeleteSnapshotPayload: {
// root type
snapshot?: NexusGenRootTypes['Snapshot'] | null; // Snapshot
Expand Down Expand Up @@ -783,6 +793,10 @@ export interface NexusGenObjects {
// root type
deviceNames: string[]; // [String!]!
};
UpdateLocationPayload: {
// root type
location: NexusGenRootTypes['Location']; // Location!
};
UpdateStreamPayload: {
// root type
stream?: NexusGenRootTypes['Stream'] | null; // Stream
Expand Down Expand Up @@ -968,6 +982,10 @@ export interface NexusGenFieldTypes {
// field return type
label: NexusGenRootTypes['Label'] | null; // Label
};
DeleteLocationPayload: {
// field return type
location: NexusGenRootTypes['Location']; // Location!
};
DeleteSnapshotPayload: {
// field return type
snapshot: NexusGenRootTypes['Snapshot'] | null; // Snapshot
Expand Down Expand Up @@ -1220,6 +1238,7 @@ export interface NexusGenFieldTypes {
deleteBlueprint: NexusGenRootTypes['DeleteBlueprintPayload']; // DeleteBlueprintPayload!
deleteDevice: NexusGenRootTypes['DeleteDevicePayload']; // DeleteDevicePayload!
deleteLabel: NexusGenRootTypes['DeleteLabelPayload']; // DeleteLabelPayload!
deleteLocation: NexusGenRootTypes['DeleteLocationPayload']; // DeleteLocationPayload!
deleteSnapshot: NexusGenRootTypes['DeleteSnapshotPayload'] | null; // DeleteSnapshotPayload
deleteStream: NexusGenRootTypes['DeleteStreamPayload']; // DeleteStreamPayload!
importCSV: NexusGenRootTypes['CSVImport'] | null; // CSVImport
Expand All @@ -1234,6 +1253,7 @@ export interface NexusGenFieldTypes {
updateDevice: NexusGenRootTypes['UpdateDevicePayload']; // UpdateDevicePayload!
updateDiscoveredAt: NexusGenRootTypes['DeviceDiscoveryPayload'][]; // [DeviceDiscoveryPayload!]!
updateGraphNodeCoordinates: NexusGenRootTypes['UpdateGraphNodeCoordinatesPayload']; // UpdateGraphNodeCoordinatesPayload!
updateLocation: NexusGenRootTypes['UpdateLocationPayload']; // UpdateLocationPayload!
updateStream: NexusGenRootTypes['UpdateStreamPayload']; // UpdateStreamPayload!
};
NetInterface: {
Expand Down Expand Up @@ -1519,6 +1539,10 @@ export interface NexusGenFieldTypes {
// field return type
deviceNames: string[]; // [String!]!
};
UpdateLocationPayload: {
// field return type
location: NexusGenRootTypes['Location']; // Location!
};
UpdateStreamPayload: {
// field return type
stream: NexusGenRootTypes['Stream'] | null; // Stream
Expand Down Expand Up @@ -1704,6 +1728,10 @@ export interface NexusGenFieldTypeNames {
// field return type name
label: 'Label';
};
DeleteLocationPayload: {
// field return type name
location: 'Location';
};
DeleteSnapshotPayload: {
// field return type name
snapshot: 'Snapshot';
Expand Down Expand Up @@ -1956,6 +1984,7 @@ export interface NexusGenFieldTypeNames {
deleteBlueprint: 'DeleteBlueprintPayload';
deleteDevice: 'DeleteDevicePayload';
deleteLabel: 'DeleteLabelPayload';
deleteLocation: 'DeleteLocationPayload';
deleteSnapshot: 'DeleteSnapshotPayload';
deleteStream: 'DeleteStreamPayload';
importCSV: 'CSVImport';
Expand All @@ -1970,6 +1999,7 @@ export interface NexusGenFieldTypeNames {
updateDevice: 'UpdateDevicePayload';
updateDiscoveredAt: 'DeviceDiscoveryPayload';
updateGraphNodeCoordinates: 'UpdateGraphNodeCoordinatesPayload';
updateLocation: 'UpdateLocationPayload';
updateStream: 'UpdateStreamPayload';
};
NetInterface: {
Expand Down Expand Up @@ -2255,6 +2285,10 @@ export interface NexusGenFieldTypeNames {
// field return type name
deviceNames: 'String';
};
UpdateLocationPayload: {
// field return type name
location: 'Location';
};
UpdateStreamPayload: {
// field return type name
stream: 'Stream';
Expand Down Expand Up @@ -2386,6 +2420,10 @@ export interface NexusGenArgTypes {
// args
id: string; // String!
};
deleteLocation: {
// args
id: string; // String!
};
deleteSnapshot: {
// args
input: NexusGenInputs['DeleteSnapshotInput']; // DeleteSnapshotInput!
Expand Down Expand Up @@ -2444,6 +2482,11 @@ export interface NexusGenArgTypes {
// args
input: NexusGenInputs['UpdateGraphNodeCoordinatesInput']; // UpdateGraphNodeCoordinatesInput!
};
updateLocation: {
// args
id: string; // String!
input: NexusGenInputs['UpdateLocationInput']; // UpdateLocationInput!
};
updateStream: {
// args
id: string; // String!
Expand Down