Skip to content

Commit

Permalink
refactor(schema): flattern query connections to be returned from the …
Browse files Browse the repository at this point in the history
…top of the query
  • Loading branch information
aalises committed Nov 16, 2020
1 parent a9d8c7f commit 7500c43
Show file tree
Hide file tree
Showing 17 changed files with 178 additions and 294 deletions.
43 changes: 15 additions & 28 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ schema {

"""Root Query"""
type RootQuery {
metroStations(after: String, first: Int, before: String, last: Int, filterBy: FilterByInputMetro): MetroStations
"""Information about the metro stations of the city of Barcelona"""
metroStations(after: String, first: Int, before: String, last: Int, filterBy: FilterByInputMetro): MetroStationConnection

"""Returns the information about a metro station"""
metroStation(findBy: FindByInput!): MetroStation

"""Returns the information about a metro line"""
metroLine(findBy: FindByInput!): MetroLine
metroLines(after: String, first: Int, before: String, last: Int): MetroLines
bikeStations(after: String, first: Int, before: String, last: Int, filterBy: FilterByInputBike): BikeStations
bikeStation(findBy: FindByInput!): BikeStation
}

"""Information about the metro stations of the city of Barcelona"""
type MetroStations {
"""Connection with the data about stations"""
stations: MetroStationConnection
"""Information about the metro lines of the city of Barcelona"""
metroLines(after: String, first: Int, before: String, last: Int): MetroLineConnection

"""Total number of stations"""
numberOfStations: Int
"""
Information about the public bike stations (SMOU) of the city of Barcelona
"""
bikeStations(after: String, first: Int, before: String, last: Int, filterBy: FilterByInputBike): BikeStationConnection

"""Returns the information about a bike station"""
bikeStation(findBy: FindByInput!): BikeStation
}

"""A connection to a list of items."""
Expand Down Expand Up @@ -123,15 +127,6 @@ type MetroLine {
color: String
}

"""Information about the metro lines of the city of Barcelona"""
type MetroLines {
"""Connection with the data about lines"""
lines: MetroLineConnection

"""Total number of lines"""
numberOfLines: Int
}

"""A connection to a list of items."""
type MetroLineConnection {
"""Information to aid in pagination."""
Expand All @@ -150,14 +145,6 @@ type MetroLineEdge {
cursor: String!
}

"""
Information about the public bike stations (SMOU) of the city of Barcelona
"""
type BikeStations {
"""Connection with the data about bike stations"""
stations: BikeStationConnection
}

"""A connection to a list of items."""
type BikeStationConnection {
"""Information to aid in pagination."""
Expand Down
42 changes: 13 additions & 29 deletions src/datasources/MetroDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,15 @@ export default class MetroDataSource extends TmbApiDataSource {
return this.metroStationReducer(station);
}

async getAllStations(): Promise<{
numberOfStations: number | null;
stations: MetroStationType[];
}> {
async getAllStations(): Promise<MetroStationType[]> {
const response: MetroStationsAPIType | null = await this.get("estacions");

return {
numberOfStations: response?.numberReturned ?? null,
stations:
response?.features?.map((station: MetroStationAPIType) =>
this.metroStationReducer(station)
) ?? [],
};
const stations =
response?.features?.map((station: MetroStationAPIType) =>
this.metroStationReducer(station)
) ?? [];

return stations;
}

metroLineReducer({ properties }: MetroLineAPIType): MetroLineType {
Expand All @@ -173,10 +169,7 @@ export default class MetroDataSource extends TmbApiDataSource {
async getLineStations({
id,
name,
}: FindByInput): Promise<{
numberOfStations: number | null;
stations: MetroStationType[];
}> {
}: FindByInput): Promise<MetroStationType[]> {
const path = ["linies/metro", id, "estacions"].filter(Boolean).join("/");
const nameFilterParameter = name ? { filter: `NOM_LINIA='${name}'` } : {};

Expand All @@ -186,10 +179,7 @@ export default class MetroDataSource extends TmbApiDataSource {
response?.features?.map((station) => this.metroStationReducer(station)) ??
[];

return {
numberOfStations: response?.numberReturned ?? null,
stations,
};
return stations;
}

async getLine({ id, name }: FindByInput): Promise<MetroLineType | null> {
Expand All @@ -215,7 +205,7 @@ export default class MetroDataSource extends TmbApiDataSource {
return new ApolloError("The line object returned did not exist");
}

const { stations } = await this.getLineStations({ id, name });
const stations = await this.getLineStations({ id, name });

return {
...line,
Expand All @@ -229,16 +219,13 @@ export default class MetroDataSource extends TmbApiDataSource {
};
}

async getAllLines(): Promise<{
numberOfLines: number | null;
lines: MetroLineType[];
}> {
async getAllLines(): Promise<MetroLineType[]> {
const response: MetroLinesAPIType | null = await this.get("linies/metro");

const lines = await Promise.all(
(response?.features ?? []).map(async (line: MetroLineAPIType) => {
const reducedLine = this.metroLineReducer(line);
const { stations } = await this.getLineStations({
const stations = await this.getLineStations({
id: reducedLine.id,
name: reducedLine.name,
});
Expand All @@ -258,9 +245,6 @@ export default class MetroDataSource extends TmbApiDataSource {
})
);

return {
numberOfLines: response?.numberReturned ?? null,
lines,
};
return lines;
}
}
61 changes: 29 additions & 32 deletions src/datasources/__fixtures__/MetroLinesFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,30 @@ export const mockMetroLinesAPIResponse: any = {
},
};

export const mockMetroLinesResponse: any = {
numberOfLines: 1,
lines: [
{
id: 1,
name: "L1",
originStation: {
export const mockMetroLinesResponse: any = [
{
id: 1,
name: "L1",
originStation: {
id: 6660935,
lines: ["L1"],
location: {
latitude: 41.442817,
longitude: 2.224737,
},
name: "Hospital de Bellvitge",
},
endingStation: {
id: 6660525,
lines: ["L1"],
location: {
latitude: 41.41506,
longitude: 2.181497,
},
name: "Fondo",
},
stations: [
{
id: 6660935,
lines: ["L1"],
location: {
Expand All @@ -65,7 +82,7 @@ export const mockMetroLinesResponse: any = {
},
name: "Hospital de Bellvitge",
},
endingStation: {
{
id: 6660525,
lines: ["L1"],
location: {
Expand All @@ -74,27 +91,7 @@ export const mockMetroLinesResponse: any = {
},
name: "Fondo",
},
stations: [
{
id: 6660935,
lines: ["L1"],
location: {
latitude: 41.442817,
longitude: 2.224737,
},
name: "Hospital de Bellvitge",
},
{
id: 6660525,
lines: ["L1"],
location: {
latitude: 41.41506,
longitude: 2.181497,
},
name: "Fondo",
},
],
color: "CE1126",
},
],
};
],
color: "CE1126",
},
];
39 changes: 18 additions & 21 deletions src/datasources/__fixtures__/MetroStationsFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,23 @@ export const mockMetroStationsAPIResponse: any = {
},
};

export const mockMetroStationsResponse: any = {
numberOfStations: 2,
stations: [
{
id: 6660935,
lines: ["L10N"],
location: {
latitude: 41.442817,
longitude: 2.224737,
},
name: "La Salut",
export const mockMetroStationsResponse: any = [
{
id: 6660935,
lines: ["L10N"],
location: {
latitude: 41.442817,
longitude: 2.224737,
},
{
id: 6660525,
lines: ["L5"],
location: {
latitude: 41.41506,
longitude: 2.181497,
},
name: "Camp de l'Arpa",
name: "La Salut",
},
{
id: 6660525,
lines: ["L5"],
location: {
latitude: 41.41506,
longitude: 2.181497,
},
],
};
name: "Camp de l'Arpa",
},
];
23 changes: 11 additions & 12 deletions src/datasources/__tests__/MetroDataSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ describe("MetroDataSource", () => {
const mockGet = jest.fn();

MetroDataSource.get = mockGet;
MetroDataSource.getLineStations = jest.fn().mockReturnValue({
numberOfStations: 5,
stations: mockMetroLinesResponse.lines[0].stations,
});
MetroDataSource.getLineStations = jest
.fn()
.mockReturnValue(mockMetroLinesResponse[0].stations);

describe("[getAllLines]", () => {
it("Looks up the lines from the API", async () => {
Expand Down Expand Up @@ -53,7 +52,7 @@ describe("MetroDataSource", () => {
});
const res = await MetroDataSource.getLine({ id: 32 });

expect(res).toEqual(mockMetroLinesResponse.lines[0]);
expect(res).toEqual(mockMetroLinesResponse[0]);
expect(mockGet.mock.calls[0][0]).toBe("linies/metro/32");
});

Expand All @@ -65,15 +64,15 @@ describe("MetroDataSource", () => {
name: "L4",
});

expect(res).toEqual(mockMetroLinesResponse.lines[0]);
expect(res).toEqual(mockMetroLinesResponse[0]);
expect(mockGet).toBeCalledWith("linies/metro", {
filter: "NOM_LINIA='L4'",
});
});
});

it("[metroLineReducer]: Parses a metro line API data to the schema format", () => {
const lineResponse = mockMetroLinesResponse.lines[0];
const lineResponse = mockMetroLinesResponse[0];
expect(
MetroDataSource.metroLineReducer(mockMetroLinesAPIResponse.features[0])
).toEqual({
Expand Down Expand Up @@ -118,7 +117,7 @@ describe("MetroDataSource", () => {
});
const res = await MetroDataSource.getStation({ id: 32 });

expect(res).toEqual(mockMetroStationsResponse.stations[0]);
expect(res).toEqual(mockMetroStationsResponse[0]);
expect(mockGet.mock.calls[0][0]).toBe("estacions/32");
});

Expand All @@ -130,7 +129,7 @@ describe("MetroDataSource", () => {
name: "Urwhatawave",
});

expect(res).toEqual(mockMetroStationsResponse.stations[0]);
expect(res).toEqual(mockMetroStationsResponse[0]);
expect(mockGet).toBeCalledWith("estacions", {
filter: "NOM_ESTACIO='Urwhatawave'",
});
Expand All @@ -139,10 +138,10 @@ describe("MetroDataSource", () => {
it("Gets a station by proximity", async () => {
mockGet.mockReturnValueOnce(mockMetroStationsAPIResponse);
const res = await MetroDataSource.getStation({
closest: mockMetroStationsResponse.stations[1].location,
closest: mockMetroStationsResponse[1].location,
});

expect(res).toEqual(mockMetroStationsResponse.stations[1]);
expect(res).toEqual(mockMetroStationsResponse[1]);
expect(mockGet).toBeCalledWith("estacions", {});
});
});
Expand All @@ -152,7 +151,7 @@ describe("MetroDataSource", () => {
MetroDataSource.metroStationReducer(
mockMetroStationsAPIResponse.features[0]
)
).toEqual(mockMetroStationsResponse.stations[0]);
).toEqual(mockMetroStationsResponse[0]);
});

test.each([
Expand Down
1 change: 1 addition & 0 deletions src/queries/BikeStationQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { GraphQLNonNull } from "graphql";

export default {
type: BikeStation,
description: "Returns the information about a bike station",
args: {
findBy: {
type: new GraphQLNonNull(FindByInput),
Expand Down
Loading

0 comments on commit 7500c43

Please sign in to comment.