From 8af7d33d0885382aecdc66fbf10b71b73d4c6df3 Mon Sep 17 00:00:00 2001 From: Eric Hulburd Date: Sun, 31 Jan 2016 18:08:42 -0600 Subject: [PATCH] make update schema gulp task --- app.express.js | 24 +++++++++++-------- config/graphql/schema.graphql | 2 +- config/graphql/schema.json | 43 +---------------------------------- gulpfile.babel.js | 4 +++- lib/tasks/update_schema.js | 35 ++++++++++++++++++++++++++++ models/house.js | 5 ++-- package.json | 2 +- relay/app.relay.js | 15 ------------ scripts/updateSchema.js | 34 --------------------------- 9 files changed, 58 insertions(+), 106 deletions(-) create mode 100644 lib/tasks/update_schema.js delete mode 100644 relay/app.relay.js diff --git a/app.express.js b/app.express.js index aaa7345..cc180da 100644 --- a/app.express.js +++ b/app.express.js @@ -7,22 +7,26 @@ import graphQLHTTP from 'express-graphql'; import path from 'path'; import webpack from 'webpack'; import WebpackDevServer from 'webpack-dev-server'; -import {Schema} from './config/graphql/schema'; +import schema from './config/graphql/schema'; + +import DB from './config/database'; const APP_PORT = 3000; const GRAPHQL_PORT = 8080; var graphql_server = express(); -// Expose a GraphQL endpoint -graphql_server.use('/', graphQLHTTP({ - graphiql: true, - pretty: true, - schema: Schema, -})); -graphql_server.listen(GRAPHQL_PORT, () => console.log( - `GraphQL Server is now running on http://localhost:${GRAPHQL_PORT}` -)); +DB.sync.then(()=>{ + // Expose a GraphQL endpoint + graphql_server.use('/', graphQLHTTP({ + graphiql: true, + pretty: true, + schema: schema(), + })); + graphql_server.listen(GRAPHQL_PORT, () => console.log( + `GraphQL Server is now running on http://localhost:${GRAPHQL_PORT}` + )); +}); /* * Compile and Serve Relay App w/ Webpack diff --git a/config/graphql/schema.graphql b/config/graphql/schema.graphql index 386366e..5dce13a 100644 --- a/config/graphql/schema.graphql +++ b/config/graphql/schema.graphql @@ -50,7 +50,7 @@ type Query { type User implements Node { id: ID! username: String! - house(after: String, first: Int, before: String, last: Int): HouseConnection + house: HouseConnection } type UserConnection { diff --git a/config/graphql/schema.json b/config/graphql/schema.json index a7cbdd7..ee80b96 100644 --- a/config/graphql/schema.json +++ b/config/graphql/schema.json @@ -633,48 +633,7 @@ { "name": "house", "description": "Returns user's house.", - "args": [ - { - "name": "after", - "description": null, - "type": { - "kind": "SCALAR", - "name": "String", - "ofType": null - }, - "defaultValue": null - }, - { - "name": "first", - "description": null, - "type": { - "kind": "SCALAR", - "name": "Int", - "ofType": null - }, - "defaultValue": null - }, - { - "name": "before", - "description": null, - "type": { - "kind": "SCALAR", - "name": "String", - "ofType": null - }, - "defaultValue": null - }, - { - "name": "last", - "description": null, - "type": { - "kind": "SCALAR", - "name": "Int", - "ofType": null - }, - "defaultValue": null - } - ], + "args": [], "type": { "kind": "OBJECT", "name": "HouseConnection", diff --git a/gulpfile.babel.js b/gulpfile.babel.js index b25aa7b..074ce78 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -1,7 +1,9 @@ import gulp from 'gulp'; import yargs from 'yargs'; + import DB from './config/database'; -import {PowerDataSeed, HouseSeed, UserSeed} from './lib/tasks/seed_data' +import {PowerDataSeed, HouseSeed, UserSeed} from './lib/tasks/seed_data'; +import updateSchema from './lib/tasks/update_schema'; gulp.task('generate_power_csv', function(done){ DB.sync().then(()=>{ diff --git a/lib/tasks/update_schema.js b/lib/tasks/update_schema.js new file mode 100644 index 0000000..5c7ea58 --- /dev/null +++ b/lib/tasks/update_schema.js @@ -0,0 +1,35 @@ +import fs from 'fs'; +import path from 'path'; +import { graphql, GraphQLSchema } from 'graphql'; +import { introspectionQuery, printSchema } from 'graphql/utilities'; + +import DB from './../../config/database'; +import schema from './../../config/graphql/schema'; + +DB.sync().then(()=>{ + + var Schema = schema(); + + // Save JSON of full schema introspection for Babel Relay Plugin to use + (async () => { + var result = await (graphql(Schema, introspectionQuery)); + if (result.errors) { + console.error( + 'ERROR introspecting schema: ', + JSON.stringify(result.errors, null, 2) + ); + } else { + fs.writeFileSync( + path.join(__dirname, './../../config/graphql/schema.json'), + JSON.stringify(result, null, 2) + ); + } + })(); + + // Save user readable type system shorthand of schema + fs.writeFileSync( + path.join(__dirname, './../../config/graphql/schema.graphql'), + printSchema(Schema) + ); + +}); diff --git a/models/house.js b/models/house.js index 962fbfc..c291ec3 100644 --- a/models/house.js +++ b/models/house.js @@ -37,7 +37,8 @@ var House = DB.sequelize.define(NAME, { }, classMethods: { set: ()=>{ - + House.associate(); + House.defineGraphQLType(); }, associate: ()=>{ House.hasMany(DB.PowerDatum, {as: 'PowerData'}); @@ -82,7 +83,7 @@ var House = DB.sequelize.define(NAME, { }, interfaces: [nodeInterface] }); - } + }, getPowerDataByTime: (start_date, end_date, page)=>{ var params = extend({ order: 'time ASC', diff --git a/package.json b/package.json index c9eddf0..149990d 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "start": "babel-node ./app.express.js", - "update-schema": "babel-node --debug ./scripts/updateSchema.js" + "update-schema": "babel-node ./lib/tasks/update_schema.js" }, "dependencies": { "body-parser": "~1.12.0", diff --git a/relay/app.relay.js b/relay/app.relay.js deleted file mode 100644 index e3d8963..0000000 --- a/relay/app.relay.js +++ /dev/null @@ -1,15 +0,0 @@ -import 'babel-polyfill'; - -import App from './components/App'; -import AppHomeRoute from './routes/AppHomeRoute'; -import React from 'react'; -import ReactDOM from 'react-dom'; -import Relay from 'react-relay'; - -ReactDOM.render( - , - document.getElementById('root') -); diff --git a/scripts/updateSchema.js b/scripts/updateSchema.js index bf2c89d..139597f 100755 --- a/scripts/updateSchema.js +++ b/scripts/updateSchema.js @@ -1,36 +1,2 @@ -#!/usr/bin/env babel-node --optional es7.asyncFunctions -import fs from 'fs'; -import path from 'path'; -import schema from '../config/graphql/schema'; -import { graphql, GraphQLSchema } from 'graphql'; -import { introspectionQuery, printSchema } from 'graphql/utilities'; -import DB from './../config/database'; -DB.sync().then(()=>{ - - var Schema = schema(); - - // Save JSON of full schema introspection for Babel Relay Plugin to use - (async () => { - var result = await (graphql(Schema, introspectionQuery)); - if (result.errors) { - console.error( - 'ERROR introspecting schema: ', - JSON.stringify(result.errors, null, 2) - ); - } else { - fs.writeFileSync( - path.join(__dirname, '../config/graphql/schema.json'), - JSON.stringify(result, null, 2) - ); - } - })(); - - // Save user readable type system shorthand of schema - fs.writeFileSync( - path.join(__dirname, '../config/graphql/schema.graphql'), - printSchema(Schema) - ); - -});