Permalink
Browse files

update: renaming from cabotage to subql (#29)

  • Loading branch information...
1 parent fab911e commit d212d1631a162f9914ce690094a7de8e8a15ef6b @Martin-Ting Martin-Ting committed on GitHub Dec 7, 2016
Showing with 16 additions and 167 deletions.
  1. +7 −0 .gitignore
  2. +1 −0 .npmignore
  3. +1 −1 experimentation/server.js
  4. +6 −6 package.json
  5. +0 −159 src/cabotage.js
  6. +1 −1 src/sockets.js
View
@@ -35,3 +35,10 @@ jspm_packages
# Optional REPL history
.node_repl_history
+
+# folders to ignore
+test
+# experimentation # leave this in for git
+
+# files to ignore
+situation.js
View
@@ -38,6 +38,7 @@ jspm_packages
# folders to ignore
test
+experimentation
# files to ignore
situation.js
@@ -3,7 +3,7 @@ var graphqlHTTP = require('express-graphql');
var app = express();
var server = require('http').Server(app);
var { buildSchema } = require('graphql');
-var { parseSchema, registerResolver, registerType, getRoot } = require('../src/cabotage.js');
+var { parseSchema, registerResolver, registerType, getRoot } = require('../src/subql.js');
var { setup } = require('../src/sockets.js');
parseSchema(`
View
@@ -1,14 +1,14 @@
{
- "name": "cabotage",
- "version": "0.0.7",
+ "name": "subql",
+ "version": "0.0.1",
"description": "Cabotage enables real time data subscriptions to graphQL APIs",
- "main": "src/cabotage.js",
+ "main": "src/subql.js",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
- "url": "git+https://github.com/CMDco/Cabotage.git"
+ "url": "git+https://github.com/CMDco/subql.git"
},
"keywords": [
"graphql",
@@ -29,9 +29,9 @@
"author": "Martin-Ting",
"license": "GPL-3.0",
"bugs": {
- "url": "https://github.com/CMDco/Cabotage/issues"
+ "url": "https://github.com/CMDco/subql/issues"
},
- "homepage": "https://github.com/CMDco/Cabotage#readme",
+ "homepage": "https://github.com/CMDco/subql#readme",
"dependencies": {
"body-parser": "^1.15.2",
"express": "^4.14.0",
View
@@ -1,159 +0,0 @@
-const graphql = require('graphql');
-var { connected } = require('./socketdata.js');
-
-const db = {};
-const mroot = {};
-const otypes = {};
-const operations = {};
-var storedSchema = '';
-
-function registerResolver(...rootFn){
- if(!rootFn){
- throw new Error('registerResolver :: registerResolver must take at least one resolver function.');
- }
- rootFn.forEach((fn) => {
- if(fn.name.length <= 0){
- throw new Error('registerResolver :: registerResolver can not take anonymous functions as arguments');
- }
- mroot[fn.name] = wrapResolver(fn);
- });
-}
-
-function getRoot(){
- return mroot;
-}
-
-function wrapResolver(fn){
- if(operations[fn.name].type === 'Mutation'){
- return function (...args){
- let ret = fn(...args);
- if(operations[fn.name].type === 'Mutation'){
- triggerType(ret.constructor.name, ret)
- }
- return ret;
- }
- }else{
- return fn;
- }
-}
-
-function registerType(classFn, ...uniqKeys){
- if(typeof classFn !== 'function'){
- throw new Error('registerType :: registerType must take in a constructor function as first argument');
- }
- if(!uniqKeys){
- throw new Error('registerType :: registerType did not recieve any keys as arguments');
- }
- otypes[classFn.name] = {
- name: classFn.name,
- classType: classFn,
- keys: uniqKeys,
- };
-}
-
-function parseSchema(schema) {
- if (!schema) {
- throw new Error('parseSchema :: parseSchema must take in a schema string');
- }
- storedSchema = schema;
- let schemaSource = new graphql.Source(schema);
- let parsedSchema = graphql.parse(schema);
- parsedSchema.definitions.forEach((ele) => {
- if (ele.name.value === 'Query' || ele.name.value === 'Mutation') {
- ele.fields.forEach((field) => {
- operations[field.name.value] = {
- name: field.name.value,
- type: ele.name.value,
- value: field.type.name.value
- }
- });
- }
- });
-}
-function findFields(obj, store) {
- var collection = obj.definitions[0].selectionSet.selections
- function findFieldshelper(val, store) {
- store[val.name.value] = []
- val.selectionSet.selections.forEach(field => {
- if (field.selectionSet) {
- store[val.name.value].push(findFieldshelper(field, {}))
- }
- else store[val.name.value].push(field.name.value)
- });
- return store
- }
- collection.forEach((val) => {
- findFieldshelper(val, store)
- });
- }
-function handleSubscribe(query, socketid) {
-
- const root = Object.assign({}, getRoot());
- connected[socketid].query = query.query
- const parseQuery = graphql.parse(query.query);
- connected[socketid].operationFields = {};
- findFields(parseQuery, connected[socketid].operationFields)
- Object.keys(root).forEach((resolverName) => {
- if (operations[resolverName].type === 'Query') {
- let oldResolver = root[resolverName];
- root[resolverName] = function (...args) {
- let ret = oldResolver(...args);
- let uniqIdentifier = generateUniqueIdentifier(operations[resolverName].value, ret);
- db[uniqIdentifier] = !db[uniqIdentifier] ? [socketid] : [...db[uniqIdentifier], socketid];
- return ret;
- }
- }
- });
-
- graphql.graphql(
- graphql.buildSchema(storedSchema),
- query.query,
- root
- ).then((result) => {
- connected[socketid].socket.emit(socketid, result);
- });
-}
-
-function handleDisconnect(socketid){
- Object.keys(db).forEach( (uniqIdentifier) => {
- let socketIndex = db[uniqIdentifier].indexOf(socketid);
- if(socketIndex >= 0){
- db[uniqIdentifier].splice(socketIndex, 1);
- }
- });
- delete connected[socketid];
-}
-
-function triggerType(typename, obj){
- if(otypes[typename] === undefined){
- throw new Error(`triggerType :: There exists no registered type named ${typename}`);
- }
- let uniqIdentifier = generateUniqueIdentifier(typename, obj);
- db[uniqIdentifier].forEach((socket) => {
- if(connected[socket] !== undefined){
- db[uniqIdentifier].forEach((socketid) => {
- connected[socketid].emit(socketid, obj);
- });
- }
- });
-}
-
-function generateUniqueIdentifier(typename, obj){
- if(otypes[typename] === undefined){
- throw new Error(`generateUniqueIdentifier :: There exists no registered type named ${typename}`);
- }
- let uniqKeys = otypes[typename].keys;
- return typename + uniqKeys.reduce((acc, curr) => {
- return acc + curr + obj[curr];
- }, '');
-}
-
-module.exports = {
- registerResolver,
- getRoot,
- registerType,
- parseSchema,
- handleSubscribe,
- handleDisconnect,
- triggerType
-};
View
@@ -1,6 +1,6 @@
var io;
var http = require('http')
-var { handleSubscribe, handleDisconnect } = require('./cabotage.js');
+var { handleSubscribe, handleDisconnect } = require('./subql.js');
var { connected } = require('./socketdata.js');
const debugLog = false;

0 comments on commit d212d16

Please sign in to comment.