clean up node interface

This commit is contained in:
Eric Hulburd
2016-01-31 19:09:52 -06:00
parent 8af7d33d08
commit bf43ca6e40
9 changed files with 20 additions and 89 deletions

View File

@@ -16,7 +16,8 @@ const GRAPHQL_PORT = 8080;
var graphql_server = express();
DB.sync.then(()=>{
DB.sync().then(()=>{
console.log("db done syncing")
// Expose a GraphQL endpoint
graphql_server.use('/', graphQLHTTP({
graphiql: true,
@@ -34,7 +35,7 @@ DB.sync.then(()=>{
var compiler = webpack({
entry: {
app: path.resolve(__dirname, 'relay', 'application.js')
app: path.resolve(__dirname, 'lib', 'relay', 'app.relay.js')
},
module: {
loaders: [
@@ -99,6 +100,7 @@ dev_server.app.get("/", (req, res, next)=>{
res.render("index");
});
console.log("launching dev server")
dev_server.listen(APP_PORT, () => {
console.log(`App is now running on http://localhost:${APP_PORT}`);
});

View File

@@ -1,4 +1,4 @@
var getbabelRelayPlugin = require('babel-relay-plugin'),
schema = require('../data/schema.json');
schema = require('../config/graphql/schema.json');
module.exports = getbabelRelayPlugin(schema.data, {abortOnError: true});

View File

@@ -15,19 +15,20 @@ const model_dir = __dirname + '/../models'
class Database {
static sync(){
console.log("syncing db")
fs.readdirSync(model_dir).forEach(function(file) {
model = require(model_dir + '/' + file);
var model = require(model_dir + '/' + file);
Database[model.name] = model;
Database.models.push(model);
});
// add associations
for (var model of Database.models){
console.log(`setting ${model.name}`);
model.set();
}
return sequelize.sync();
return sequelize.sync().then(()=>{ console.log("done syncing db") });
}
}
Database.sequelize = sequelize;

View File

@@ -3,17 +3,16 @@ import {
nodeDefinitions,
} from 'graphql-relay';
import DB from './../database'
var {nodeInterface, nodeField} = nodeDefinitions(
(globalId) => {
var {type, id} = fromGlobalId(globalId);
return null;
var {graphql_type_name, id} = fromGlobalId(globalId),
model = DB[graphql_type_name];
return model.findOne({where: {id: id}});
},
(obj) => {
if (obj instanceof Array) {
return Array;
} else {
return null;
}
(instance) => {
return instance.Model().graphql_type;
}
);

View File

@@ -15,7 +15,7 @@ export default function(){
viewer: {
type: DB.User.graphql_type,
resolve: (_, args) => {
return DB.User.findOne({where: {username: args.username}});
return DB.User.findOne({where: {username: 'bethany'}});
}
},
}),

View File

@@ -1,12 +1,10 @@
import React from 'react';
import Relay from 'react-relay';
import DB from './../../config/database'
class App extends React.Component {
render() {
var viewer = this.props.viewer,
house = viewer.house.edge.node;
house = viewer.house.edges[0].node;
return (
<div>
<h1>Hi, {viewer.username}</h1>
@@ -19,10 +17,10 @@ class App extends React.Component {
export default Relay.createContainer(App, {
fragments: {
viewer: () => Relay.QL`
fragment on ${DB.User.name} {
fragment on User {
username
house {
edge {
edges {
node {
name
}

View File

@@ -1,30 +0,0 @@
query GetAllCountries {
countries {
id
name
}
}
query GetYearData {
data(year: $years, country_id: $country_ids){
...DatumFragment
}
}
query GetCountryData($id: Integer!) {
countries(id: $id){
data {
...DatumFragment
}
}
}
fragment DatumFragment on Datum {
year
population
gdp
total_emissions
energy_emissions
industrial_emissions
agriculture_emissions
waste_emissions
lucf_emissions
}

View File

@@ -1,37 +0,0 @@
var queryType = new GraphQLObjectType({
name: 'Query',
fields: () => ({
countries: {
type: new GraphQLList(Country.graphql),
args: {
id: {
type: new GraphQLNonNull(GraphQLInteger)
},
name: {
type: new GraphQLNonNull(GraphQLString)
}
},
resolve: (root, args) => {
Country.sql.find(args)
}
},
data: {
type: new GraphQLList(Datum.graphql),
args: {
years: {
type: new GraphQLList(GraphQLInteger)
},
country_id: {
type: new GraphQLList(GraphQLInteger)
}
},
resolve: (root, args)=>{
Datum.findAll({where: args})
}
}
})
});
module.exports = new GraphQLSchema({
query: queryType
});

View File

@@ -1,2 +0,0 @@