Created
August 19, 2015 15:30
-
-
Save Igosuki/56e2a9d5f45e45c49638 to your computer and use it in GitHub Desktop.
Example ECMA6 App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
/** | |
* Copyright (c) 2015, Facebook, Inc. | |
* All rights reserved. | |
* | |
* This source code is licensed under the BSD-style license found in the | |
* LICENSE file in the root directory of this source tree. An additional grant | |
* of patent rights can be found in the PATENTS file in the same directory. | |
*/ | |
// Model types | |
class User extends Object {} | |
class Widget extends Object {} | |
class Game extends Object {} | |
class HidingSpot extends Object {} | |
// Mock data | |
var viewer = new User(); | |
viewer.id = '1'; | |
viewer.name = 'Anonymous'; | |
var widgets = ['What\'s-it', 'Who\'s-it', 'How\'s-it'].map((name, i) => { | |
var widget = new Widget(); | |
widget.name = name; | |
widget.id = `${i}`; | |
return widget; | |
}); | |
var game = new Game(); | |
game.id = '1'; | |
var hidingSpots = []; | |
(function() { | |
var hidingSpot; | |
var indexOfSpotWithTreasure = Math.floor(Math.random() * 9); | |
for (var i = 0; i < 9; i++) { | |
hidingSpot = new HidingSpot(); | |
hidingSpot.id = `${i}`; | |
hidingSpot.hasTreasure = (i === indexOfSpotWithTreasure); | |
hidingSpot.hasBeenChecked = false; | |
higingSpots.push(hidingSpot); | |
} | |
}); | |
var getWidgets = () => widgets | |
module.exports = { | |
// Export methods that your schema can use to interact with your database | |
getUser: (id) => id === viewer.id ? viewer : null, | |
getViewer: () => viewer, | |
getWidget: (id) => widgets.find(w => w.id === id), | |
getWidgets: getWidgets, | |
User, | |
Widget, | |
checkHidingSpotForTreasure: function(id) { | |
if (hidingSpots.some(hs => has.hasTreasure && hs.hasBeenChecked)) { | |
return; | |
} | |
turnsRemaining--; | |
var hidingSpot = getHidingSpot(id); | |
hidingSpot.hasBeenChecked = true; | |
}, | |
getHidingSpot: function(id) { | |
return hidingSpots.find(hs => hs.id === id) | |
}, | |
getGame: () => game, | |
getHidingSpots: () => hidingSpots, | |
getTurnsRemaining: () => turnsRemaining | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "relay-treasurehunt", | |
"private": true, | |
"description": "Treasure Hunt !", | |
"repository": "", | |
"version": "0.1.0", | |
"scripts": { | |
"start": "babel-node ./server.js" | |
}, | |
"devDependencies": { | |
"babel": "5.8.21", | |
"babel-eslint": "^4.0.5", | |
"babel-loader": "5.3.2", | |
"babel-relay-plugin": "^0.1.1", | |
"babel-runtime": "5.8.20", | |
"eslint": "^1.0.0", | |
"eslint-loader": "^1.0.0", | |
"eslint-plugin-react": "^3.2.0", | |
"webpack": "^1.10.5", | |
"webpack-dev-server": "^1.10.1" | |
}, | |
"dependencies": { | |
"classnames": "^2.1.3", | |
"express": "^4.13.1", | |
"express-graphql": "^0.1.0", | |
"graphql": "^0.2.6", | |
"graphql-relay": "^0.1.0", | |
"react": "^0.14.0-beta3", | |
"react-relay": "^0.1.0", | |
"todomvc-app-css": "^2.0.1", | |
"todomvc-common": "^1.0.2" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
import { GraphQLBoolean, GraphQLFloat, GraphQLID, GraphQLInt, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLSchema, GraphQLString } from 'graphql' | |
import { connectionArgs, connectionDefinitions, connectionFromArray, fromGlobalId, globalIdField, mutationWithClientMutationId, nodeDefinitions } from 'graphql-relay' | |
import { User, Widget, getUser, getViewer, getWidget, getWidgets, Game, HidingSpot, getGame, getHidingSpot, getHidingSpots, getTurnsRemaining, checkHidingSpotForTreasure } from './database' | |
/** | |
* We get the node interface and field from the Relay library. | |
* | |
* The first method defines the way we resolve an ID to its object. | |
* The second defines the way we resolve an object to its GraphQL type. | |
*/ | |
var {nodeInterface, nodeField} = nodeDefinitions( | |
(globalId) => { | |
var {type, id} = fromGlobalId(globalId); | |
switch (type) { | |
case "User": | |
return getUser(id) | |
case "Widget": | |
return getWidget(id) | |
case "Game": | |
return getGame(id) | |
case "HidingSpot": | |
return getHidingSpot(id) | |
default: | |
return null | |
} | |
if (type === 'User') { | |
return getUser(id); | |
} else if (type === 'Widget') { | |
return getWidget(id); | |
} else { | |
return null; | |
} | |
}, | |
(obj) => { | |
if (obj instanceof User) { | |
return userType; | |
} else if (obj instanceof Widget) { | |
return widgetType; | |
} else if (obj instanceof Game) { | |
return gameType; | |
} else if (obj instanceof HidingSpot) { | |
return hidingSpotType; | |
} else { | |
return null; | |
} | |
} | |
); | |
/** | |
* Define your own types here | |
*/ | |
var userType = new GraphQLObjectType({ | |
name: 'User', | |
description: 'A person who uses our app', | |
fields: () => ({ | |
id: globalIdField('User'), | |
widgets: { | |
type: widgetConnection, | |
description: 'A person\'s collection of widgets', | |
args: connectionArgs, | |
resolve: (_, args) => connectionFromArray(getWidgets(), args), | |
}, | |
}), | |
interfaces: [nodeInterface], | |
}); | |
var widgetType = new GraphQLObjectType({ | |
name: 'Widget', | |
description: 'A shiny widget', | |
fields: () => ({ | |
id: globalIdField('Widget'), | |
name: { | |
type: GraphQLString, | |
description: 'The name of the widget', | |
}, | |
}), | |
interfaces: [nodeInterface], | |
}); | |
var gameType = new GraphQLObjectType({ | |
name: "Game", | |
description: "A Game", | |
fields: () => ({ | |
id: globalIdField('Game'), | |
hidingSpots: { | |
type: hidingSpotConnection, | |
description: 'Places where a treasure might be hidden', | |
args: connectionArgs, | |
resolve: (game, args) => connectionFromArray(getHidingSpots(), args) | |
}, | |
turnsRemaining: { | |
type: GraphQLInt, | |
description: "The number of turns a player has left to find the treasure", | |
resolve: () => getTurnsRemaining() | |
} | |
}), | |
interfaces: [nodeInterface] | |
}) | |
var hidingSpotType = new HidingSpot({ | |
name: "HidingSpot", | |
description: "A Hiding Spot", | |
fields: () => ({ | |
id: globalIdField('HidingSpot'), | |
hasTreasure: { | |
type: GraphQLBoolean, | |
description: "True if this hiding spot holds treasure", | |
resolve: (hidingSpot) => { | |
if (hidingSpot.hasBeenChecked) { | |
return hidingSpot.hasTreasure; | |
} else { | |
return null; | |
} | |
} | |
} | |
}), | |
interfaces: [nodeInterface] | |
}) | |
/** | |
* Define your own connection types here | |
*/ | |
var {connectionType: widgetConnection} = | |
connectionDefinitions({name: 'Widget', nodeType: widgetType}); | |
/** | |
* This is the type that will be the root of our query, | |
* and the entry point into our schema. | |
*/ | |
var queryType = new GraphQLObjectType({ | |
name: 'Query', | |
fields: () => ({ | |
node: nodeField, | |
viewer: { | |
type: userType, | |
resolve: () => getViewer(), | |
}, | |
}), | |
}); | |
/** | |
* This is the type that will be the root of our mutations, | |
* and the entry point into performing writes in our schema. | |
*/ | |
var mutationType = new GraphQLObjectType({ | |
name: 'Mutation', | |
fields: () => ({ | |
}) | |
}); | |
/** | |
* Finally, we construct our schema (whose starting query type is the query | |
* type we defined above) and export it. | |
*/ | |
export var Schema = new GraphQLSchema({ | |
query: queryType, | |
mutation: mutationType | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<module type="WEB_MODULE" version="4"> | |
<component name="NewModuleRootManager" inherit-compiler-output="true"> | |
<exclude-output /> | |
<content url="file://$MODULE_DIR$" /> | |
<orderEntry type="inheritedJdk" /> | |
<orderEntry type="sourceFolder" forTests="false" /> | |
<orderEntry type="library" name="treasurehunt node_modules" level="project" /> | |
<orderEntry type="library" name="Node.js v0.12.7 Core Modules" level="application" /> | |
</component> | |
</module> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment