APIUtil.tsx 920 Bytes
Newer Older
Mikkel Marstein's avatar
Mikkel Marstein committed
1
2
3
4
5
import React from 'react';
import axios, {AxiosResponse} from 'axios';
import { Country } from '../interfaces/Country';

export function getData(): Promise<Country[]> {
6
    return axios.get('http://localhost:3001/graphql?query={country{_id, name, native, continent, capital, currency, languages, favorite}}')
Mikkel Marstein's avatar
Mikkel Marstein committed
7
8
9
10
11
        .then((response: AxiosResponse) => {
            console.log(response.data);
            console.log(response.data.data.country[0]);
            return response.data.data.country as Country[]
        });
12
13
}

14
15
16
17
18
19
20
21
22
23
24
25
26
27
export function saveData(_id:String): Promise<Country> {
    let config = {
        headers: {
          "Content-Type": "application/json",
          'Access-Control-Allow-Origin': '*',
          }
        }
    return axios.post('http://localhost:3001/graphql?', 
    {query: `mutation {
            updateFavorite(input:{_id:"${_id}"}) 
            {name, favorite}
        }`},
    config);
}