Undefined type error. Make sure you are providing an explicit type for... After updating the packages

126 views Asked by At

I've been using nestjs with graphql for quite some time and have encountered this error, luckily it's usually an easy fix. But after updating the project dependencies, this error began to appear on all fields of all models where the type was not specified in the Field decorator.

Error:

/usr/src/app/node_modules/@nestjs/graphql/dist/utils/reflection.utilts.js:17
            throw new undefined_type_error_1.UndefinedTypeError((0, lodash_1.get)(prototype, 'constructor.name'), propertyKey, index);
                  ^
Error: Undefined type error. Make sure you are providing an explicit type for the "value" of the "ServiceHistoryModel" class.
    at reflectTypeFromMetadata (/usr/src/app/node_modules/@nestjs/graphql/dist/utils/reflection.utilts.js:17:19)
    at applyMetadataFn (/usr/src/app/node_modules/@nestjs/graphql/dist/decorators/field.decorator.js:31:107)
    at singleCallFunctionWrapper (/usr/src/app/node_modules/@nestjs/graphql/dist/schema-builder/storages/lazy-metadata.storage.js:67:13)
    at /usr/src/app/node_modules/@nestjs/graphql/dist/schema-builder/storages/lazy-metadata.storage.js:41:44
    at Array.forEach (<anonymous>)
    at LazyMetadataStorageHost.load (/usr/src/app/node_modules/@nestjs/graphql/dist/schema-builder/storages/lazy-metadata.storage.js:41:26)
    at GraphQLSchemaFactory.create (/usr/src/app/node_modules/@nestjs/graphql/dist/schema-builder/graphql-schema.factory.js:35:53)
    at GraphQLSchemaBuilder.generateSchema (/usr/src/app/node_modules/@nestjs/graphql/dist/graphql-schema.builder.js:35:52)
    at GraphQLSchemaBuilder.build (/usr/src/app/node_modules/@nestjs/graphql/dist/graphql-schema.builder.js:22:31)
    at GraphQLFactory.generateSchema (/usr/src/app/node_modules/@nestjs/graphql/dist/graphql.factory.js:27:69)

ServiceHistoryModel:

@ObjectType()
export class ServiceHistoryModel {
  @Field(() => ID)
  id: string;

  @Field()
  value: string;

  @Field({ nullable: true })
  date?: Date;
}

This file did not change for about a month, and during this time the backend was deployed a bunch of times on local and production.

If I put () => String in the FIeld decorator, this error goes away. But this means that I will have to specify types for all fields, all models. Previously, everything worked fine if the field had a readable type, like string or Date. I reverted dependencies to previous versions, but that didn't help. I also tried deleting everything and installing it again. It looks like an internal dependency of one of the packages has broken. How can I fix this?

Edit: I found the previous version of package-lock.json, restored it and installed all the packages again via npm ci, this fixed the problem. But I still have no idea which package broke everything. When uploading to the server, the dependencies will be installed again, so simply using the old lock file is not a solution.

0

There are 0 answers