Skip to content

Commit

Permalink
fix(graphql): use POST-based fetchGraphQL for feed entity fetching
Browse files Browse the repository at this point in the history
fixes #211
  • Loading branch information
landonreed committed Jul 11, 2018
1 parent 30f11ba commit aafdd8b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 33 deletions.
1 change: 1 addition & 0 deletions lib/common/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fetch from 'isomorphic-fetch'

import {GTFS_GRAPHQL_PREFIX} from '../../common/constants'
import { setErrorMessage } from '../../manager/actions/status'

Expand Down
22 changes: 11 additions & 11 deletions lib/gtfs/actions/patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import {createAction} from 'redux-actions'

import {secureFetch} from '../../common/actions'
import {fetchGraphQL} from '../../common/actions'
import {compose, patterns} from '../../gtfs/util/graphql'
import {updateRouteFilter, updatePatternFilter} from './filter'

Expand Down Expand Up @@ -32,17 +32,17 @@ export function fetchPatterns (namespace: string, routeId: ?string) {
}
const {date} = getState().gtfs.filter.dateTimeFilter
return dispatch(
secureFetch(compose(patterns, {namespace, routeId, date}))
fetchGraphQL({query: patterns, variables: {namespace, routeId, date}})
)
.then(response => {
if (response.status >= 300) {
return dispatch(errorFetchingPatterns())
}
return response.json()
})
.then(json => {
if (json.data) {
const {routes} = json.data.feed
// .then(response => {
// if (response.status >= 300) {
// return dispatch(errorFetchingPatterns())
// }
// return response.json()
// })
.then(data => {
if (data) {
const {routes} = data.feed
dispatch(receivePatterns({routes}))
} else {
console.log('Error fetching patterns')
Expand Down
28 changes: 12 additions & 16 deletions lib/gtfs/actions/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import {createAction} from 'redux-actions'

import {secureFetch} from '../../common/actions'
import {fetchGraphQL} from '../../common/actions'
import {updateRouteOffset} from './filter'
import {allRoutes, compose, routeDetails} from '../../gtfs/util/graphql'

Expand Down Expand Up @@ -33,23 +33,20 @@ export function fetchRouteDetails (namespace: string) {
} = state.gtfs.filter
dispatch(fetchingRouteDetails(namespace))
return dispatch(
secureFetch(
compose(routeDetails, {
date: dateTimeFilter.date,
namespace,
routeLimit,
routeOffset
})
)
fetchGraphQL({query: routeDetails, variables: {
date: dateTimeFilter.date,
namespace,
routeLimit,
routeOffset
}})
)
.then(response => response.json())
.then(json => {
.then(data => {
const {
routes,
row_counts: {
routes: numRoutes
}
} = json.data.feed
} = data.feed
dispatch(receiveRouteDetails({numRoutes, routes}))
})
}
Expand All @@ -58,10 +55,9 @@ export function fetchRouteDetails (namespace: string) {
export function fetchRoutes (namespace: string) {
return function (dispatch: dispatchFn, getState: getStateFn) {
dispatch(fetchingRoutes(namespace))
return dispatch(secureFetch(compose(allRoutes, { namespace })))
.then(response => response.json())
.then(json => {
dispatch(receiveRoutes(json.data.feed.routes))
return dispatch(fetchGraphQL({query: allRoutes, variables: {namespace}}))
.then(data => {
dispatch(receiveRoutes(data.feed.routes))
})
}
}
Expand Down
9 changes: 4 additions & 5 deletions lib/gtfs/actions/timetables.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import {createAction} from 'redux-actions'

import {secureFetch} from '../../common/actions'
import {fetchGraphQL} from '../../common/actions'
import {
updateArrivalDisplay,
updatePatternFilter,
Expand Down Expand Up @@ -39,10 +39,9 @@ export function fetchTimetablesWithFilters (namespace: string) {
return dispatch(receiveTimetables({data: {trips: []}}))
}
dispatch(fetchingTimetables())
const params = {date, from, namespace, patternId, routeId, to}
dispatch(secureFetch(compose(timetables, params)))
.then(response => response.json())
.then(json => dispatch(receiveTimetables({data: json.data})))
const variables = {date, from, namespace, patternId, routeId, to}
return dispatch(fetchGraphQL({query: timetables, variables}))
.then(data => dispatch(receiveTimetables({data})))
}
}

Expand Down
8 changes: 7 additions & 1 deletion lib/gtfs/util/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

import {GTFS_GRAPHQL_PREFIX} from '../../common/constants'

// variable names/keys must match those specified in GraphQL schema
/**
* Construct GET GraphQL request URL. NOTE: variable names/keys must match those
* specified in GraphQL schema.
*
* @deprecated Request can become too large and generate a 413 'request entity
* too large' server error. Use lib/common/actions#fetchGraphQL instead.
*/
export function compose (query: string, variables: Object) {
return `${GTFS_GRAPHQL_PREFIX}?query=${encodeURIComponent(query)}&variables=${encodeURIComponent(JSON.stringify(variables))}`
}
Expand Down

0 comments on commit aafdd8b

Please sign in to comment.