Skip to content

Instantly share code, notes, and snippets.

@YonatanKra
Created October 23, 2020 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YonatanKra/3f9adc62954939927ccb577467534d96 to your computer and use it in GitHub Desktop.
Save YonatanKra/3f9adc62954939927ccb577467534d96 to your computer and use it in GitHub Desktop.
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