import { withFilter } from 'graphql-subscriptions'; import { PubsubIdentifier, IResolvers, } from '@files-stack/core'; import { IResolverOptions } from '@common-stack/server-core'; export const resolver: (options: IResolverOptions) => IResolvers = (options: IResolverOptions) => ({ Query: { readTextFile(root, args, ctx) { return ctx.dataSources .textFileCache .readCache(args.resource, args.options); }, readStringStreamTextFile(root, args, ctx) { return ctx.textFileService.readStringStream(args.resource, args.options); }, }, Mutation: { writeTextFile(root, args, ctx) { options.logger.debug('updateContent (%s)', args.resource.toString()); return ctx.dataSources .textFileCache .writeCache(args.resource, args.value, args.options); }, writeChunk(root, args, ctx) { options.logger.debug('updateContent (%s)', args.resource.toString()); return ctx.textFileService .setDataCache(ctx.dataSources.textFileCache) .writeChunk(args.resource, args.changes, args.options) as any; // .catch(e => console.log(e)); }, writeChunkWithDelay(root, args, ctx) { options.logger.debug('updateContent (%s)', args.resource.toString()); return ctx.textFileService .setDataCache(ctx.dataSources.textFileCache) .writeChunkWithDelay(args.resource, args.changes, args.options); }, async moveTextFile(root, args, ctx) { options.logger.debug('move file (%s)', args.source.toString(), args.target.toString()); // unwatch the old file path as it is not needed anymore. // ctx.fileService.unwatch(args.source); return await ctx.fileService.move(args.source, args.target, args.overwrite); }, createTextFile(root, args, ctx) { options.logger.debug('create file (%s)', args.resource); return ctx.textFileService.create(args.resource, args.content, { overwrite: args.overwrite }); }, deleteTextFile(root, args, ctx) { options.logger.debug('delete (%j)', args.resource); ctx.dataSources .textFileCache .deleteCache(args.resource, { useTrash: args.useTrash }); // ctx.fileService.unwatch(args.resource); return true; }, readTextFile(root, args, ctx) { return ctx.dataSources .textFileCache .readCache(args.resource, args.options); }, }, Subscription: { // readStreamTextFile: { // subscribe: withFilter(() => options.pubsub.asyncIterator(`${PubsubIdentifier.FILE_CONTENT_STREAM}/${options.subscriptionID}`), // (payload, variables) => { // //TODO options.logger.debug('subscribe readStreamTextFile: (%j), variables: (%j)', payload, variables); // return true; // }), // }, readStreamTextString: { subscribe: () => options.pubsub.asyncIterator(`${PubsubIdentifier.FILE_CONTENT_STREAM}/${options.subscriptionID}`), }, }, });