gql-country-service
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

GraphQL Country Service

This Angular library can be used to query the GraphQL API for GraphQL Country. GraphQL Country is a public API for retrieving data about countries.

Usage

npm install gql-country-service

Import GqlCountryServiceModule into your desired module.

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { GqlCountryServiceModule } from 'gql-country-service';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    GqlCountryServiceModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Inject GqlCountryService in the constructor where it will be used.

app.component.ts

import { Component, OnInit } from "@angular/core";
import { GqlCountryService } from "gql-country-service";
import { CountryNodeEdge } from "projects/gql-country-service/src/public-api";
import gql from "graphql-tag";

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.scss"]
})
export class AppComponent implements OnInit {
  title = "graphql-country-service";

  countries: CountryNodeEdge[];

  loading: boolean;

  constructor(private graphqlCountryService: GqlCountryService) {}

  ngOnInit() {
    this.loading = true;

    this.graphqlCountryService
      .queryCountries()
      .valueChanges.subscribe(({ data, loading }) => {
        this.loading = loading;
        this.countries = data.countries.edges;
        console.log(this.countries);
      });
  }
}

Use graphql-tag to create queries to pass as parameters to .queryCountries() or .queryCountry().

queries.ts

import gql from "graphql-tag";

export const GET_COUNTRIES = {
    gql: gql`query{
        countries {
          edges {
            node {
              name
              alpha2Code
            }
          }
        }
      }
      `
  };

export const GET_COUNTRY = {
    gql: gql`query ($id: ID!) {
        country(id: $id) {
          name
          alpha2Code
        }
      }`
  };
graphqlCountryService.queryCountries(GET_COUNTRIES.gql)

Create objects to pass as variables.

let variables = {
    id: 'qwefweEFObwefb323'
}

graphqlCountryService.queryCountries(GET_COUNTRY.gql, variables)

Check GraphQL Country website for more details on the API. Also see GraphQL Country GraphiQL playground.

More Coming Soon...

Readme

Keywords

none

Package Sidebar

Install

npm i gql-country-service

Weekly Downloads

2

Version

0.0.3

License

none

Unpacked Size

197 kB

Total Files

32

Last publish

Collaborators

  • kingpinapps