@nestjs/graphql#TypeMetadataStorage TypeScript Examples

The following examples show how to use @nestjs/graphql#TypeMetadataStorage. 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: loaders-explorer.service.ts    From nestjs-mercurius with MIT License 6 votes vote down vote up
/**
   * Get a map of all Interfaces and their ObjectType implementations
   * @private
   */
  private getInterfacesImplementations(): Record<string, Set<string>> {
    return TypeMetadataStorage.getObjectTypesMetadata().reduce((acc, curr) => {
      getInterfacesArray(curr.interfaces).forEach((interfaceTarget) => {
        const int = TypeMetadataStorage.getInterfaceMetadataByTarget(
          interfaceTarget as Type<unknown>,
        );
        if (!acc[int.name]) {
          acc[int.name] = new Set();
        }
        acc[int.name].add(curr.name);
      });
      return acc;
    }, {} as Record<string, Set<string>>);
  }
Example #2
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 #3
Source File: loaders-explorer.service.ts    From nestjs-mercurius with MIT License 5 votes vote down vote up
private getObjectTypeMetadataByName(
    name: string,
  ): ObjectTypeMetadata | undefined {
    return TypeMetadataStorage.getObjectTypesMetadata().find(
      (m) => m.name === name,
    );
  }