Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
feat: improved handling of graphql responses and type improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
acaldas committed Sep 20, 2024
1 parent bbb6708 commit aec6371
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 33 deletions.
40 changes: 32 additions & 8 deletions src/server/listener/transmitter/pull-responder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class PullResponderTransmitter implements IPullResponderTransmitter {
filter: ListenerFilter
): Promise<Listener['listenerId']> {
// graphql request to switchboard
const { registerPullResponderListener } = await requestGraphql<{
const result = await requestGraphql<{
registerPullResponderListener: {
listenerId: Listener['listenerId'];
};
Expand All @@ -134,7 +134,17 @@ export class PullResponderTransmitter implements IPullResponderTransmitter {
`,
{ filter }
);
return registerPullResponderListener.listenerId;

const error = result.errors?.at(0);
if (error) {
throw error;
}

if (!result.registerPullResponderListener) {
throw new Error('Error registering listener');
}

return result.registerPullResponderListener.listenerId;
}

static async pullStrands(
Expand All @@ -143,11 +153,7 @@ export class PullResponderTransmitter implements IPullResponderTransmitter {
listenerId: string,
options?: GetStrandsOptions // TODO add support for since
): Promise<StrandUpdate[]> {
const {
system: {
sync: { strands }
}
} = await requestGraphql<PullStrandsGraphQL>(
const result = await requestGraphql<PullStrandsGraphQL>(
url,
gql`
query strands($listenerId: ID!) {
Expand Down Expand Up @@ -188,7 +194,17 @@ export class PullResponderTransmitter implements IPullResponderTransmitter {
`,
{ listenerId }
);
return strands.map(s => ({

const error = result.errors?.at(0);
if (error) {
throw error;
}

if (!result.system) {
return [];
}

return result.system.sync.strands.map(s => ({
...s,
operations: s.operations.map(o => ({
...o,
Expand All @@ -215,6 +231,14 @@ export class PullResponderTransmitter implements IPullResponderTransmitter {
`,
{ listenerId, revisions }
);
const error = result.errors?.at(0);
if (error) {
throw error;
}

if (result.acknowledge === null) {
throw new Error('Error acknowledging strands');
}
return result.acknowledge;
}

Expand Down
6 changes: 3 additions & 3 deletions src/storage/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class MemoryStorage implements IDriveStorage {

async saveDocument(drive: string, id: string, document: Document) {
this.documents[drive] = this.documents[drive] ?? {};
this.documents[drive]![id] = document;
this.documents[drive][id] = document;
}

async clearStorage(): Promise<void> {
Expand All @@ -65,7 +65,7 @@ export class MemoryStorage implements IDriveStorage {
clipboard,
state
} = document;
this.documents[drive]![id] = {
this.documents[drive][id] = {
operations,
initialState,
name,
Expand Down Expand Up @@ -105,7 +105,7 @@ export class MemoryStorage implements IDriveStorage {
if (!this.documents[drive]) {
throw new DriveNotFoundError(drive);
}
delete this.documents[drive]![id];
delete this.documents[drive][id];
}

async getDrives() {
Expand Down
41 changes: 22 additions & 19 deletions src/utils/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,27 @@ export type DriveState = DriveInfo &
nodes: Array<FolderNode | Omit<FileNode, 'synchronizationUnits'>>;
};

export type DocumentGraphQLResult<D extends Document> = Pick<
D,
'name' | 'created' | 'documentType' | 'lastModified'
> & {
id: string;
revision: number;
state: InferDocumentState<D>;
initialState: InferDocumentState<D>;
operations: (Pick<
Operation,
| 'id'
| 'hash'
| 'index'
| 'skip'
| 'timestamp'
| 'type'
| 'error'
| 'context'
> & { inputText: string })[];
};

export async function fetchDocument<D extends Document>(
url: string,
documentId: string,
Expand All @@ -192,25 +213,7 @@ export async function fetchDocument<D extends Document>(
const stateFields = generateDocumentStateQueryFields(documentModel);
const name = pascalCase(documentModel.name);
const result = await requestGraphql<{
document: Pick<
D,
'name' | 'created' | 'documentType' | 'lastModified'
> & {
id: string;
revision: number;
state: InferDocumentState<D>;
initialState: InferDocumentState<D>;
operations: (Pick<
Operation,
| 'id'
| 'hash'
| 'index'
| 'skip'
| 'timestamp'
| 'type'
| 'error'
> & { inputText: string })[];
};
document: DocumentGraphQLResult<D>;
}>(
url,
gql`
Expand Down
6 changes: 3 additions & 3 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from 'document-model/document';
import { DocumentModelDocument } from 'document-model/document-model';
import { ExpectStatic } from 'vitest';
import { DocumentDriveServer } from '../src';
import { BaseDocumentDriveServer } from '../src';

export function expectUUID(expect: ExpectStatic): unknown {
return expect.stringMatching(
Expand Down Expand Up @@ -93,7 +93,7 @@ export class BasicClient {
private unsyncedOperations: Operation[] = [];

constructor(
private server: DocumentDriveServer,
private server: BaseDocumentDriveServer,
private driveId: string,
private documentId: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -166,7 +166,7 @@ export class DriveBasicClient {
[];

constructor(
private server: DocumentDriveServer,
private server: BaseDocumentDriveServer,
private driveId: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private document: Document<any, any, any>,
Expand Down

0 comments on commit aec6371

Please sign in to comment.