import * as express from "express"; import * as graphqlHTTP from "express-graphql"; import {buildSchema} from "graphql"; import { GameServer } from "./GameServer"; import { Direction, MazeTile } from "./MazeMap"; import { createSampleMaps } from "./SampleMaps"; const port = 8080; const apiRoot = "/graphql"; const app = express(); const schema = buildSchema(` enum Direction { none top left bottom right } """ Representing a tile with it's possible pathways. """ type MazeTileQL { id: String! paths: [Direction!]! } type Mutation { "Start a new game" startGame(userID: String!, mapID: String!): MazeTileQL } type Query { "Tells you your where you currently are and how it looks there in case you forgot." currentPostition(userID: String!): MazeTileQL "Lists currently available maps." listMaps: [String!]! } `); class MazeTileQL { public static DirectionToString(dir:Direction): string { switch (dir) { case Direction.none: return "none"; case Direction.top: return "top"; case Direction.left: return "left"; case Direction.bottom: return "bottom"; case Direction.right: return "right"; } } public id:string; public paths:string[]; constructor(tile:MazeTile) { this.id = tile.id; this.paths = tile.paths.map(MazeTileQL.DirectionToString); } } const games = new GameServer(); const maps = createSampleMaps(); maps.forEach(m => games.addMap(m)); const root = { currentPostition: ({userID}) => new MazeTileQL(games.currentPosition(userID)), listMaps: () => games.listMaps(), startGame: ({userID, mapID}) => new MazeTileQL(games.startGame(userID, mapID)), }; app.use(apiRoot, graphqlHTTP({ graphiql: true, rootValue: root, schema, })); app.listen(port, () => console.log(`App listening on port ${port}...`));