Created
October 23, 2020 13:03
-
-
Save YonatanKra/3f9adc62954939927ccb577467534d96 to your computer and use it in GitHub Desktop.
covid-19 GraphQL schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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