Problem with multiple graphql resolver implementations in nestjs - node.js

I just started learning NestJS and GraphQL. I started with a single resolver class UserResolver defined in UserModule. This class provides methods to read a list of users or a specific user. Methods are decorated with #Query(), a user.graphqlfile is provided, GraphQL is initialized in AppModule, all as described in the docs. All works well, I can get a list of users or specific user through Insomnia Tool or through Playground. I am happy!
Now I have created a second module, RoleModule. I created a role.graphql file and a RoleResolver class, I basically replicated all the work done for User but this time for Role. The GraphQL type definition for type Role as well as the Query definitions in the role.graphql file are recognized. What is not recognized are my Query() implementations in the RoleResolver class, they are not getting invoked.
If I put all these Role related #Query() definitions into the UserResolver class, these Role related queries are now getting invoked.
Is that expected behavior? Do I need to put all my GraphQL query definitions into a single class? Is it possible to spread NestJS-GraphQL resolver implementations over several modules? Am I doing something wrong? Please help.

Make sure that you import Query from #nestjs/graphql and not from #nestjs/common.

Related

Node.js express app architecture with testing

Creating new project with auto-testing feature.
It uses basic express.
The question is how to orginize the code in order to be able to test it properly. (with mocha)
Almost every controller needs to have access to the database in order to fetch some data to proceed. But while testing - reaching the actual database is unwanted.
There are two ways as I see:
Stubbing a function, which intends to read/write from/to database.
Building two separate controller builders, one of each will be used to reach it from the endpoints, another one from tests.
just like that:
let myController = new TargetController(AuthService, DatabaseService...);
myController.targetMethod()
let myTestController = new TargetController(FakeAuthService, FakeDatabaseService...);
myTestController.targetMethod() // This method will use fake services which doesnt have any remote connection functionality
Every property passed will be set to a private variable inside the constructor of the controller. And by aiming to this private variable we could not care about what type of call it is. Test or Production one.
Is that a good approach of should it be remade?
Alright, It's considered to be a good practice as it is actually a dependency injection pattern

what is getRepositoryToken in nestjs typeorm and when to use it?

The docs here are as below:
When it comes to unit testing an application, we usually want to avoid
making a database connection, keeping our test suites independent and
their execution process as fast as possible. But our classes might
depend on repositories that are pulled from the connection instance.
How do we handle that? The solution is to create mock repositories. In
order to achieve that, we set up custom providers. Each registered
repository is automatically represented by a Repository
token, where EntityName is the name of your entity class.
The #nestjs/typeorm package exposes the getRepositoryToken() function
which returns a prepared token based on a given entity.
What does that even mean? Autocomplete docs just give the signature with no explanation.
getRepositoryToken() is a helper method that allows you to get the same injection token that #InjectRepository() returns. This is useful when it comes to tests so that you can define a custom provider that has a matching token for the DI resolution, and so you can provide a mock of the Repository methods without the need to actually talk to the database. So for example, if you have
#Injectable()
export class FooService {
constructor(#InjectRepository(Foo) private readonly fooRepo: Repository<Foo>) {}
}
In your test you can add the provider
{
provide: getRepositoryToken(Foo),
useValue: {
find: jest.fn(),
insert: jest.fn(),
},
}
And now you've got a mock injectable provider for the Repository.
The biggest reason that things have to be done this way is because typescript doesn't reflect generic classes, it only reflects Repository, and if Nest tries to figure out which repository you mean to inject, with just that name (Repository) it's most likely going to get it wrong and inject the wrong class. Using #InjectRepsitory() allows for setting the proper injection token.

Jest, NestJS, TypeORM End To End Testing

In the NestJS tutorial E2E tests are set up with their single example module imported.
This pattern does not seem to work in an application with more complex relations between the typeORM entities. After extensively checking that there were no inconsistencies in import statements and no missing TypeOrmModule.forFeature() in the relevant files, I gave up and simply imported my entire AppModule (root module for my application -- same as NestJS default).
In short if you are seeing of the form
Error: Entity metadata for EntityA#entityB was not found.
Check if you specified a correct entity object and if it's connected in the connection options.
You can try importing all the related modules or simply import your entire application.

How to use NESTJS modules in separate classes

Sometimes I need to use some methods which implemented in nestjs code structure in separate from this classes
For example we have such architecture:
entities
-entity.entity.ts
-entity.module.ts
-entity.service.ts
-entity.providers.ts
So how Can I use method or property from entity.service in separate class? Such like this:
import EntityService from './entities.entity.service'
export class SeparateClass{
propertyFromEntityService: string
constructor() {
this.propertyFromEntityService = EntityService.propertyFromEntityService
}
}
And one more important point. I don’t want to implement this separate class in the nestjs structure, I just want to use it as a regular class. Thank you
If you are not wanting to use dependency injection via Nest, but you still want to use a property from a class in the Nest application, you'll need to manually instantiate the class, providing whatever dependencies that class has, and then pulling the property from the service. The other option, if this class will be used outside of the Nest context, but still while the Nest application is running, is you can use the Nest application to get the service via app.get(EntityService), but this does require you to have access to app after the NestFactory has created the application.

use functions inside of defaultscope in sequelize

I'm using Sequelize for a project with a single db for multiple applications. I need to query MSSQL database for each request based on the application_id I receive in the request. (on Nodejs)
Calling a function inside of scope in Sequelize model definition requires specifying the name of the scope for each query which is not desired.
I think if I could use functions inside of defaultscope to make it work (without manually overriding it again and again) . Or if there's any other way to accomplish this (better without rewriting Sequelize functions). Thank you!

Resources