1.0.84 • Published 11 months ago

@webage/resolver v1.0.84

Weekly downloads
-
License
MIT
Repository
-
Last release
11 months ago

resolver

GraphQL Resolver builder

Install

pnpm add @webage/resolver

Examples

import { CreateSampleInput, SampleGraph, QuerySampleInput, UpdateSampleInput } from '@webage/graph';
import { Sample } from '@webage/entity';
import { PubSub } from 'graphql-subscriptions';
import { ResolverBuilder } from '@webage/resolver';
import { OperationNameBuilder } from '@webage/path';
import { IdArg, RelationInput, UnsetRelationInput } from '@webage/graphql';
import { SampleService } from './sample.service';
import { Args } from '@nestjs/graphql';

const productPubSub = new PubSub();

const N = new OperationNameBuilder(Sample.name);
const R = new ResolverBuilder({
  operationBuilder: N,
  graph: SampleGraph,
  createInput: CreateSampleInput,
  updateInput: UpdateSampleInput,
  queryInput: QuerySampleInput,
});

@R.resolver()
export class SampleResolver {
  constructor(protected readonly service: SampleService) {}

  @R.save()
  async save(@R.saveArg() body: CreateSampleInput) {
    const saveSample = await this.service.saveOne(body);
    productPubSub.publish(N.SAVE_MUTATION, { saveSample });
    return saveSample;
  }

  @R.find()
  find(@R.queryArg() query: QuerySampleInput) {
    return this.service.findAll(query);
  }

  @R.findOneById()
  findOneById(@IdArg() id: number) {
    return this.service.findOneById(id);
  }

  @R.delete()
  delete(@IdArg() id: number) {
    productPubSub.publish(N.DELETE_MUTATION, { id });
    return this.service.deleteById(id);
  }

  @R.update()
  update(@IdArg() id: number, @R.updateArg() body: UpdateSampleInput) {
    productPubSub.publish(N.UPDATE_MUTATION, { id, body });
    return this.service.updateOneById(id, body);
  }

  @R.addRelation()
  addRelation(@Args('addRelation', { type: () => RelationInput }) query: RelationInput) {
    return this.service.addRelation(query);
  }

  @R.removeRelation()
  removeRelation(@Args('removeRelation', { type: () => RelationInput }) query: RelationInput) {
    return this.service.removeRelation(query);
  }

  @R.setRelation()
  setRelation(@Args('setRelation', { type: () => RelationInput }) query: RelationInput) {
    return this.service.setRelation(query);
  }

  @R.unsetRelation()
  unsetRelation(
    @Args('unsetRelation', { type: () => UnsetRelationInput })
    query: UnsetRelationInput
  ) {
    return this.service.unsetRelation(query);
  }

  @R.subSave()
  save$() {
    return productPubSub.asyncIterator(N.SAVE_MUTATION);
  }

  @R.subUpdate()
  update$() {
    return productPubSub.asyncIterator(N.UPDATE_MUTATION);
  }

  @R.subDelete()
  delete$() {
    return productPubSub.asyncIterator(N.DELETE_MUTATION);
  }
}
1.0.84

11 months ago

1.0.83

11 months ago

1.0.81

11 months ago

1.0.8

11 months ago

1.0.71

11 months ago

1.0.7

11 months ago

1.0.62

11 months ago

1.0.61

11 months ago

1.0.1

11 months ago