gql-country-service
v0.0.3
Published
This Angular library can be used to query the GraphQL API for [GraphQL Country](https://graphql.country/). GraphQL Country is a public API for retrieving data about countries.
Downloads
3
Readme
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.