Skip to content
This repository has been archived by the owner on Aug 5, 2021. It is now read-only.

Commit

Permalink
working graphql queries
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilWaldmann committed Jul 28, 2017
1 parent 1f49a18 commit 8f5c82e
Show file tree
Hide file tree
Showing 10 changed files with 653 additions and 225 deletions.
19 changes: 19 additions & 0 deletions lib/graphql/attributes.js
@@ -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)
}
}
101 changes: 101 additions & 0 deletions lib/graphql/data_types.js
@@ -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
})
}
}
17 changes: 17 additions & 0 deletions lib/graphql/method.js
@@ -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)
}
}
54 changes: 54 additions & 0 deletions lib/graphql/root.js
@@ -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)
}
}

0 comments on commit 8f5c82e

Please sign in to comment.