Skip to content

Instantly share code, notes, and snippets.

@YonatanKra

YonatanKra/schema.js

Created Oct 23, 2020
Embed
What would you like to do?
covid-19 GraphQL schema
const axios = require('axios').default;
const {
GraphQLSchema,
GraphQLString,
GraphQLObjectType,
GraphQLList,
GraphQLInt
} = require('graphql');
const CovidDataType = new GraphQLObjectType({
name: 'CovidStats',
fields: () => ({
active: {
type: GraphQLInt
},
confirmed: {
type: GraphQLString
},
deaths: {
type: GraphQLString
},
recovered: {
type: GraphQLString
}
})
});
const StateCovidDataType = new GraphQLObjectType({
name: 'statewise',
fields: {
state: {
type: GraphQLString
},
active: {
type: GraphQLString
},
confirmed: {
type: GraphQLString
},
deaths: {
type: GraphQLString
},
recovered: {
type: GraphQLString
}
}
});
const DailyCovidDataType = new GraphQLObjectType({
name: 'daily',
fields: {
date: {
type: GraphQLString
},
dailyconfirmed: {
type: GraphQLInt
},
dailydeceased: {
type: GraphQLInt
},
dailyrecovered: {
type: GraphQLInt
}
}
});
/**
* Total data
*/
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
total: {
type: CovidDataType,
async resolve() {
const data = await axios.get('https://api.covid19india.org/data.json')
.then(res => res.data.statewise[0]);
return data;
}
},
statewise: {
type: new GraphQLList(StateCovidDataType),
async resolve() {
const data = await axios.get('https://api.covid19india.org/data.json')
.then(res => res.data.statewise.splice(1));
return data;
}
},
datewise: {
type: new GraphQLList(DailyCovidDataType),
async resolve() {
const data = await axios.get('https://api.covid19india.org/data.json')
.then(res => res.data.cases_time_series);
return data;
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.