create valid relay schema.json

This commit is contained in:
Eric Hulburd
2016-01-31 17:18:27 -06:00
parent 2c42090cd9
commit c0efde030d
20 changed files with 90004 additions and 266 deletions

View File

@@ -1,23 +1,27 @@
import {
GraphQLString,
GraphQLNonNull,
GraphQLObjectType
GraphQLObjectType,
GraphQLInt,
GraphQLString
} from 'graphql';
import {
fromGlobalId,
globalIdField,
nodeDefinitions,
connectionDefinitions,
connectionArgs
} from 'graphql-relay';
import extend from 'extend';
import DB from "./../config/database";
import {nodeInterface} from './../lib/node.relay';
import {nodeInterface} from './../config/graphql/node';
const NAME = 'House';
/**
* Define your own types here
* Sequelize Definition
*/
var House = DB.sequelize.define('House', {
var House = DB.sequelize.define(NAME, {
id: {
type: DB.Sequelize.INTEGER,
primaryKey: true,
@@ -25,25 +29,63 @@ var House = DB.sequelize.define('House', {
},
name: DB.Sequelize.STRING
}, {
tableName: "houses",
paranoid: true,
underscored: true,
tableName: "houses",
instanceMethods: {
},
classMethods: {
associate: ()=>{
set: ()=>{
House.hasMany(DB.PowerDatum, {as: 'PowerData'});
House.hasMany(DB.User, {as: 'Habitants'});
House.graphql_type = new GraphQLObjectType({
name: NAME,
description: 'A house',
fields: () => ({
id: globalIdField(NAME),
name: {
type: new GraphQLNonNull(GraphQLString)
},
power_data: {
type: connectionDefinitions({name: DB.PowerDatum.name, nodeType: DB.PowerDatum.graphql_type}).connectionType,
description: "Returns house's power data.",
args: connectionArgs,
resolve: (house, args) => {
return house.getPowerDataByTime(args);
}
},
habitants: {
type: connectionDefinitions({name: DB.User.name, nodeType: DB.User.graphql_type}).connectionType,
description: "Returns list of house's habitants.",
args: connectionArgs,
resolve: (house, args) => {
var params = extend({
order: 'name ASC',
limit: 50,
offset: 0,
}, args);
delete params.where; // don't allow any additional query params.
return house.getHabitants(params);
}
}
}),
interfaces: [nodeInterface]
});
},
getPowerDataByTime: (start_date, end_date, page)=>{
var params = extend({
order: 'time ASC',
limit: 500
}, args.page);
params.where = {time: {}};
if (start_date) params.where.time.$gt = moment.utc(start_date).toDate();
if (end_date) params.where.time.$lt = moment.utc(end_date).toDate();
return House.getPowerData(params);
}
}
});
House.graphql_type = new GraphQLObjectType({
name: 'House',
description: 'A house',
fields: () => ({
id: globalIdField('House'),
name: GraphQLNonNull(GraphQLInt)
}),
interfaces: [nodeInterface],
});
House.name = 'House';
House.name = NAME;
module.exports = House;

View File

@@ -1,23 +1,23 @@
import {
GraphQLFloat,
GraphQLInt,
GraphQLNonNull,
GraphQLObjectType
} from 'graphql';
import {
fromGlobalId,
globalIdField
} from 'graphql-relay';
import DB from "./../config/database";
import {nodeInterface} from './../lib/node.relay';
import {nodeInterface} from './../config/graphql/node';
const NAME = 'PowerDatum'
/**
* Define your own types here
*/
var PowerDatum = DB.sequelize.define('PowerDatum', {
var PowerDatum = DB.sequelize.define(NAME, {
id: {
type: DB.Sequelize.INTEGER,
primaryKey: true,
@@ -26,27 +26,36 @@ var PowerDatum = DB.sequelize.define('PowerDatum', {
time: DB.Sequelize.DATE,
power: DB.Sequelize.FLOAT
}, {
paranoid: true,
underscored: true,
tableName: "power_data",
instanceMethods: {
},
classMethods: {
associate: ()=>{
set: ()=>{
PowerDatum.belongsTo(DB.House);
PowerDatum.graphql_type = new GraphQLObjectType({
name: NAME,
description: 'A person who uses our app',
fields: () => ({
id: globalIdField(NAME),
power: {
type: GraphQLFloat,
},
time: {
type: GraphQLInt,
description: "Time the power was recorded.",
resolve: (power_datum, _) => {
return power_datum.time.getTime();
}
},
}),
interfaces: [nodeInterface]
});
}
}
});
PowerDatum.graphql_type = new GraphQLObjectType({
name: 'PowerDatum',
description: 'A person who uses our app',
fields: () => ({
id: globalIdField('PowerDatum'),
time: GraphQLInt,
power: GraphQLFloat
}),
interfaces: [nodeInterface],
});
PowerDatum.name = 'PowerDatum';
PowerDatum.name = NAME;
module.exports = PowerDatum;

67
models/user.js Normal file
View File

@@ -0,0 +1,67 @@
import {
GraphQLString,
GraphQLNonNull,
GraphQLObjectType
} from 'graphql';
import {
globalIdField,
connectionDefinitions,
connectionArgs
} from 'graphql-relay';
import DB from "./../config/database";
import {nodeInterface} from './../config/graphql/node';
const NAME = 'User';
/**
* Sequelize Definition
*/
var User = DB.sequelize.define(NAME, {
id: {
type: DB.Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true // Automatically gets converted to SERIAL for postgres
},
username: {
type: DB.Sequelize.STRING,
unique: true
}
}, {
paranoid: true,
underscored: true,
tableName: "users",
instanceMethods: {
},
classMethods: {
set: ()=>{
User.belongsTo(DB.House);
User.graphql_type = new GraphQLObjectType({
name: NAME,
description: 'A house',
fields: () => ({
id: globalIdField(NAME),
username: {
type: new GraphQLNonNull(GraphQLString)
},
house: {
type: connectionDefinitions({name: DB.House.name, nodeType: DB.House.graphql_type}).connectionType,
description: "Returns user's house.",
args: connectionArgs,
ref: DB.PowerDatum.name,
resolve: (user, args) => {
return user.getHouse();
}
}
}),
interfaces: [nodeInterface]
});
}
}
});
User.name = NAME;
module.exports = User;