Skip to content

Commit

Permalink
feat: add pushUpdates mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
froid1911 committed Jan 15, 2024
1 parent c03fab1 commit 340c475
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 36 deletions.
13 changes: 7 additions & 6 deletions api/src/generated/nexus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ export interface NexusGenInputs {
remoteUrl?: string | null; // String
}
InputOperationUpdate: { // input type
hash: string; // String!
index: number; // Int!
input: string; // String!
revision: number; // Int!
skip: number; // Int!
stateHash: string; // String!
skip?: number | null; // Int
timestamp: string; // String!
type: string; // String!
}
InputStrandUpdate: { // input type
Expand Down Expand Up @@ -133,7 +134,7 @@ export interface NexusGenObjects {
driveId: string; // String!
revision: number; // Int!
scope: string; // String!
status: NexusGenEnums['UpdateStatus']; // UpdateStatus!
status: string; // String!
}
Mutation: {};
Node: { // root type
Expand Down Expand Up @@ -230,7 +231,7 @@ export interface NexusGenFieldTypes {
driveId: string; // String!
revision: number; // Int!
scope: string; // String!
status: NexusGenEnums['UpdateStatus']; // UpdateStatus!
status: string; // String!
}
Mutation: { // field return type
addDrive: NexusGenRootTypes['AddDriveResponse'] | null; // AddDriveResponse
Expand Down Expand Up @@ -336,7 +337,7 @@ export interface NexusGenFieldTypeNames {
driveId: 'String'
revision: 'Int'
scope: 'String'
status: 'UpdateStatus'
status: 'String'
}
Mutation: { // field return type name
addDrive: 'AddDriveResponse'
Expand Down
9 changes: 5 additions & 4 deletions api/src/generated/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ input DocumentDriveStateInput {
}

input InputOperationUpdate {
hash: String!
index: Int!
input: String!
revision: Int!
skip: Int!
stateHash: String!
skip: Int
timestamp: String!
type: String!
}

Expand All @@ -79,7 +80,7 @@ type ListenerRevision {
driveId: String!
revision: Int!
scope: String!
status: UpdateStatus!
status: String!
}

input ListenerRevisionInput {
Expand Down
9 changes: 5 additions & 4 deletions api/src/modules/DocumentDrive/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const ListenerRevision = objectType({
t.nonNull.string('documentId');
t.nonNull.string('scope');
t.nonNull.string('branch');
t.nonNull.field('status', { type: UpdateStatus });
t.nonNull.string('status');
t.nonNull.int('revision');
},
});
Expand All @@ -181,11 +181,12 @@ export const OperationUpdate = objectType({
export const InputOperationUpdate = inputObjectType({
name: 'InputOperationUpdate',
definition(t) {
t.nonNull.int('revision');
t.nonNull.int('skip');
t.nonNull.int('index');
t.int('skip');
t.nonNull.string('type');
t.nonNull.string('input');
t.nonNull.string('stateHash');
t.nonNull.string('hash');
t.nonNull.string('timestamp');
},
});

Expand Down
7 changes: 6 additions & 1 deletion api/src/modules/DocumentDrive/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function getDocumentDriveCRUD(prisma: Prisma.TransactionClient) {
return true;
},

createDocument: async (driveId: string, input: CreateDocumentInput) => {
addDocument: async (driveId: string, input: CreateDocumentInput) => {
try {
await driveServer.createDocument(driveId, input);
} catch (e) {
Expand All @@ -82,5 +82,10 @@ export function getDocumentDriveCRUD(prisma: Prisma.TransactionClient) {
) => {
return await driveServer.addOperation(driveId, documentId, operation);
},

addDriveOperations: async (driveId: string, operations: Operation[]) => {
const result = await driveServer.addDriveOperations(driveId, operations);
return result;
},
};
}
2 changes: 0 additions & 2 deletions api/src/modules/DocumentDrive/mutations/addDrive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { mutationField, nonNull, objectType } from 'nexus';
import { GraphQLJSONObject } from 'graphql-type-json';
import { scalarType } from 'nexus';

import {
DocumentDriveLocalState,
Expand Down
63 changes: 44 additions & 19 deletions api/src/modules/DocumentDrive/mutations/pushUpdates.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { list, mutationField, nonNull } from 'nexus';
import { InputStrandUpdate, ListenerRevision } from '../definitions';
import {
InputStrandUpdate,
ListenerRevision,
UpdateStatus,
} from '../definitions';
import { OperationScope } from 'document-model/document';
import { Operation } from 'document-model/dist/browser/document';
import e from 'express';

export const pushUpdates = mutationField('pushUpdates', {
type: list(ListenerRevision),
Expand All @@ -10,23 +15,43 @@ export const pushUpdates = mutationField('pushUpdates', {
},
resolve: async (_parent, { strands }, ctx) => {
//@todo: get connect drive server from ctx and apply updates
// if (!strands || strands?.length === 0) return [[]];
// await Promise.all(
// strands.map((s) => {
// return Promise.all(
// s.operations?.map((o) => {
// const op: Operation = {
// scope: s.scope as OperationScope,
// ...o,
// index: o.revision,
// timestamp: new Date().toISOString(),
// hash: '',
// };
// const result = ctx.prisma.document.addOperation(
// s.driveId,
// s.documentId,
// op,
// );
return [];

if (!strands || strands?.length === 0) return [[]];
const results = await Promise.all(
strands.map(async (s) => {
const operations = s.operations?.map((o) => {
const op = {
scope: s.scope as OperationScope,
branch: s.branch,
...o,
input: JSON.parse(o.input),
};

return op;
});
try {
const result = await ctx.prisma.document.addDriveOperations(
s.driveId,
operations,
);
console.log(result);
return result;
} catch (e) {
console.log(e);
return null;
}
}),
);
return strands.map((r, i) => {
// if (!result) return null;
return {
driveId: r.driveId,
documentId: r.documentId,
scope: r.scope,
branch: r.branch,
status: 'SUCCESS',
revision: r.operations[r.operations.length - 1].index + 1,
};
});
},
});
18 changes: 18 additions & 0 deletions api/src/modules/DocumentDrive/queries/document.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { nonNull, queryField } from 'nexus';
import { DocumentDriveState } from '../definitions';

export const getDocument = queryField('document', {
type: DocumentDriveState,
args: {
driveId: nonNull('String'),
id: nonNull('String'),
},
resolve: async (_parent, { driveId, id }, ctx) => {
try {
const drive = await ctx.prisma.document.getDocument(driveId, id);
return drive;
} catch (e) {
return null;
}
},
});

0 comments on commit 340c475

Please sign in to comment.