Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript: GraphQL queries #4621

Draft
wants to merge 17 commits into
base: v3
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: update relay to v14
Joona Ojapalo committed Aug 26, 2022
commit b02b1200cc8046f73315ababc3bd2e89e074f00b
11 changes: 9 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
const fs = require('fs');
const path = require('path');

const graphqlSchemaString = fs.readFileSync(
path.resolve(__dirname, './build/schema.graphql'),
{ encoding: 'utf-8' },
);

module.exports = {
parser: 'babel-eslint',
extends: [
@@ -48,8 +56,7 @@ module.exports = {
'error',
{
env: 'relay',
// eslint-disable-next-line global-require
schemaJson: require('./build/schema.json').data,
schemaString: graphqlSchemaString,
tagName: 'graphql',
},
],
4 changes: 3 additions & 1 deletion app/component/MessageBar.js
Original file line number Diff line number Diff line change
@@ -54,7 +54,9 @@ const fetchServiceAlerts = async (feedids, relayEnvironment) => {
}
`;

const result = await fetchQuery(relayEnvironment, query, { feedids });
const result = await fetchQuery(relayEnvironment, query, {
feedids,
}).toPromise();
return result && Array.isArray(result.alerts) ? result.alerts : [];
};

15 changes: 10 additions & 5 deletions app/component/RoutePatternSelect.js
Original file line number Diff line number Diff line change
@@ -180,12 +180,17 @@ class RoutePatternSelect extends Component {
const params = { name: searchSimilarTo, mode: route.mode };
fetchQuery(this.props.relayEnvironment, query, params, {
force: true,
}).then(results => {
this.setState({
similarRoutes: filterSimilarRoutes(results.routes, this.props.route),
loadingSimilar: false,
})
.toPromise()
.then(results => {
this.setState({
similarRoutes: filterSimilarRoutes(
results.routes,
this.props.route,
),
loadingSimilar: false,
});
});
});
}
};

1 change: 0 additions & 1 deletion app/component/StopAlertsContainer.js
Original file line number Diff line number Diff line change
@@ -45,7 +45,6 @@ const containerComponent = createFragmentContainer(StopAlertsContainer, {
@argumentDefinitions(
startTime: { type: "Long" }
timeRange: { type: "Int", defaultValue: 900 }
date: { type: "String" }
) {
routes {
gtfsId
6 changes: 2 additions & 4 deletions app/component/StopsNearYouFavouritesContainer.js
Original file line number Diff line number Diff line change
@@ -89,8 +89,7 @@ const refetchContainer = createFragmentContainer(
{
stops: graphql`
fragment StopsNearYouFavouritesContainer_stops on Stop
@relay(plural: true)
@argumentDefinitions(startTime: { type: "Long!", defaultValue: 0 }) {
@relay(plural: true) {
...StopNearYouContainer_stop
gtfsId
lat
@@ -99,8 +98,7 @@ const refetchContainer = createFragmentContainer(
`,
stations: graphql`
fragment StopsNearYouFavouritesContainer_stations on Stop
@relay(plural: true)
@argumentDefinitions(startTime: { type: "Long!", defaultValue: 0 }) {
@relay(plural: true) {
...StopNearYouContainer_stop
gtfsId
lat
4 changes: 2 additions & 2 deletions app/component/StopsNearYouPage.js
Original file line number Diff line number Diff line change
@@ -454,7 +454,7 @@ class StopsNearYouPage extends React.Component {
$first: Int!
$maxResults: Int!
$maxDistance: Int!
$omitNonPickups: Boolean
$omitNonPickups: Boolean!
$feedIds: [String!]
) {
stopPatterns: viewer {
@@ -740,7 +740,7 @@ class StopsNearYouPage extends React.Component {
$first: Int!
$maxResults: Int!
$maxDistance: Int!
$omitNonPickups: Boolean
$omitNonPickups: Boolean!
$prioritizedStopIds: [String!]!
) {
stops: viewer {
203 changes: 101 additions & 102 deletions app/component/SummaryPage.js
Original file line number Diff line number Diff line change
@@ -941,6 +941,7 @@ class SummaryPage extends React.Component {
);

fetchQuery(this.props.relayEnvironment, query, planParams)
.toPromise()
.then(result => {
this.setState(
{
@@ -1081,21 +1082,23 @@ class SummaryPage extends React.Component {
);
fetchQuery(this.props.relayEnvironment, query, planParams, {
force: true,
}).then(({ plan: results }) => {
this.setState(
{
alternativePlan: results,
earlierItineraries: [],
laterItineraries: [],
separatorPosition: undefined,
},
() => {
this.setLoading(false);
this.isFetching = false;
this.setParamsAndQuery();
},
);
});
})
.toPromise()
.then(({ plan: results }) => {
this.setState(
{
alternativePlan: results,
earlierItineraries: [],
laterItineraries: [],
separatorPosition: undefined,
},
() => {
this.setLoading(false);
this.isFetching = false;
this.setParamsAndQuery();
},
);
});
};

onLater = (itineraries, reversed) => {
@@ -1149,45 +1152,43 @@ class SummaryPage extends React.Component {

this.setModeToParkRideIfSelected(tunedParams);

fetchQuery(
this.props.relayEnvironment,
moreItinerariesQuery,
tunedParams,
).then(({ plan: result }) => {
this.showScreenreaderLoadedAlert();
if (reversed) {
const reversedItineraries = result.itineraries
.slice() // Need to copy because result is readonly
.reverse()
.filter(
itinerary => !itinerary.legs.every(leg => leg.mode === 'WALK'),
);
// We need to filter only walk itineraries out to place the "separator" accurately between itineraries
this.setState(prevState => {
return {
earlierItineraries: [
...reversedItineraries,
...prevState.earlierItineraries,
],
loadingMoreItineraries: undefined,
separatorPosition: prevState.separatorPosition
? prevState.separatorPosition + reversedItineraries.length
: reversedItineraries.length,
};
});
this.resetSummaryPageSelection();
} else {
this.setState(prevState => {
return {
laterItineraries: [
...prevState.laterItineraries,
...result.itineraries,
],
loadingMoreItineraries: undefined,
};
});
}
/*
fetchQuery(this.props.relayEnvironment, moreItinerariesQuery, tunedParams)
.toPromise()
.then(({ plan: result }) => {
this.showScreenreaderLoadedAlert();
if (reversed) {
const reversedItineraries = result.itineraries
.slice() // Need to copy because result is readonly
.reverse()
.filter(
itinerary => !itinerary.legs.every(leg => leg.mode === 'WALK'),
);
// We need to filter only walk itineraries out to place the "separator" accurately between itineraries
this.setState(prevState => {
return {
earlierItineraries: [
...reversedItineraries,
...prevState.earlierItineraries,
],
loadingMoreItineraries: undefined,
separatorPosition: prevState.separatorPosition
? prevState.separatorPosition + reversedItineraries.length
: reversedItineraries.length,
};
});
this.resetSummaryPageSelection();
} else {
this.setState(prevState => {
return {
laterItineraries: [
...prevState.laterItineraries,
...result.itineraries,
],
loadingMoreItineraries: undefined,
};
});
}
/*
const max = result.itineraries.reduce(
(previous, { endTime }) =>
endTime > previous ? endTime : previous,
@@ -1203,11 +1204,11 @@ class SummaryPage extends React.Component {
newTime = moment(max).add(1, 'minutes');
}
*/
// this.props.setLoading(false);
/* replaceQueryParams(this.context.router, this.context.match, {
// this.props.setLoading(false);
/* replaceQueryParams(this.context.router, this.context.match, {
time: newTime.unix(),
}); */
});
});
// }
};

@@ -1259,52 +1260,50 @@ class SummaryPage extends React.Component {

this.setModeToParkRideIfSelected(tunedParams);

fetchQuery(
this.props.relayEnvironment,
moreItinerariesQuery,
tunedParams,
).then(({ plan: result }) => {
if (result.itineraries.length === 0) {
// Could not find routes arriving at original departure time
// --> cannot calculate earlier start time
this.setError('no-route-start-date-too-early');
}
this.showScreenreaderLoadedAlert();
if (reversed) {
this.setState(prevState => {
return {
laterItineraries: [
...prevState.laterItineraries,
...result.itineraries,
],
loadingMoreItineraries: undefined,
};
});
} else {
// Reverse the results so that route suggestions are in ascending order
const reversedItineraries = result.itineraries
.slice() // Need to copy because result is readonly
.reverse()
.filter(
itinerary => !itinerary.legs.every(leg => leg.mode === 'WALK'),
);
// We need to filter only walk itineraries out to place the "separator" accurately between itineraries
this.setState(prevState => {
return {
earlierItineraries: [
...reversedItineraries,
...prevState.earlierItineraries,
],
loadingMoreItineraries: undefined,
separatorPosition: prevState.separatorPosition
? prevState.separatorPosition + reversedItineraries.length
: reversedItineraries.length,
};
});
fetchQuery(this.props.relayEnvironment, moreItinerariesQuery, tunedParams)
.toPromise()
.then(({ plan: result }) => {
if (result.itineraries.length === 0) {
// Could not find routes arriving at original departure time
// --> cannot calculate earlier start time
this.setError('no-route-start-date-too-early');
}
this.showScreenreaderLoadedAlert();
if (reversed) {
this.setState(prevState => {
return {
laterItineraries: [
...prevState.laterItineraries,
...result.itineraries,
],
loadingMoreItineraries: undefined,
};
});
} else {
// Reverse the results so that route suggestions are in ascending order
const reversedItineraries = result.itineraries
.slice() // Need to copy because result is readonly
.reverse()
.filter(
itinerary => !itinerary.legs.every(leg => leg.mode === 'WALK'),
);
// We need to filter only walk itineraries out to place the "separator" accurately between itineraries
this.setState(prevState => {
return {
earlierItineraries: [
...reversedItineraries,
...prevState.earlierItineraries,
],
loadingMoreItineraries: undefined,
separatorPosition: prevState.separatorPosition
? prevState.separatorPosition + reversedItineraries.length
: reversedItineraries.length,
};
});

this.resetSummaryPageSelection();
}
});
this.resetSummaryPageSelection();
}
});
};

// save url-defined location to old searches
1 change: 0 additions & 1 deletion app/component/TerminalAlertsContainer.js
Original file line number Diff line number Diff line change
@@ -45,7 +45,6 @@ const containerComponent = createFragmentContainer(TerminalAlertsContainer, {
@argumentDefinitions(
startTime: { type: "Long" }
timeRange: { type: "Int", defaultValue: 900 }
date: { type: "String" }
) {
routes {
gtfsId
12 changes: 7 additions & 5 deletions app/component/map/StopPageMap.js
Original file line number Diff line number Diff line change
@@ -97,11 +97,13 @@ const StopPageMap = (
}
}
`;
fetchQuery(environment, query, variables).then(({ plan: result }) => {
if (isMounted) {
setPlan({ plan: result, isFetching: false });
}
});
fetchQuery(environment, query, variables)
.toPromise()
.then(({ plan: result }) => {
if (isMounted) {
setPlan({ plan: result, isFetching: false });
}
});
}
}
};
8 changes: 5 additions & 3 deletions app/component/map/StopsNearYouMap.js
Original file line number Diff line number Diff line change
@@ -214,9 +214,11 @@ function StopsNearYouMap(
}
`;
if (stop.distance < walkRoutingThreshold) {
fetchQuery(environment, query, variables).then(({ plan: result }) => {
setFirstPlan({ itinerary: result, isFetching: false, stop });
});
fetchQuery(environment, query, variables)
.toPromise()
.then(({ plan: result }) => {
setFirstPlan({ itinerary: result, isFetching: false, stop });
});
} else {
setFirstPlan({ itinerary: [], isFetching: false, stop });
}
Loading