Skip to content

Commit

Permalink
Gotten basics of fetching data for states and U.S. ready
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrubias committed Aug 6, 2020
1 parent b71663f commit 285ee6a
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "covid-tracking-graphql",
"version": "1.0.0",
"version": "0.1.0",
"description": "A GraphQL wrapper for the API at https://covidtracking.com/",
"main": "index.js",
"repository": "https://github.com/Tyrubias/covid-track-graphql",
Expand All @@ -12,7 +12,7 @@
"compile": "babel src -d build",
"format": "prettier --write .",
"lint": "eslint --fix .",
"pkg": "pkg build/index.js -o build/HedwigBackend --options trace-warnings",
"pkg": "pkg build/index.js -o build/COVIDTracking --options trace-warnings",
"refresh": "rm -rf build node_modules && rm package-lock.json",
"start": "nodemon",
"server": "node --trace-warnings build/index.js"
Expand Down
163 changes: 162 additions & 1 deletion src/graphql/StatesModel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { sc } from "graphql-compose";
import moment from "moment";
import UrlTC from "./CommonTypes";

const StateMetadataTC = sc.createObjectTC({
Expand Down Expand Up @@ -50,4 +51,164 @@ const StateDataTC = sc.createObjectTC({
},
});

export { StateDataTC, StateMetadataTC };
StateMetadataTC.addResolver({
name: "findOne",
type: StateMetadataTC,
args: {
stateAbbrev: "String!",
},
resolve: async ({ args, context: { dataSources } }) => {
return dataSources.covidAPI
.getMetadataState(args.stateAbbrev)
.then((data) => ({
covid19Site: data.covid19Site,
covid19SiteOld: data.covid19SiteOld,
covid19SiteSecondary: data.covid19SiteSecondary,
covid19SiteTertiary: data.covid19SiteTertiary,
fips: data.fips,
name: data.fips,
notes: data.notes,
state: data.state,
twitter: data.twitter,
}));
},
})
.addResolver({
name: "findMany",
type: [StateMetadataTC],
resolve: async ({ context: { dataSources } }) => {
return dataSources.covidAPI.getStatesMetadata().then((allData) =>
allData.map((data) => ({
covid19Site: data.covid19Site,
covid19SiteOld: data.covid19SiteOld,
covid19SiteSecondary: data.covid19SiteSecondary,
covid19SiteTertiary: data.covid19SiteTertiary,
fips: data.fips,
name: data.fips,
notes: data.notes,
state: data.state,
twitter: data.twitter,
})),
);
},
})
.addResolver({
name: "count",
type: "Int!",
resolve: async ({ context: { dataSources } }) => {
return dataSources.covidAPI
.getStatesMetadata()
.then((allData) => allData.length);
},
});

StateDataTC.addResolver({
name: "findOne",
type: StateDataTC,
args: {
stateAbbrev: "String!",
date: "Date!",
},
resolve: async ({ args, context: { dataSources } }) => {
return dataSources.covidAPI
.getStateOnDate(args.stateAbbrev, args.date)
.then((data) => ({
dataQualityGrade: data.dataQualityGrade,
date: moment(data.date, "YYYYMMDD").toDate(),
death: data.death,
deathConfirmed: data.deathConfirmed,
deathIncrease: data.deathIncrease,
deathProbable: data.deathProbable,
fips: data.fips,
hospitalizedCumulative: data.hospitalizedCumulative,
hospitalizedCurrently: data.hospitalizedCurrently,
hospitalizedIncrease: data.hospitalizedIncrease,
inIcuCumulative: data.inIcuCumulative,
inIcuCurrently: data.inIcuCurrently,
lastUpdateEt: moment(data.lastUpdateEt, [
"M/D/YYYY HH:mm",
"MM/DD/YYYY HH:mm",
]).toDate(),
negative: data.negative,
negativeTestsViral: data.negativeTestsViral,
onVentilatorCumulative: data.onVentilatorCumulative,
onVentilatorCurrently: data.onVentilatorCurrently,
pending: data.pending,
positiveCasesViral: data.positiveCasesViral,
positiveIncrease: data.positiveIncrease,
positiveTestsViral: data.positiveTestsViral,
recovered: data.recovered,
state: data.state,
totalTestResults: data.totalTestResults,
totalTestResultsIncrease: data.totalTestResultsIncrease,
totalTestViral: data.totalTestViral,
}));
},
})
.addResolver({
name: "findMany",
type: [StateDataTC],
args: {
stateAbbrev: "String!",
},
resolve: async ({ args, context: { dataSources } }) => {
return dataSources.covidAPI
.getHistoricForState(args.stateAbbrev)
.then((allData) =>
allData.map((data) => ({
dataQualityGrade: data.dataQualityGrade,
date: moment(data.date, "YYYYMMDD").toDate(),
death: data.death,
deathConfirmed: data.deathConfirmed,
deathIncrease: data.deathIncrease,
deathProbable: data.deathProbable,
fips: data.fips,
hospitalizedCumulative: data.hospitalizedCumulative,
hospitalizedCurrently: data.hospitalizedCurrently,
hospitalizedIncrease: data.hospitalizedIncrease,
inIcuCumulative: data.inIcuCumulative,
inIcuCurrently: data.inIcuCurrently,
lastUpdateEt: moment(data.lastUpdateEt, [
"M/D/YYYY HH:mm",
"MM/DD/YYYY HH:mm",
]).toDate(),
negative: data.negative,
negativeTestsViral: data.negativeTestsViral,
onVentilatorCumulative: data.onVentilatorCumulative,
onVentilatorCurrently: data.onVentilatorCurrently,
pending: data.pending,
positiveCasesViral: data.positiveCasesViral,
positiveIncrease: data.positiveIncrease,
positiveTestsViral: data.positiveTestsViral,
recovered: data.recovered,
state: data.state,
totalTestResults: data.totalTestResults,
totalTestResultsIncrease: data.totalTestResultsIncrease,
totalTestViral: data.totalTestViral,
})),
);
},
})
.addResolver({
name: "count",
type: "Int!",
resolve: async ({ args, context: { dataSources } }) => {
return dataSources.covidAPI
.getHistoricForState(args.stateAbbrev)
.then((allData) => allData.length);
},
});

const StateMetadataQueries = {
metaOne: StateMetadataTC.getResolver("findOne"),
metaMany: StateMetadataTC.getResolver("findMany"),
metaCount: StateMetadataTC.getResolver("count"),
};

const StateDataQueries = {
stateOne: StateDataTC.getResolver("findOne"),
stateMany: StateDataTC.getResolver("findMany"),
stateCount: StateDataTC.getResolver("count"),
};

export { StateDataTC, StateMetadataTC, StateMetadataQueries, StateDataQueries };
8 changes: 0 additions & 8 deletions src/graphql/USModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,12 @@ USDataTC.addResolver({
.getHistoricUS()
.then((allData) => allData.length);
},
})
.addResolver({
name: "findCurrent",
type: USDataTC,
resolve: async ({ context: { dataSources } }) => {
return dataSources.covidAPI.getCurrentUS();
},
});

const USQueries = {
usFindOne: USDataTC.getResolver("findOne"),
usFindMany: USDataTC.getResolver("findMany"),
usCount: USDataTC.getResolver("count"),
usCurrent: USDataTC.getResolver("findCurrent"),
};

export { USDataTC, USQueries };
3 changes: 3 additions & 0 deletions src/graphql/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { sc } from "graphql-compose";
import { USQueries } from "./USModel";
import { StateDataQueries, StateMetadataQueries } from "./StatesModel";

sc.Query.addFields({
...USQueries,
...StateDataQueries,
...StateMetadataQueries,
});

const Schema = sc.buildSchema();
Expand Down

0 comments on commit 285ee6a

Please sign in to comment.