diff --git a/api/src/generated/nexus.ts b/api/src/generated/nexus.ts index 70d2b9f7..bf625b41 100644 --- a/api/src/generated/nexus.ts +++ b/api/src/generated/nexus.ts @@ -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 @@ -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 @@ -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 @@ -336,7 +337,7 @@ export interface NexusGenFieldTypeNames { driveId: 'String' revision: 'Int' scope: 'String' - status: 'UpdateStatus' + status: 'String' } Mutation: { // field return type name addDrive: 'AddDriveResponse' diff --git a/api/src/generated/schema.graphql b/api/src/generated/schema.graphql index 145e44a3..482356b9 100644 --- a/api/src/generated/schema.graphql +++ b/api/src/generated/schema.graphql @@ -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! } @@ -79,7 +80,7 @@ type ListenerRevision { driveId: String! revision: Int! scope: String! - status: UpdateStatus! + status: String! } input ListenerRevisionInput { diff --git a/api/src/modules/DocumentDrive/definitions.ts b/api/src/modules/DocumentDrive/definitions.ts index 1a389b8d..ade51151 100644 --- a/api/src/modules/DocumentDrive/definitions.ts +++ b/api/src/modules/DocumentDrive/definitions.ts @@ -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'); }, }); @@ -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'); }, }); diff --git a/api/src/modules/DocumentDrive/model.ts b/api/src/modules/DocumentDrive/model.ts index 06242b65..f53d6b0e 100644 --- a/api/src/modules/DocumentDrive/model.ts +++ b/api/src/modules/DocumentDrive/model.ts @@ -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) { @@ -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; + }, }; } diff --git a/api/src/modules/DocumentDrive/mutations/addDrive.ts b/api/src/modules/DocumentDrive/mutations/addDrive.ts index 67f0b57a..0bf9e0d7 100644 --- a/api/src/modules/DocumentDrive/mutations/addDrive.ts +++ b/api/src/modules/DocumentDrive/mutations/addDrive.ts @@ -1,6 +1,4 @@ import { mutationField, nonNull, objectType } from 'nexus'; -import { GraphQLJSONObject } from 'graphql-type-json'; -import { scalarType } from 'nexus'; import { DocumentDriveLocalState, diff --git a/api/src/modules/DocumentDrive/mutations/pushUpdates.ts b/api/src/modules/DocumentDrive/mutations/pushUpdates.ts index 1589bd51..64c8a79b 100644 --- a/api/src/modules/DocumentDrive/mutations/pushUpdates.ts +++ b/api/src/modules/DocumentDrive/mutations/pushUpdates.ts @@ -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), @@ -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, + }; + }); }, }); diff --git a/api/src/modules/DocumentDrive/queries/document.ts b/api/src/modules/DocumentDrive/queries/document.ts new file mode 100644 index 00000000..980bfff4 --- /dev/null +++ b/api/src/modules/DocumentDrive/queries/document.ts @@ -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; + } + }, +});