Skip to content

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:

  1. Create a countries folder in your project's src/modules directory.

  2. Inside the countries folder, create a countries.service.ts file and extends BaseCountriesService from 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 {}
  3. Inside the countries folder, create a countries.resolver.ts file and extends BaseCountriesResolver with 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() {}
  4. Inside the countries folder, create a countries.module.ts file 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 {}
  5. 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 CountriesModule and adding it to the imports array of our AppModule.

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.

Fetch country list with GraphQL ​

Here's an example GraphQL query to fetch countries.

Created by DeepVision Software.