Skip to content

Commit

Permalink
feat(query): add metroStations query
Browse files Browse the repository at this point in the history
It gets the stations data (name, lines, etc) from the tmb website
  • Loading branch information
Albert Alises committed Oct 12, 2020
1 parent 8f00bae commit c9c9e89
Show file tree
Hide file tree
Showing 14 changed files with 281 additions and 41 deletions.
21 changes: 21 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"parser": "@typescript-eslint/parser",
"extends": [
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"plugins": ["node", "@typescript-eslint", "prettier"],
"env": {
"es6": true,
"jest": true,
"node": true
},
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".ts"]
}
}
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@graphql-codegen/schema-ast": "^1.17.8",
"@graphql-codegen/typescript": "^1.17.10",
"@types/webpack-env": "^1.15.3",
"@types/graphql-relay": "^0.6.0",
"@typescript-eslint/eslint-plugin": "^4.4.0",
"@typescript-eslint/parser": "^4.4.0",
"clean-webpack-plugin": "^3.0.0",
Expand All @@ -39,7 +40,8 @@
},
"dependencies": {
"apollo-server": "^2.18.2",
"graphql": "^15.3.0"
"graphql": "^15.3.0",
"graphql-relay": "^0.6.0"
},
"husky": {
"hooks": {
Expand Down
68 changes: 64 additions & 4 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,71 @@ schema {

"""Root Query"""
type RootQuery {
message: Message
metroStations(after: String, first: Int, before: String, last: Int): MetroStationsQueryResponse
}

"""Just a message"""
type Message {
union MetroStationsQueryResponse = Error | MetroStationConnection

"""Generic Error"""
type Error {
"""HTTP Status Code of the Error"""
code: Int

"""Error Description"""
message: String
description: String
}

"""A connection to a list of items."""
type MetroStationConnection {
"""Information to aid in pagination."""
pageInfo: PageInfo!

"""A list of edges."""
edges: [MetroStationEdge]
}

"""Information about pagination in a connection."""
type PageInfo {
"""When paginating forwards, are there more items?"""
hasNextPage: Boolean!

"""When paginating backwards, are there more items?"""
hasPreviousPage: Boolean!

"""When paginating backwards, the cursor to continue."""
startCursor: String

"""When paginating forwards, the cursor to continue."""
endCursor: String
}

"""An edge in a connection."""
type MetroStationEdge {
"""The item at the end of the edge"""
node: MetroStation

"""A cursor for use in pagination"""
cursor: String!
}

"""Metro station information"""
type MetroStation {
"""Unique ID of the station"""
id: ID

"""Name of the station"""
name: String

"""Location coordinates of the station"""
location: Coordinates

"""Lines the station belongs to e.g. L1, L2"""
lines: [String]
}

"""Coordinates (Latitude, Longitude, Altitude), of a given station/stop"""
type Coordinates {
latitude: Float
longitude: Float
altitude: Float
}
4 changes: 2 additions & 2 deletions src/RootQuery.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { GraphQLObjectType } from "graphql";

import message from "./queries/MessageQuery";
import metroStations from "./queries/MetroStationsQuery";

export default new GraphQLObjectType({
name: "RootQuery",
description: "Root Query",
fields: {
message,
metroStations
},
});
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ server.listen(process.env.PORT || 4000).then(({ url }) => {

if (module.hot) {
module.hot.accept();
module.hot.dispose(() => console.log("Module disposed. "));
module.hot.dispose(() => console.log("Module disposed."));
}
12 changes: 0 additions & 12 deletions src/queries/MessageQuery.ts

This file was deleted.

41 changes: 41 additions & 0 deletions src/queries/MetroStationsQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import MetroStation from "../types/outputs/MetroStation";
import Error from "../types/outputs/Error";
import { GraphQLUnionType } from 'graphql';
import {
connectionArgs,
connectionDefinitions,
connectionFromArray
} from 'graphql-relay';
import type {
MetroStationsQueryResponse as MetroStationsQueryResponseType,
MetroStationConnection as MetroStationConnectionType
} from "../types";

const { connectionType: MetroStationConnection } = connectionDefinitions({
nodeType: MetroStation,
});

//MetroStationQuery response as a union of an error or a connection of MetroLine
const MetroStationsQueryResponse = new GraphQLUnionType({
name: 'MetroStationsQueryResponse',
types: [Error, MetroStationConnection],
resolveType(value) {
return value.code !== undefined ? Error : MetroStationConnection;
},
});

export default {
type: MetroStationsQueryResponse,
args: connectionArgs,
resolve: (_, args): MetroStationsQueryResponseType => {
return connectionFromArray([{
id: '43943949',
lines: ['L1'],
name: 'Clot',
location: {
latitude: 2.05,
longitude: 232.4,
}
}], args) as MetroStationConnectionType;
}
};
75 changes: 70 additions & 5 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,77 @@ export type Scalars = {
/** Root Query */
export type RootQuery = {
__typename?: 'RootQuery';
message?: Maybe<Message>;
metroStations?: Maybe<MetroStationsQueryResponse>;
};

/** Just a message */
export type Message = {
__typename?: 'Message';

/** Root Query */
export type RootQueryMetroStationsArgs = {
after?: Maybe<Scalars['String']>;
first?: Maybe<Scalars['Int']>;
before?: Maybe<Scalars['String']>;
last?: Maybe<Scalars['Int']>;
};

export type MetroStationsQueryResponse = Error | MetroStationConnection;

/** Generic Error */
export type Error = {
__typename?: 'Error';
/** HTTP Status Code of the Error */
code?: Maybe<Scalars['Int']>;
/** Error Description */
message?: Maybe<Scalars['String']>;
description?: Maybe<Scalars['String']>;
};

/** A connection to a list of items. */
export type MetroStationConnection = {
__typename?: 'MetroStationConnection';
/** Information to aid in pagination. */
pageInfo: PageInfo;
/** A list of edges. */
edges?: Maybe<Array<Maybe<MetroStationEdge>>>;
};

/** Information about pagination in a connection. */
export type PageInfo = {
__typename?: 'PageInfo';
/** When paginating forwards, are there more items? */
hasNextPage: Scalars['Boolean'];
/** When paginating backwards, are there more items? */
hasPreviousPage: Scalars['Boolean'];
/** When paginating backwards, the cursor to continue. */
startCursor?: Maybe<Scalars['String']>;
/** When paginating forwards, the cursor to continue. */
endCursor?: Maybe<Scalars['String']>;
};

/** An edge in a connection. */
export type MetroStationEdge = {
__typename?: 'MetroStationEdge';
/** The item at the end of the edge */
node?: Maybe<MetroStation>;
/** A cursor for use in pagination */
cursor: Scalars['String'];
};

/** Metro station information */
export type MetroStation = {
__typename?: 'MetroStation';
/** Unique ID of the station */
id?: Maybe<Scalars['ID']>;
/** Name of the station */
name?: Maybe<Scalars['String']>;
/** Location coordinates of the station */
location?: Maybe<Coordinates>;
/** Lines the station belongs to e.g. L1, L2 */
lines?: Maybe<Array<Maybe<Scalars['String']>>>;
};

/** Coordinates (Latitude, Longitude, Altitude), of a given station/stop */
export type Coordinates = {
__typename?: 'Coordinates';
latitude?: Maybe<Scalars['Float']>;
longitude?: Maybe<Scalars['Float']>;
altitude?: Maybe<Scalars['Float']>;
};
17 changes: 17 additions & 0 deletions src/types/outputs/Coordinates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { GraphQLFloat, GraphQLObjectType } from "graphql";

export default new GraphQLObjectType({
name: "Coordinates",
description: "Coordinates (Latitude, Longitude, Altitude), of a given station/stop",
fields: {
latitude: {
type: GraphQLFloat,
},
longitude: {
type: GraphQLFloat,
},
altitude: {
type: GraphQLFloat
}
},
});
16 changes: 16 additions & 0 deletions src/types/outputs/Error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { GraphQLObjectType, GraphQLString, GraphQLInt } from "graphql";

export default new GraphQLObjectType({
name: "Error",
description: "Generic Error",
fields: {
code: {
type: GraphQLInt,
description: "HTTP Status Code of the Error"
},
message: {
type: GraphQLString,
description: "Error Description"
},
},
});
14 changes: 0 additions & 14 deletions src/types/outputs/Message.ts

This file was deleted.

25 changes: 25 additions & 0 deletions src/types/outputs/MetroStation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Coordinates from './Coordinates';
import { GraphQLObjectType, GraphQLID, GraphQLString, GraphQLList } from "graphql";

export default new GraphQLObjectType({
name: "MetroStation",
description: "Metro station information",
fields: {
id: {
description: "Unique ID of the station",
type: GraphQLID
},
name: {
description: "Name of the station",
type: GraphQLString
},
location: {
description: "Location coordinates of the station",
type: Coordinates
},
lines: {
description: "Lines the station belongs to e.g. L1, L2",
type: GraphQLList(GraphQLString)
},
},
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"module": "esnext",
"moduleResolution": "node",
"strict": true,
"noImplicitAny": true,
"noImplicitAny": false,
"strictNullChecks": true,
"esModuleInterop": true,
"skipLibCheck": true,
Expand Down
Loading

0 comments on commit c9c9e89

Please # to comment.