Skip to content

Commit

Permalink
Use separate servers for development and deployed service
Browse files Browse the repository at this point in the history
  • Loading branch information
juusaw committed Feb 17, 2019
1 parent a17a03a commit f4ffe63
Show file tree
Hide file tree
Showing 6 changed files with 540 additions and 670 deletions.
4 changes: 2 additions & 2 deletions now.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"version": 2,
"name": "d20ql",
"builds": [
{ "src": "src/index.ts", "use": "@now/node@canary" }
{ "src": "src/micro.ts", "use": "@now/node@canary" }
],
"routes": [{ "src": "/(.*)", "dest": "src/index.ts" }]
"routes": [{ "src": "/(.*)", "dest": "src/micro.ts" }]
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"author": "juusaw",
"license": "MIT",
"dependencies": {
"apollo-server": "^2.4.2",
"apollo-server-micro": "^2.4.0",
"graphql": "^14.1.1",
"micro": "^9.3.3",
"ts-node": "^8.0.2",
"typescript": "^3.3.3"
},
"scripts": {
Expand All @@ -19,6 +19,7 @@
"@types/graphql": "^14.0.7",
"@types/jest": "^24.0.5",
"jest": "^24.1.0",
"ts-jest": "^23.10.5"
"ts-jest": "^23.10.5",
"ts-node": "^8.0.2"
}
}
96 changes: 96 additions & 0 deletions src/graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLNonNull,
GraphQLFloat,
GraphQLList
} from 'graphql'
import {
Dice,
DieRoll,
parseDice,
calculateMax,
calculateMean,
calculateMin,
rollDice,
countResult,
getDistribution
} from './dice'

interface RollStatsParent extends RollParent { }
const RollStatsType = new GraphQLObjectType({
name: 'Statistics',
fields: {
average: {
type: new GraphQLNonNull(GraphQLFloat),
resolve: (parent: RollStatsParent) => calculateMean(parent.roll)
},
max: {
type: new GraphQLNonNull(GraphQLInt),
resolve: (parent: RollStatsParent) => calculateMax(parent.roll)
},
min: {
type: new GraphQLNonNull(GraphQLInt),
resolve: (parent: RollStatsParent) => calculateMin(parent.roll)
},
distribution: {
type: new GraphQLList(GraphQLInt),
resolve: (parent: RollStatsParent) => getDistribution(parent.roll)
}
}
})

interface RollResultParent extends RollParent { rollResult: DieRoll[] }
const RollResultType = new GraphQLObjectType({
name: 'Results',
fields: {
total: {
type: new GraphQLNonNull(GraphQLInt),
resolve: (parent: RollResultParent) => countResult(parent.rollResult)
},
details: {
type: new GraphQLList(GraphQLInt),
resolve: (parent: RollResultParent) => parent.rollResult.map(({ result }) => result)
}
}
})

interface RollParent { roll: Dice[] }
const RollType = new GraphQLObjectType({
name: 'Roll',
fields: {
result: {
type: new GraphQLNonNull(RollResultType),
resolve: (parent: RollParent) => ({ ...parent, rollResult: rollDice(parent.roll) })
},
statistics: {
type: new GraphQLNonNull(RollStatsType),
resolve: (parent: RollParent) => parent
}
}
})

export const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
roll: {
type: RollType,
description: 'Roll a dice. Example argument: 2d12+5',
args: {
dice: {
name: 'dice',
type: new GraphQLNonNull(GraphQLString)
}
},
resolve: (_, args) => {
const roll = parseDice(args.dice)
if (!roll) return null
else return { roll }
}
}
}
})
})
106 changes: 6 additions & 100 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,106 +1,12 @@
import { ApolloServer } from 'apollo-server-micro'
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLNonNull,
GraphQLFloat,
GraphQLList
} from 'graphql'
import {
Dice,
DieRoll,
parseDice,
calculateMax,
calculateMean,
calculateMin,
rollDice,
countResult,
getDistribution
} from './dice'
import { ApolloServer } from 'apollo-server'
import { schema } from './graphql'


interface RollStatsParent extends RollParent {}
const RollStatsType = new GraphQLObjectType({
name: 'Statistics',
fields: {
average: {
type: new GraphQLNonNull(GraphQLFloat),
resolve: (parent: RollStatsParent) => calculateMean(parent.roll)
},
max: {
type: new GraphQLNonNull(GraphQLInt),
resolve: (parent: RollStatsParent) => calculateMax(parent.roll)
},
min: {
type: new GraphQLNonNull(GraphQLInt),
resolve: (parent: RollStatsParent) => calculateMin(parent.roll)
},
distribution: {
type: new GraphQLList(GraphQLInt),
resolve: (parent: RollStatsParent) => getDistribution(parent.roll)
}
}
})

interface RollResultParent extends RollParent { rollResult: DieRoll[] }
const RollResultType = new GraphQLObjectType({
name: 'Results',
fields: {
total: {
type: new GraphQLNonNull(GraphQLInt),
resolve: (parent: RollResultParent) => countResult(parent.rollResult)
},
details: {
type: new GraphQLList(GraphQLInt),
resolve: (parent: RollResultParent) => parent.rollResult.map(({result}) => result)
}
}
})

interface RollParent { roll: Dice[] }
const RollType = new GraphQLObjectType({
name: 'Roll',
fields: {
result: {
type: new GraphQLNonNull(RollResultType),
resolve: (parent: RollParent) => ({...parent, rollResult: rollDice(parent.roll)})
},
statistics: {
type: new GraphQLNonNull(RollStatsType),
resolve: (parent: RollParent) => parent
}
}
})

const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
roll: {
type: RollType,
description: 'Roll a dice. Example argument: 2d12+5',
args: {
dice: {
name: 'dice',
type: new GraphQLNonNull(GraphQLString)
}
},
resolve: (_, args) => {
const roll = parseDice(args.dice)
if (!roll) return null
else return { roll }
}
}
}
})
})

const server = new ApolloServer({
export const server = new ApolloServer({
schema,
introspection: true,
playground: true
})

export default server.createHandler()
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
})
10 changes: 10 additions & 0 deletions src/micro.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ApolloServer } from 'apollo-server-micro'
import { schema } from './graphql'

export const server = new ApolloServer({
schema,
introspection: true,
playground: true
})

export default server.createHandler()
Loading

0 comments on commit f4ffe63

Please sign in to comment.