This repository has been archived by the owner on Aug 5, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f49a18
commit 8f5c82e
Showing
10 changed files
with
653 additions
and
225 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
exports.definition = { | ||
|
||
mixinCallback: function(){ | ||
this.graphqlGetter = {} | ||
}, | ||
|
||
getter: function(name, fn, returnType){ | ||
if(returnType){ | ||
this.graphqlGetter[name] = returnType | ||
} | ||
return this.callParent(name, fn) | ||
}, | ||
|
||
|
||
variant: function(name, fn, args){ | ||
fn.args = args | ||
return this.callParent(name, fn) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
const graphql = require('graphql') | ||
const inflection = require('inflection') | ||
|
||
|
||
exports.store = { | ||
mixinCallback: function(){ | ||
var store = this | ||
process.nextTick(function(){ | ||
store.ready(function(){ | ||
store.generateGraphQLTypes() | ||
}) | ||
}) | ||
}, | ||
|
||
|
||
|
||
/* | ||
GraphQLBoolean | ||
GraphQLEnumType | ||
GraphQLFloat | ||
GraphQLID | ||
GraphQLInputObjectType | ||
GraphQLInt | ||
GraphQLInterfaceType | ||
GraphQLList | ||
GraphQLNonNull | ||
GraphQLObjectType | ||
GraphQLScalarType | ||
GraphQLSchema | ||
GraphQLString | ||
GraphQLUnionType | ||
*/ | ||
generateGraphQLTypes(){ | ||
var store = this | ||
|
||
Object.keys(this.attribute_types).forEach(function(key){ | ||
var typeDef = store.attribute_types[key] | ||
var name = typeDef.name | ||
var type = typeDef.graphQLType | ||
var isList = false | ||
|
||
if(/^\w+_array$/.test(name)){ | ||
type = name.replace('_array', '') | ||
isList = true | ||
} | ||
|
||
switch(name){ | ||
case String: | ||
case 'string': | ||
case 'text': | ||
type = graphql.GraphQLString | ||
break | ||
|
||
case 'integer': | ||
type = graphql.GraphQLInt | ||
break | ||
|
||
case Number: | ||
case 'float': | ||
type = graphql.GraphQLFloat | ||
break | ||
|
||
case Boolean: | ||
case 'boolean': | ||
type = graphql.GraphQLBoolean | ||
break | ||
|
||
case Array: | ||
isList = true | ||
type = graphql.GraphQLString // TODO... | ||
break | ||
case Object: | ||
name = 'object' | ||
break | ||
case Date: | ||
name = 'date' | ||
break | ||
case Buffer: | ||
name = 'buffer' | ||
break | ||
} | ||
|
||
if(!type){ | ||
type = new graphql.GraphQLScalarType({ | ||
name: inflection.camelize(name), | ||
serialize: typeDef.cast.output, | ||
parseValue: typeDef.cast.input, | ||
parseLiteral(ast) { | ||
return typeDef.cast.input(ast.value) | ||
} | ||
}) | ||
} | ||
|
||
if(isList){ | ||
type = new graphql.GraphQLList(type) | ||
} | ||
|
||
typeDef.graphQLType = type | ||
}) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* DEFINITION | ||
*/ | ||
exports.definition = { | ||
mixinCallback: function(){ | ||
this.record_methods = {} | ||
}, | ||
|
||
|
||
method: function(name, fn, options){ | ||
fn.options = options | ||
|
||
this.record_methods[name] = fn | ||
|
||
return this.callParent(name, fn) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const Utils = require('../utils') | ||
const Store = require('../store') | ||
|
||
exports.store = { | ||
|
||
mixinCallback: function(){ | ||
this.graphqlRoots = {} | ||
}, | ||
|
||
graphRoot: function(name, options){ | ||
var self = this | ||
|
||
options = options || {} | ||
options.model = options.model || name | ||
options.name = name | ||
|
||
if(!name) throw new Error('no name given!') | ||
if(!options.model) throw new Error('no model given!') | ||
if(!options.type) throw new Error('no type given!') | ||
|
||
Utils.getStore(Store, options.store, self, function(store){ | ||
Utils.getModel(store, options.model, function(model){ | ||
if(!model[options.scope]) throw new Error('unknown scope "' + options.scope + '"') | ||
|
||
options.model = model | ||
self.graphqlRoots[name] = options | ||
}) | ||
}) | ||
|
||
return this | ||
}, | ||
|
||
|
||
|
||
graphList: function(name, options){ | ||
options = options || {} | ||
options.type = 'list' | ||
|
||
if(!options.scope) options.scope = 'limit' | ||
|
||
return this.graphRoot(name, options) | ||
}, | ||
|
||
|
||
|
||
graphGet: function(name, options){ | ||
options = options || {} | ||
options.type = 'get' | ||
|
||
if(!options.scope) options.scope = 'find' | ||
|
||
return this.graphRoot(name, options) | ||
} | ||
} |
Oops, something went wrong.