Skip to content

Commit

Permalink
feat: add filter by lineName in bus stops query
Browse files Browse the repository at this point in the history
  • Loading branch information
aalises committed Dec 21, 2020
1 parent 980b66d commit dc14e92
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 43 deletions.
15 changes: 4 additions & 11 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ schema {
"""Root Query"""
type RootQuery {
"""Information about the metro stations of the city of Barcelona"""
metroStations(after: String, first: Int, before: String, last: Int, filterBy: FilterByInputMetro): MetroStationConnection
metroStations(after: String, first: Int, before: String, last: Int, filterBy: FilterByInputTmb): MetroStationConnection

"""Returns the information about a metro station"""
metroStation(findBy: FindByInput!): MetroStationQueryResponse
Expand All @@ -28,7 +28,7 @@ type RootQuery {
busStop(findBy: FindByInput!): BusStopQueryResponse

"""Information about the bus stops of the city of Barcelona"""
busStops(after: String, first: Int, before: String, last: Int, filterBy: FilterByInputBus): BusStopConnection
busStops(after: String, first: Int, before: String, last: Int, filterBy: FilterByInputTmb): BusStopConnection
}

"""A connection to a list of items."""
Expand Down Expand Up @@ -87,9 +87,9 @@ type CoordinatesOutput {
}

"""
Input for the filterBy argument of the metro queries, which allows filtering a connection by some parameters (e.g. lineName or lineId)
Input for the filterBy argument of the metro and bus queries, which allows filtering a connection by some parameters (e.g. lineName or lineId)
"""
input FilterByInputMetro {
input FilterByInputTmb {
lineId: Int
lineName: String
}
Expand Down Expand Up @@ -290,10 +290,3 @@ type BusStopEdge {
"""A cursor for use in pagination"""
cursor: String!
}

"""
Input for the filterBy argument of the bus queries, which allows filtering a connection by some parameters (e.g. lineId)
"""
input FilterByInputBus {
lineId: Int
}
6 changes: 4 additions & 2 deletions src/datasources/BusDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ export default class BusDataSource extends TmbApiDataSource {
return this.busStopReducer(stop);
}

async getLineStops({ id }: FindByInputType): Promise<BusStopType[]> {
async getLineStops({ id, name }: FindByInputType): Promise<BusStopType[]> {
const path = ["linies/bus", id, "parades"].filter(Boolean).join("/");
const response = await this.get(path);
const nameFilterParameter = name ? { filter: `DESC_LINIA='${name}'` } : {};

const response = await this.get(path, nameFilterParameter);

const stops =
response?.features?.map((stop) => this.busStopReducer(stop)) ?? [];
Expand Down
17 changes: 3 additions & 14 deletions src/inputs/FilterByInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
GraphQLBoolean,
} from "graphql";

export const FilterByInputMetro = new GraphQLInputObjectType({
name: "FilterByInputMetro",
export const FilterByInputTmb = new GraphQLInputObjectType({
name: "FilterByInputTmb",
description:
"Input for the filterBy argument of the metro queries, which allows filtering a connection by some parameters (e.g. lineName or lineId)",
"Input for the filterBy argument of the metro and bus queries, which allows filtering a connection by some parameters (e.g. lineName or lineId)",
fields: {
lineId: {
type: GraphQLInt,
Expand All @@ -19,17 +19,6 @@ export const FilterByInputMetro = new GraphQLInputObjectType({
},
});

export const FilterByInputBus = new GraphQLInputObjectType({
name: "FilterByInputBus",
description:
"Input for the filterBy argument of the bus queries, which allows filtering a connection by some parameters (e.g. lineId)",
fields: {
lineId: {
type: GraphQLInt,
},
},
});

export const FilterByInputBike = new GraphQLInputObjectType({
name: "FilterByInputBike",
description:
Expand Down
7 changes: 4 additions & 3 deletions src/queries/BusStopsQuery.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import { connectionArgs, connectionFromArray } from "graphql-relay";
import type { BusStopConnectionType } from "../../types";
import { BusStopConnection } from "../outputs/BusStop";
import { FilterByInputBus } from "../inputs/FilterByInput";
import { FilterByInputTmb } from "../inputs/FilterByInput";

export default {
type: BusStopConnection,
description: "Information about the bus stops of the city of Barcelona",
args: {
...connectionArgs,
filterBy: {
type: FilterByInputBus,
type: FilterByInputTmb,
},
},
resolve: async (_, args, { dataSources }): Promise<BusStopConnectionType> => {
const { filterBy } = args;

const stops = await (async () => {
if (!filterBy?.lineId) {
if (!filterBy?.lineId && !filterBy?.lineName) {
return await dataSources.bus.getAllStops();
}
return await dataSources.bus.getLineStops({
id: filterBy?.lineId ?? null,
name: filterBy?.lineName ?? null,
});
})();

Expand Down
4 changes: 2 additions & 2 deletions src/queries/MetroStationsQuery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { connectionArgs, connectionFromArray } from "graphql-relay";
import type { MetroStationConnectionType } from "../../types";
import { FilterByInputMetro } from "../inputs/FilterByInput";
import { FilterByInputTmb } from "../inputs/FilterByInput";
import { MetroStationConnection } from "../outputs/MetroStation";

export default {
Expand All @@ -9,7 +9,7 @@ export default {
args: {
...connectionArgs,
filterBy: {
type: FilterByInputMetro,
type: FilterByInputTmb,
},
},
resolve: async (
Expand Down
3 changes: 2 additions & 1 deletion src/queries/__tests__/BusStopsQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "../../datasources/__fixtures__/BusStopsFixtures";

const GET_BUS_STOPS = gql`
query getBusStops($first: Int, $filterBy: FilterByInputBus) {
query getBusStops($first: Int, $filterBy: FilterByInputTmb) {
busStops(first: $first, filterBy: $filterBy) {
edges {
node {
Expand Down Expand Up @@ -49,6 +49,7 @@ describe("busStops Query", () => {

expect(mockGetLineStops).toHaveBeenCalledWith({
id: 3,
name: null,
});
});
});
2 changes: 1 addition & 1 deletion src/queries/__tests__/MetroStationsQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import createTestServer from "../../utils/createTestServer";
import { mockMetroStationsAPIResponse } from "../../datasources/__fixtures__/MetroStationsFixtures";

const GET_METRO_STATIONS = gql`
query getMetroStations($first: Int, $filterBy: FilterByInputMetro) {
query getMetroStations($first: Int, $filterBy: FilterByInputTmb) {
metroStations(first: $first, filterBy: $filterBy) {
edges {
node {
Expand Down
13 changes: 4 additions & 9 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface RootQueryMetroStationsArgsType {
first: Maybe<Scalars['Int']>;
before: Maybe<Scalars['String']>;
last: Maybe<Scalars['Int']>;
filterBy: Maybe<FilterByInputMetroType>;
filterBy: Maybe<FilterByInputTmbType>;
}


Expand Down Expand Up @@ -92,7 +92,7 @@ export interface RootQueryBusStopsArgsType {
first: Maybe<Scalars['Int']>;
before: Maybe<Scalars['String']>;
last: Maybe<Scalars['Int']>;
filterBy: Maybe<FilterByInputBusType>;
filterBy: Maybe<FilterByInputTmbType>;
}

/** A connection to a list of items. */
Expand Down Expand Up @@ -147,8 +147,8 @@ export interface CoordinatesOutputType {
altitude: Maybe<Scalars['Float']>;
}

/** Input for the filterBy argument of the metro queries, which allows filtering a connection by some parameters (e.g. lineName or lineId) */
export interface FilterByInputMetroType {
/** Input for the filterBy argument of the metro and bus queries, which allows filtering a connection by some parameters (e.g. lineName or lineId) */
export interface FilterByInputTmbType {
lineId: Maybe<Scalars['Int']>;
lineName: Maybe<Scalars['String']>;
}
Expand Down Expand Up @@ -340,8 +340,3 @@ export interface BusStopEdgeType {
/** A cursor for use in pagination */
cursor: Scalars['String'];
}

/** Input for the filterBy argument of the bus queries, which allows filtering a connection by some parameters (e.g. lineId) */
export interface FilterByInputBusType {
lineId: Maybe<Scalars['Int']>;
}

0 comments on commit dc14e92

Please sign in to comment.