Appearance
CountriesModule
The CountriesModule provides a list of countries with their corresponding codes, flags, and regions. The entity contains the ISO codes in different formats.
Key Components
The module provides a set of reusable components that can be easily integrated into a NestJS project:
Country Entity
The Country entity is a representation of a country that includes information such as the country code in various formats (alpha-2, alpha-3, and numeric), a flag in SVG format, the country's region (e.g., Europe, Asia, etc.), and the country's name in English.
NestJS Countries Service
The CountriesService provides a set of methods to interact with the Countries API. It allows fetching a list of countries and filtering them based on different criteria.
GraphQL Countries Resolver
The NestJS CountriesResolver is a component that handles incoming requests related to countries and retrieves the relevant data using the CountriesService. It defines the GraphQL queries for fetching countries data.
How to install CountriesModule to my app
To create a CountriesModule in NestJS, you can follow these steps:
Create a
countriesfolder in your project'ssrc/modulesdirectory.Inside the
countriesfolder, create acountries.service.tsfile and extendsBaseCountriesServicefrom NestKit with the following code:ts// src/modules/countries/countries.service.ts import { BaseCountriesService } from '@deeepvision/nest-kit/dist/modules/countries'; export class CountriesService extends BaseCountriesService {}Inside the
countriesfolder, create acountries.resolver.tsfile and extendsBaseCountriesResolverwith the following code:ts// src/modules/countries/countries.resolver.ts import { Country, BaseCountriesResolver } from '@deeepvision/nest-kit/dist/modules/countries'; import { Resolver } from '@nestjs/graphql'; @Resolver(() => Country) export class CountriesResolver extends BaseCountriesResolver() {}Inside the
countriesfolder, create acountries.module.tsfile with the following code:ts// src/modules/countries/countries.module.ts import { COUNTRIES_SERVICE_TOKEN } from '@deeepvision/nest-kit/dist/modules/countries'; import { Module } from '@nestjs/common'; import { CountriesResolver } from './countries.resolver'; import { CountriesService } from './countries.service'; @Module({ providers: [ CountriesService, { provide: COUNTRIES_SERVICE_TOKEN, useExisting: CountriesService, }, CountriesResolver, ], exports: [ CountriesService, { provide: COUNTRIES_SERVICE_TOKEN, useExisting: CountriesService, }, ], }) export class CountriesModule {}Finally, import the CountriesModule into your main AppModule or another module:
ts// src/modules/app.module.ts import { Module } from '@nestjs/common'; import { CountriesModule } from './modules/countries/countries.module.ts'; @Module({ imports: [CountriesModule], }) export class AppModule {}In this code, we are importing the
CountriesModuleand adding it to theimportsarray of ourAppModule.
How to get countries list
Use the getMany() method in other parts of your application to retrieve a list of countries.
ts
const [countries, meta] = await this.countriesService.getMany({
filter: {
...
},
orderBy: CountriesOrderBy.title_ASC,
});See CountriesFilter and CountriesOrderBy for more information on how to filter and order countries.