@nestjs/graphql#ReturnTypeFunc TypeScript Examples

The following examples show how to use @nestjs/graphql#ReturnTypeFunc. 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: resolve-connection-field.decorator.ts    From nestjs-relay with MIT License 6 votes vote down vote up
export function ResolveConnectionField(
  nodeTypeFunc: ReturnTypeFunc,
  options?: ResolveConnectionFieldOptions,
): MethodDecorator {
  return (target: Record<string, any>, key: string | symbol, descriptor: PropertyDescriptor) => {
    // eslint-disable-next-line @typescript-eslint/ban-types
    const nodeType = nodeTypeFunc() as Function;
    const typeMetadata = MetadataStorage.getClassMetadata({ target: nodeType });

    const connection = ConnectionTypeFactory.create({
      nodeTypeFunc,
      nodeTypeName: typeMetadata.name,
    });

    const resolveFieldOptions = { ...options, nullable: true };
    ResolveField(() => connection, resolveFieldOptions)(target, key, descriptor);
  };
}
Example #2
Source File: input-arg.decorator.ts    From nestjs-relay with MIT License 6 votes vote down vote up
export function InputArg<T>(
  typeFunc: ReturnTypeFunc,
  options?: InputArgOptions,
): ParameterDecorator {
  // eslint-disable-next-line @typescript-eslint/ban-types
  return (target: Object, key: string | symbol, paramIndex: number) => {
    MetadataStorage.addMethodMetadata({
      ...options,
      typeFunc,
      target,
      key,
      paramIndex,
    });
  };
}
Example #3
Source File: resolve-loader.decorator.ts    From nestjs-mercurius with MIT License 5 votes vote down vote up
export function ResolveLoader(
  propertyNameOrFunc?: string | ReturnTypeFunc,
  typeFuncOrOptions?: ReturnTypeFunc | ResolveLoaderOptions,
  resolveFieldOptions?: ResolveLoaderOptions,
): MethodDecorator {
  return (
    target: Function | Record<string, any>,
    key: any,
    descriptor?: any,
  ) => {
    // eslint-disable-next-line prefer-const
    let [propertyName, typeFunc, options] = isFunction(propertyNameOrFunc)
      ? typeFuncOrOptions && typeFuncOrOptions.name
        ? [typeFuncOrOptions.name, propertyNameOrFunc, typeFuncOrOptions]
        : [undefined, propertyNameOrFunc, typeFuncOrOptions]
      : [propertyNameOrFunc, typeFuncOrOptions, resolveFieldOptions];
    SetMetadata(LOADER_NAME_METADATA, propertyName)(target, key, descriptor);
    SetMetadata(LOADER_PROPERTY_METADATA, true)(target, key, descriptor);

    SetMetadata(
      FIELD_RESOLVER_MIDDLEWARE_METADATA,
      (options as ResolveLoaderOptions)?.middleware,
    )(target, key, descriptor);

    options = isObject(options)
      ? {
          name: propertyName as string,
          ...options,
        }
      : propertyName
      ? { name: propertyName as string }
      : {};

    LazyMetadataStorage.store(
      target.constructor as Type<unknown>,
      function resolveLoader() {
        let typeOptions: TypeOptions, typeFn: (type?: any) => GqlTypeReference;
        try {
          const implicitTypeMetadata = reflectTypeFromMetadata({
            metadataKey: 'design:returntype',
            prototype: target,
            propertyKey: key,
            explicitTypeFn: typeFunc as ReturnTypeFunc,
            typeOptions: options as any,
          });
          typeOptions = implicitTypeMetadata.options;
          typeFn = implicitTypeMetadata.typeFn;
        } catch {}

        TypeMetadataStorage.addResolverPropertyMetadata({
          kind: 'external',
          methodName: key,
          schemaName: options.name || key,
          target: target.constructor,
          typeFn,
          typeOptions,
          description: (options as ResolveLoaderOptions).description,
          deprecationReason: (options as ResolveLoaderOptions)
            .deprecationReason,
          complexity: (options as ResolveLoaderOptions).complexity,
        });
      },
    );
  };
}
Example #4
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);
  };
}