@nestjs/graphql#MutationOptions TypeScript Examples

The following examples show how to use @nestjs/graphql#MutationOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: relay-mutation.decorator.ts    From nestjs-relay with MIT License 5 votes vote down vote up
export function RelayMutation<T>(
  typeFunc: ReturnTypeFunc,
  options?: RelayMutationOptions,
): MethodDecorator {
  return (target: Record<string, any>, key: string | symbol, descriptor: PropertyDescriptor) => {
    const mutationName = options?.name ? options.name : String(key);

    /**
     * Resolver Interceptor
     */
    const originalMethod = descriptor.value;
    descriptor.value = async function (...args: any[]) {
      const clientMutationId = getClientMutationId(args);
      const methodResult = await ensurePromise(originalMethod.apply(this, args));
      return { ...methodResult, clientMutationId };
    };

    /**
     * Input Type
     */
    const params = MetadataStorage.getMethodMetadata({ target, key });
    const { paramIndex, ...argOptions } = InputArgFactory.create({ params, mutationName });
    const inputArgOptions = {
      name: 'input',
      nullable: false,
      ...argOptions,
    };
    Args(inputArgOptions)(target, key, paramIndex);

    /**
     * Payload Type
     */
    const payloadType = PayloadTypeFactory.create({ typeFunc, mutationName });
    const mutationOptions: MutationOptions = {
      ...options,
      name: mutationName,
      nullable: true,
    };
    Mutation(() => payloadType, mutationOptions)(target, key, descriptor);
  };
}