@nestjs/graphql#GqlTypeReference TypeScript Examples

The following examples show how to use @nestjs/graphql#GqlTypeReference. 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-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,
        });
      },
    );
  };
}