db2graphql
Advanced tools
Comparing version 0.4.2 to 0.5.0
@@ -61,4 +61,4 @@ /** | ||
for (let j = 0; j < 15; j++) data[userFields[j]] = genString(); | ||
let id = await db('users').returning('id').insert(data) | ||
users_ids.push(id[0]); | ||
let ids = await db('users').insert(data, ['id']); | ||
users_ids.push(ids[0]); | ||
console.log('user', i); | ||
@@ -71,4 +71,5 @@ } | ||
for (let j = 0; j < 15; j++) data[categoryFields[j]] = genString(); | ||
let id = await db('categories').returning('id').insert(data) | ||
categories_ids.push(id[0]); | ||
let ids = await db('categories').insert(data, ['id']) | ||
console.log(ids); | ||
categories_ids.push(ids[0]); | ||
console.log('cat', i); | ||
@@ -84,4 +85,5 @@ } | ||
data['category_id'] = cid; | ||
let id = await db('posts').returning('id').insert(data) | ||
posts_ids.push(id[0]); | ||
let ids = await db('posts').insert(data, ['id']); | ||
posts_ids.push(ids[0]); | ||
console.log(ids); | ||
uid = uid >= max_users ? 1 : uid+1; | ||
@@ -102,10 +104,11 @@ cid = cid >= max_categories ? 1 : cid+1; | ||
let res = await db.raw(sql, ['public', 'posts']); | ||
res = JSON.parse(JSON.stringify(res)); | ||
// WARNING: It may take up to 3-4 hours to complete the seed of 1 million posts | ||
if (res.rows.length && res.rows[0].count === '0') await createAndSeed(); | ||
if (res.length && res[0][0].total === 0) await createAndSeed(); | ||
// Setup DB2Graphql | ||
const db2g = require('../src/db2g'); | ||
const api = new db2g(db); | ||
await api.connect(); | ||
const api = new db2g('test', db); | ||
await api.connect(connection.connection.database); | ||
api.withBuilder(); | ||
@@ -112,0 +115,0 @@ const resolvers = api.getResolvers(); |
@@ -99,9 +99,10 @@ /** | ||
let res = await db.raw(sql, ['public', 'posts']); | ||
console.log(res.rows); | ||
// WARNING: It may take up to 3-4 hours to complete the seed of 1 million posts | ||
if (res.rows.length && res.rows[0].count === '0') await createAndSeed(); | ||
if (res.rows.length && res.rows[0].total === '0') await createAndSeed(); | ||
// Setup DB2Graphql | ||
const db2g = require('../src/db2g'); | ||
const api = new db2g(db); | ||
const api = new db2g('test', db); | ||
await api.connect(); | ||
@@ -108,0 +109,0 @@ api.withBuilder(); |
{ | ||
"name": "db2graphql", | ||
"version": "0.4.2", | ||
"version": "0.5.0", | ||
"description": "Generate Graphql schema based on existing relational database", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -0,1 +1,7 @@ | ||
<p align="center"> | ||
<img width="400" height="88" src="/art/logo.png" alt="db2graphql logo" /> | ||
</p> | ||
Logo by [@Caneco](https://twitter.com/Caneco) | ||
# db2graphql | ||
@@ -116,3 +122,3 @@ | ||
// Example of adding extra field | ||
api.add('Users', 'fullname', 'String', (parent, args, context) => { | ||
api.addField('Users.fullname', 'String', (parent, args, context) => { | ||
return String(args.foo + parent.username); | ||
@@ -122,3 +128,3 @@ }, { foo: 'String' }); | ||
// Example of overiding existing schema | ||
api.add('Users', 'password', 'String', () => ''); | ||
api.addField('Users.password', 'String', () => ''); | ||
@@ -151,3 +157,3 @@ // Get generated schema and resolvers | ||
// Add a query and resolver | ||
api.add('Query', 'getFoo', 'Boolean', async (root, args, context) => { | ||
api.addField('Query.getFoo', 'Boolean', async (root, args, context) => { | ||
return true; | ||
@@ -175,2 +181,7 @@ }, { param: 'String!' }); | ||
## Credits | ||
- Created by [Marco Afonso](https://twitter.com/AfonsoD3v) | ||
- Logo by [Caneco](https://twitter.com/caneco) | ||
## Collab | ||
@@ -177,0 +188,0 @@ |
@@ -114,2 +114,3 @@ const hash = require('string-hash-64'); | ||
(args) && this.addWhereFromArgs(tablename, query, args); | ||
(args) && this.addWhereFromArgsWhere(query, args); | ||
(args) && this.addPaginationFromArgs(tablename, query, args); | ||
@@ -131,2 +132,3 @@ if (args._debug) console.log('db hit:', query.toSQL().sql, query.toSQL().bindings); | ||
(args) && this.addWhereFromArgs(tablename, query, args); | ||
(args) && this.addWhereFromArgsWhere(query, args); | ||
const totalResult = await query.count(); | ||
@@ -148,2 +150,3 @@ let result = JSON.parse(JSON.stringify(totalResult)); | ||
(args) && this.addWhereFromArgs(tablename, query, args); | ||
(args) && this.addWhereFromArgsWhere(query, args); | ||
if (args._debug) console.log('db hit:', query.toSQL().sql, query.toSQL().bindings); | ||
@@ -162,14 +165,11 @@ return await query.first(); | ||
let result = null; | ||
let count = data[pk] ? await this.db(tablename).where(pk, data[pk]).count() : false; | ||
if (!count || parseInt(count[0].count, 10) === 0) { | ||
let count = data.input[pk] ? await this.db(tablename).where(pk, data.input[pk]).count() : false; | ||
if (!count || parseInt(count[0]['count(*)'], 10) === 0) { | ||
let query = this.db(tablename); | ||
await query.insert(data); | ||
// Mysql get last inserted ID | ||
const lastIdresult = await this.query('SELECT LAST_INSERT_ID() as ' + pk); | ||
result = [lastIdresult[0][pk]]; | ||
const lastIdresult = await query.insert(data.input, [pk]); | ||
result = [lastIdresult[0]]; | ||
} else { | ||
let query = this.db(tablename) | ||
query.where(pk, data[pk]) | ||
result = await query.update(data); | ||
query.where(pk, data.input[pk]) | ||
result = await query.update(data.input); | ||
} | ||
@@ -226,2 +226,18 @@ return result; | ||
/** | ||
* Load knex query with where condition | ||
* | ||
* @param {Function} query | ||
* @param {Object} args | ||
*/ | ||
addWhereFromArgsWhere(query, args) { | ||
// Validate filter arguments | ||
if (!args.where) return; | ||
// Apply filters | ||
const { sql, val } = args.where; | ||
query.whereRaw(sql, val); | ||
} | ||
/** | ||
* Load knex query with all pagination | ||
@@ -299,2 +315,6 @@ * | ||
localArgs.filter[tablename] = localArgs.filter[tablename].concat(originFilter); | ||
// Is where set? | ||
const hasWhere = args && args.where; | ||
if (hasWhere) localArgs['where'] = args.where; | ||
@@ -311,2 +331,3 @@ // Load from cache | ||
this.addWhereFromArgs(tablename, query, localArgs); | ||
this.addWhereFromArgsWhere(query, localArgs); | ||
if (args._debug) console.log('db hit:', key, query.toSQL().sql, query.toSQL().bindings); | ||
@@ -313,0 +334,0 @@ const results = await query; |
@@ -98,2 +98,3 @@ const hash = require('string-hash-64'); | ||
(args) && this.addWhereFromArgs(tablename, query, args); | ||
(args) && this.addWhereFromArgsWhere(query, args); | ||
(args) && this.addPaginationFromArgs(tablename, query, args); | ||
@@ -115,2 +116,3 @@ if (args._debug) console.log('db hit:', query.toSQL().sql, query.toSQL().bindings); | ||
(args) && this.addWhereFromArgs(tablename, query, args); | ||
(args) && this.addWhereFromArgsWhere(query, args); | ||
const result = await query.count(); | ||
@@ -131,2 +133,3 @@ return parseInt(result[0].count, 10); | ||
(args) && this.addWhereFromArgs(tablename, query, args); | ||
(args) && this.addWhereFromArgsWhere(query, args); | ||
if (args._debug) console.log('db hit:', query.toSQL().sql, query.toSQL().bindings); | ||
@@ -145,11 +148,11 @@ return await query.first(); | ||
let result = null; | ||
let count = data[pk] ? await this.db(tablename).where(pk, data[pk]).count() : false; | ||
let count = data.input[pk] ? await this.db(tablename).where(pk, data.input[pk]).count() : false; | ||
if (!count || parseInt(count[0].count, 10) === 0) { | ||
let query = this.db(tablename); | ||
query.returning(pk) | ||
result = await query.insert(data); | ||
result = await query.insert(data.input); | ||
} else { | ||
let query = this.db(tablename) | ||
query.where(pk, data[pk]) | ||
result = await query.update(data); | ||
query.where(pk, data.input[pk]) | ||
result = await query.update(data.input); | ||
} | ||
@@ -206,2 +209,18 @@ return result; | ||
/** | ||
* Load knex query with where condition | ||
* | ||
* @param {Function} query | ||
* @param {Object} args | ||
*/ | ||
addWhereFromArgsWhere(query, args) { | ||
// Validate filter arguments | ||
if (!args.where) return; | ||
// Apply filters | ||
const { sql, val } = args.where; | ||
query.whereRaw(sql, val); | ||
} | ||
/** | ||
* Load knex query with all pagination | ||
@@ -278,2 +297,6 @@ * | ||
localArgs.filter[tablename] = localArgs.filter[tablename].concat(originFilter); | ||
// Is where set? | ||
const hasWhere = args && args.where; | ||
if (hasWhere) localArgs['where'] = args.where; | ||
@@ -290,2 +313,3 @@ // Load from cache | ||
this.addWhereFromArgs(tablename, query, localArgs); | ||
this.addWhereFromArgsWhere(query, localArgs); | ||
if (args._debug) console.log('db hit:', key, query.toSQL().sql, query.toSQL().bindings); | ||
@@ -292,0 +316,0 @@ const results = await query; |
@@ -78,4 +78,4 @@ const PostgreSQL = require('../src/adapters/postgres'); | ||
/** | ||
* Adds a Graphql query to the schema. | ||
* If type does not exists, it gets created. | ||
* Adds a Graphql type field to the schema. | ||
* If the field does not exists, it gets created. | ||
* | ||
@@ -98,3 +98,3 @@ * <p>Usage example:</p> | ||
add(type, field, returns, resolver, params = {}) { | ||
this.compiler.add(type, field, returns, params); | ||
this.compiler.addType(type, field, returns, params); | ||
this.resolver.add(type, field, resolver); | ||
@@ -105,2 +105,27 @@ return this; | ||
/** | ||
* Adds a Graphql input field to the schema. | ||
* If the field does not exists, it gets created. | ||
* | ||
* <p>Usage example:</p> | ||
* | ||
* <pre> | ||
* addInput('InputUsers.username', 'String') | ||
* </pre> | ||
* | ||
* @access public | ||
* @param {String} path The input type name and field name ie. InputUsers.username | ||
* @param {String|Array} subType The Graphql returning type ie. Boolean or 'String' | ||
* | ||
* @returns {DB2Graphql} The self instance for fluent interface | ||
*/ | ||
addInput(path, subType) { | ||
let segments = path.trim().split('.').filter(i => !!i); | ||
if (segments.length < 2) throw new Error('addInput path must be in format Input.field'); | ||
let field = segments.pop(); | ||
let type = segments.pop(); | ||
this.compiler.addInput(type, field, subType); | ||
return this; | ||
} | ||
/** | ||
* Set before hook | ||
@@ -107,0 +132,0 @@ * |
@@ -23,3 +23,3 @@ const utils = require('../utils/utils'); | ||
// Hold schema | ||
this.schema = {}; | ||
this.schema = { type: {}, input: {} }; | ||
@@ -62,3 +62,3 @@ // Cache sdl | ||
const field = utils.toCamelCase(tablename); | ||
if (!this.schema[field]) this.schema[field] = {}; | ||
if (!this.schema.type[field]) this.schema.type[field] = {}; | ||
@@ -69,3 +69,3 @@ // Add fields | ||
try { | ||
this.schema[field][child] = { | ||
this.schema.type[field][child] = { | ||
name: child, | ||
@@ -83,3 +83,3 @@ type: this.dbDriver.mapDbColumnToGraphqlType(child, this.dbSchema[tablename][child]), | ||
let child = c + '_' + column.__foreign.tablename; | ||
this.schema[field][child] = { | ||
this.schema.type[field][child] = { | ||
name: child, | ||
@@ -95,3 +95,3 @@ type: utils.toCamelCase(column.__foreign.tablename), | ||
let child = r.ftablename; | ||
this.schema[field][child] = { | ||
this.schema.type[field][child] = { | ||
name: child, | ||
@@ -102,2 +102,3 @@ type: 'Page' + utils.toCamelCase(child), | ||
pagination: 'String', | ||
where: 'Condition', | ||
_debug: 'Boolean', | ||
@@ -110,3 +111,3 @@ _cache: 'Boolean' | ||
// Add pages | ||
this.schema['Page' + field] = { | ||
this.schema.type['Page' + field] = { | ||
total: { | ||
@@ -132,5 +133,5 @@ name: 'total', | ||
const field = utils.toCamelCase(tablename); | ||
const params = { filter: 'String', pagination: 'String', _debug: 'Boolean', _cache: 'Boolean' }; | ||
if (!this.schema.Query) this.schema['Query'] = {}; | ||
this.schema.Query['getPage' + field] = { | ||
const params = { filter: 'String', pagination: 'String', where: 'Condition', _debug: 'Boolean', _cache: 'Boolean' }; | ||
if (!this.schema.type.Query) this.schema.type['Query'] = {}; | ||
this.schema.type.Query['getPage' + field] = { | ||
name: 'getPage' + field, | ||
@@ -153,5 +154,5 @@ type: 'Page' + field, | ||
const field = utils.toCamelCase(tablename); | ||
const params = { filter: 'String', pagination: 'String', _debug: 'Boolean', _cache: 'Boolean' }; | ||
if (!this.schema.Query) this.schema['Query'] = {}; | ||
this.schema.Query['getFirst' + field] = { | ||
const params = { filter: 'String', pagination: 'String', where: 'Condition', _debug: 'Boolean', _cache: 'Boolean' }; | ||
if (!this.schema.type.Query) this.schema.type['Query'] = {}; | ||
this.schema.type.Query['getFirst' + field] = { | ||
name: 'getFirst' + field, | ||
@@ -164,18 +165,40 @@ type: field, | ||
/** | ||
* Create a convenient mutation putItem | ||
* to store a single record into the database | ||
* Generate input name from tablename | ||
* | ||
* @param {String} tablename | ||
*/ | ||
mapDbTableToGraphqlMutation(tablename) { | ||
const field = utils.toCamelCase(tablename) | ||
if (!this.schema.Mutation) this.schema['Mutation'] = {}; | ||
getInputName(tablename) { | ||
const field = utils.toCamelCase(tablename); | ||
const name = 'Input' + field; | ||
return name; | ||
} | ||
/** | ||
* Create a convenient input to be used in mutation | ||
* | ||
* @param {String} tablename | ||
*/ | ||
mapDbTableToGraphqlInput(tablename) { | ||
const name = this.getInputName(tablename); | ||
if (!this.schema.input[name]) this.schema.input[name] = {}; | ||
let columns = this.dbDriver.getTableColumnsFromSchema(tablename); | ||
let params = { _debug: 'Boolean' }; | ||
columns.forEach(col => { | ||
try { | ||
params[col] = this.dbDriver.mapDbColumnToGraphqlType(col, this.dbSchema[tablename][col]); | ||
this.schema.input[name][col] = this.dbDriver.mapDbColumnToGraphqlType(col, this.dbSchema[tablename][col]); | ||
} catch (err) {} | ||
}); | ||
this.schema.Mutation['putItem' + field] = { | ||
} | ||
/** | ||
* Create a convenient mutation putItem | ||
* to store a single record into the database | ||
* | ||
* @param {String} tablename | ||
*/ | ||
mapDbTableToGraphqlMutation(tablename) { | ||
const field = utils.toCamelCase(tablename) | ||
if (!this.schema.type.Mutation) this.schema.type['Mutation'] = {}; | ||
let name = this.getInputName(tablename); | ||
let params = { _debug: 'Boolean', input: name + '!' }; | ||
this.schema.type.Mutation['putItem' + field] = { | ||
name: 'putItem' + field, | ||
@@ -188,9 +211,9 @@ type: field, | ||
/** | ||
* Adds a Graphql query | ||
* Adds a Graphql field in types | ||
* | ||
* @param {String} field | ||
*/ | ||
add(type, field, returns, params) { | ||
if (!this.schema[type]) this.schema[type] = {}; | ||
this.schema[type][field] = { | ||
addType(type, field, returns, params) { | ||
if (!this.schema.type[type]) this.schema.type[type] = {}; | ||
this.schema.type[type][field] = { | ||
name: field, | ||
@@ -203,2 +226,14 @@ type: returns, | ||
/** | ||
* Add input field | ||
* | ||
* @param {String} input | ||
* @param {String} field | ||
* @param {String} subType | ||
*/ | ||
addInput(input, field, subType) { | ||
if (!this.schema.input[input]) this.schema.input[input] = {}; | ||
this.schema.input[input][field] = subType; | ||
} | ||
/** | ||
* Generate a complete SDL schema as a string. | ||
@@ -214,2 +249,3 @@ * Can be used as standalone. | ||
this.mapDbTableToGraphqlMutation(tablename); | ||
this.mapDbTableToGraphqlInput(tablename); | ||
} | ||
@@ -228,6 +264,7 @@ return this.schema; | ||
for (let field in this.schema) { | ||
// Build SDL types | ||
for (let field in this.schema.type) { | ||
let subfields = []; | ||
Object.keys(this.schema[field]).map(key => { | ||
let f = this.schema[field][key]; | ||
Object.keys(this.schema.type[field]).map(key => { | ||
let f = this.schema.type[field][key]; | ||
let type = Array.isArray(f.type) ? '[' + f.type[0] + ']' : f.type; | ||
@@ -238,3 +275,20 @@ subfields.push(" " + f.name + this.buildParamsFromObject(f.params || {}) + ": " + type); | ||
} | ||
this.sdl = items.join("\n\n") + "\n"; | ||
// Build SDL inputs | ||
for (let field in this.schema.input) { | ||
let subfields = []; | ||
Object.keys(this.schema.input[field]).map(key => { | ||
subfields.push(" " + key + ": " + this.schema.input[field][key]); | ||
}); | ||
items.push("input " + field + " {\n" + subfields.join("\n") + "\n}"); | ||
} | ||
this.sdl = items.join("\n\n"); | ||
// Add condition type | ||
if (items.length) { | ||
this.sdl += "\n\ninput Condition {\n sql: String!\n val: [String!]!\n}"; | ||
} | ||
this.sdl += "\n"; | ||
} | ||
@@ -241,0 +295,0 @@ return this.sdl.trim(); |
@@ -84,2 +84,24 @@ const Mysql = require('../../src/adapters/mysql'); | ||
test('it should add where clause from args with where input', async (done) => { | ||
const adapter = new Mysql(); | ||
const db = knex(connection); | ||
let query = db.table('foo'); | ||
let val = "1"; | ||
let args = { | ||
where: { | ||
sql: "id = ?", | ||
val: [val] | ||
} | ||
} | ||
await adapter.addWhereFromArgsWhere(query, args); | ||
let result = query.toSQL(); | ||
expect(/where/i.test(result.sql)).toBe(true); | ||
expect(/id\s=\s\?/i.test(result.sql)).toBe(true); | ||
expect(result.bindings[0]).toBe(val); | ||
// Close connection | ||
await db.destroy(); | ||
done(); | ||
}); | ||
test('it should add pagination from args', async (done) => { | ||
@@ -231,6 +253,6 @@ const db = knex(connection); | ||
const adapter = new Mysql(db, schema); | ||
let result = await adapter.putItem('foo', { bar: true }); | ||
let result = await adapter.putItem('foo', { input: { bar: true }}); | ||
expect(Array.isArray(result)).toBe(true); | ||
expect(result.length).toBe(1); | ||
result = await adapter.putItem('foo', { id: result[0], bar: true }); | ||
result = await adapter.putItem('foo', { input: { id: result[0], bar: true }}); | ||
expect(typeof result).toBe("number"); | ||
@@ -466,2 +488,7 @@ | ||
// Test with arg where | ||
args = { where: { sql: "bar = ?", val: ["2"] }, _cache: false }; | ||
result = await adapter.loadItemsIn('bar', 'foo', [1, 2], args); | ||
expect(result).toEqual([{ foo: 2, bar: 2 }]); | ||
// Test cache | ||
@@ -468,0 +495,0 @@ args = { filter: { bar: [[ '=', 'bar', 2 ]] }, _debug: true, _cache: false }; |
@@ -86,2 +86,24 @@ const PostgreSQL = require('../../src/adapters/postgres'); | ||
test('it should add where clause from args with where input', async (done) => { | ||
const adapter = new PostgreSQL(); | ||
const db = knex(connection); | ||
let query = db.table('foo'); | ||
let val = "1"; | ||
let args = { | ||
where: { | ||
sql: "id = ?", | ||
val: [val] | ||
} | ||
} | ||
await adapter.addWhereFromArgsWhere(query, args); | ||
let result = query.toSQL(); | ||
expect(/where/i.test(result.sql)).toBe(true); | ||
expect(/id\s=\s\?/i.test(result.sql)).toBe(true); | ||
expect(result.bindings[0]).toBe(val); | ||
// Close connection | ||
await db.destroy(); | ||
done(); | ||
}); | ||
test('it should add pagination from args', async (done) => { | ||
@@ -238,6 +260,6 @@ const db = knex(connection); | ||
const adapter = new PostgreSQL(db, schema); | ||
let result = await adapter.putItem('foo', { bar: true }); | ||
let result = await adapter.putItem('foo', { input: { bar: true }}); | ||
expect(Array.isArray(result)).toBe(true); | ||
expect(result.length).toBe(1); | ||
result = await adapter.putItem('foo', { id: result[0], bar: true }); | ||
result = await adapter.putItem('foo', { input: { id: result[0], bar: true }}); | ||
expect(typeof result).toBe("number"); | ||
@@ -475,2 +497,7 @@ | ||
// Test with arg where | ||
args = { where: { sql: "bar = ?", val: ["2"] }, _cache: false }; | ||
result = await adapter.loadItemsIn('bar', 'foo', [1, 2], args); | ||
expect(result).toEqual([{ foo: 2, bar: 2 }]); | ||
// Test cache | ||
@@ -477,0 +504,0 @@ args = { filter: { bar: [[ '=', 'bar', 2 ]] }, _debug: true, _cache: false }; |
@@ -8,3 +8,3 @@ { | ||
"user" : "root", | ||
"password" : "toor", | ||
"password" : "", | ||
"database" : "db2graphql_test" | ||
@@ -11,0 +11,0 @@ }, |
@@ -6,2 +6,4 @@ const db2g = require('../src/db2g'); | ||
const inputCondition = "\n\ninput Condition {\n sql: String!\n val: [String!]!\n}"; | ||
afterAll( async (done) => { | ||
@@ -82,4 +84,4 @@ await db.destroy(); | ||
getAPIName: String | ||
getPageFoo(filter: String, pagination: String, _debug: Boolean, _cache: Boolean): PageFoo | ||
getFirstFoo(filter: String, pagination: String, _debug: Boolean, _cache: Boolean): Foo | ||
getPageFoo(filter: String, pagination: String, where: Condition, _debug: Boolean, _cache: Boolean): PageFoo | ||
getFirstFoo(filter: String, pagination: String, where: Condition, _debug: Boolean, _cache: Boolean): Foo | ||
} | ||
@@ -97,3 +99,12 @@ | ||
type Mutation { | ||
putItemFoo(_debug: Boolean, bar: Int): Foo | ||
putItemFoo(_debug: Boolean, input: InputFoo!): Foo | ||
} | ||
input InputFoo { | ||
bar: Int | ||
} | ||
input Condition { | ||
sql: String! | ||
val: [String!]! | ||
}` | ||
@@ -130,3 +141,5 @@ const api = new db2g('test', db); | ||
const result = api.getSchema(); | ||
expect(result).toEqual("type Foo {\n bar: Boolean\n}"); | ||
let expected = "type Foo {\n bar: Boolean\n}" | ||
+ inputCondition; | ||
expect(result).toEqual(expected); | ||
done(); | ||
@@ -264,3 +277,5 @@ }); | ||
const result = api.getSchema(); | ||
expect(result).toEqual("type Query {\n getFoo: Foo\n}"); | ||
let expected = "type Query {\n getFoo: Foo\n}" | ||
+ inputCondition; | ||
expect(result).toEqual(expected); | ||
done(); | ||
@@ -273,6 +288,19 @@ }); | ||
const result = api.getSchema(); | ||
expect(result).toEqual("type Mutation {\n putFoo: Foo\n}"); | ||
let expected = "type Mutation {\n putFoo: Foo\n}" | ||
+ inputCondition; | ||
expect(result).toEqual(expected); | ||
done(); | ||
}); | ||
test('it should add a graphql input', async (done) => { | ||
const api = new db2g(); | ||
api.addInput('InputFoo.bar', 'Foo'); | ||
api.addInput('InputFoo.foo', 'Bar'); | ||
let result = api.getSchema(); | ||
let expected = "input InputFoo {\n bar: Foo\n foo: Bar\n}" | ||
+ inputCondition; | ||
expect(result).toEqual(expected); | ||
done(); | ||
}); | ||
test('it should throw exception on adding invalid field path', () => { | ||
@@ -285,2 +313,9 @@ const api = new db2g(); | ||
test('it should throw exception on adding invalid input path', () => { | ||
const api = new db2g(); | ||
expect(() => { | ||
api.addInput("Input.", 'Foo', (root, args, context) => {}); | ||
}).toThrow(new Error('addInput path must be in format Input.field')); | ||
}); | ||
test('it should add a graphql field by path', async (done) => { | ||
@@ -290,3 +325,5 @@ const api = new db2g(); | ||
const result = api.getSchema(); | ||
expect(result).toEqual("type Query {\n getFoo: Foo\n}"); | ||
let expected = "type Query {\n getFoo: Foo\n}" | ||
+ inputCondition; | ||
expect(result).toEqual(expected); | ||
done(); | ||
@@ -299,3 +336,5 @@ }); | ||
const result = api.getSchema(); | ||
expect(result).toEqual("type Mutation {\n putFoo(bar: Boolean): Foo\n}"); | ||
let expected = "type Mutation {\n putFoo(bar: Boolean): Foo\n}" | ||
+ inputCondition; | ||
expect(result).toEqual(expected); | ||
done(); | ||
@@ -302,0 +341,0 @@ }); |
@@ -59,11 +59,11 @@ const Compiler = require('../../src/graphql/compiler'); | ||
type Query { | ||
getPageBar(filter: String, pagination: String, _debug: Boolean, _cache: Boolean): PageBar | ||
getFirstBar(filter: String, pagination: String, _debug: Boolean, _cache: Boolean): Bar | ||
getPageFoo(filter: String, pagination: String, _debug: Boolean, _cache: Boolean): PageFoo | ||
getFirstFoo(filter: String, pagination: String, _debug: Boolean, _cache: Boolean): Foo | ||
getPageBar(filter: String, pagination: String, where: Condition, _debug: Boolean, _cache: Boolean): PageBar | ||
getFirstBar(filter: String, pagination: String, where: Condition, _debug: Boolean, _cache: Boolean): Bar | ||
getPageFoo(filter: String, pagination: String, where: Condition, _debug: Boolean, _cache: Boolean): PageFoo | ||
getFirstFoo(filter: String, pagination: String, where: Condition, _debug: Boolean, _cache: Boolean): Foo | ||
} | ||
type Mutation { | ||
putItemBar(_debug: Boolean, foo_id: Int, id: Int): Bar | ||
putItemFoo(_debug: Boolean, id: Int): Foo | ||
putItemBar(_debug: Boolean, input: InputBar!): Bar | ||
putItemFoo(_debug: Boolean, input: InputFoo!): Foo | ||
} | ||
@@ -73,3 +73,3 @@ | ||
id: Int | ||
bar(filter: String, pagination: String, _debug: Boolean, _cache: Boolean): PageBar | ||
bar(filter: String, pagination: String, where: Condition, _debug: Boolean, _cache: Boolean): PageBar | ||
} | ||
@@ -80,4 +80,20 @@ | ||
items: [Foo] | ||
} | ||
input InputBar { | ||
foo_id: Int | ||
id: Int | ||
} | ||
input InputFoo { | ||
id: Int | ||
} | ||
input Condition { | ||
sql: String! | ||
val: [String!]! | ||
}` | ||
const inputCondition = "\n\ninput Condition {\n sql: String!\n val: [String!]!\n}"; | ||
const invaliddbSchema = { | ||
@@ -101,3 +117,4 @@ "bar": { | ||
const compiler = new Compiler(dbSchema, dbDriver); | ||
let expected = "type Foo {\n id: Int\n bar(filter: String, pagination: String, _debug: Boolean, _cache: Boolean): PageBar\n}\n\ntype PageFoo {\n total: Int\n items: [Foo]\n}"; | ||
let expected = "type Foo {\n id: Int\n bar(filter: String, pagination: String, where: Condition, _debug: Boolean, _cache: Boolean): PageBar\n}\n\ntype PageFoo {\n total: Int\n items: [Foo]\n}" | ||
+ inputCondition; | ||
@@ -117,3 +134,5 @@ // Test non-existsing field | ||
result = compiler.getSDL(true); | ||
expect(result).toEqual("type Foo {\n id: Int\n bar(filter: String, pagination: String, _debug: Boolean, _cache: Boolean): PageBar\n}\n\ntype PageFoo {\n total: Int\n items: [Foo]\n}\n\ntype Bar {\n foo_id: Int\n id: Int\n foo_id_foo: Foo\n}\n\ntype PageBar {\n total: Int\n items: [Bar]\n}"); | ||
expected = "type Foo {\n id: Int\n bar(filter: String, pagination: String, where: Condition, _debug: Boolean, _cache: Boolean): PageBar\n}\n\ntype PageFoo {\n total: Int\n items: [Foo]\n}\n\ntype Bar {\n foo_id: Int\n id: Int\n foo_id_foo: Foo\n}\n\ntype PageBar {\n total: Int\n items: [Bar]\n}" | ||
+ inputCondition; | ||
expect(result).toEqual(expected); | ||
}); | ||
@@ -126,3 +145,5 @@ | ||
let result = compiler.getSDL(true); | ||
expect(result).toEqual("type Bar {\n\n}\n\ntype PageBar {\n total: Int\n items: [Bar]\n}"); | ||
let expected = "type Bar {\n\n}\n\ntype PageBar {\n total: Int\n items: [Bar]\n}" | ||
+ inputCondition; | ||
expect(result).toEqual(expected); | ||
}); | ||
@@ -135,3 +156,4 @@ | ||
let result = compiler.getSDL(true); | ||
let expected = "type Query {\n getPageFoo(filter: String, pagination: String, _debug: Boolean, _cache: Boolean): PageFoo\n}"; | ||
let expected = "type Query {\n getPageFoo(filter: String, pagination: String, where: Condition, _debug: Boolean, _cache: Boolean): PageFoo\n}" | ||
+ inputCondition; | ||
expect(result).toEqual(expected); | ||
@@ -145,3 +167,4 @@ }); | ||
let result = compiler.getSDL(true); | ||
let expected = "type Query {\n getFirstFoo(filter: String, pagination: String, _debug: Boolean, _cache: Boolean): Foo\n}"; | ||
let expected = "type Query {\n getFirstFoo(filter: String, pagination: String, where: Condition, _debug: Boolean, _cache: Boolean): Foo\n}" | ||
+ inputCondition; | ||
expect(result).toEqual(expected); | ||
@@ -155,3 +178,5 @@ }); | ||
let result = compiler.getSDL(); | ||
expect(result).toEqual("type Mutation {\n putItemFoo(_debug: Boolean, id: Int): Foo\n}"); | ||
let expected = "type Mutation {\n putItemFoo(_debug: Boolean, input: InputFoo!): Foo\n}" | ||
+ inputCondition; | ||
expect(result).toEqual(expected); | ||
}); | ||
@@ -164,3 +189,5 @@ | ||
let result = compiler.getSDL() | ||
expect(result).toEqual("type Mutation {\n putItemBar(_debug: Boolean): Bar\n}"); | ||
let expected = "type Mutation {\n putItemBar(_debug: Boolean, input: InputBar!): Bar\n}" | ||
+ inputCondition; | ||
expect(result).toEqual(expected); | ||
}); | ||
@@ -211,5 +238,7 @@ | ||
let compiler = new Compiler(schema, dbDriver); | ||
compiler.add('Query', 'getFoo', 'Foo') | ||
compiler.addType('Query', 'getFoo', 'Foo') | ||
let result = compiler.getSDL(false); | ||
expect(result).toEqual("type Query {\n getFoo: Foo\n}"); | ||
let expected = "type Query {\n getFoo: Foo\n}" | ||
+ inputCondition; | ||
expect(result).toEqual(expected); | ||
}); | ||
@@ -220,5 +249,7 @@ | ||
let compiler = new Compiler(schema, dbDriver); | ||
compiler.add('Mutation', 'putFoo', 'Foo', { bar: 'Boolean' }); | ||
compiler.addType('Mutation', 'putFoo', 'Foo', { bar: 'Boolean' }); | ||
let result = compiler.getSDL(false); | ||
expect(result).toEqual("type Mutation {\n putFoo(bar: Boolean): Foo\n}"); | ||
let expected = "type Mutation {\n putFoo(bar: Boolean): Foo\n}" | ||
+ inputCondition; | ||
expect(result).toEqual(expected); | ||
}); |
1437924
78
4930
189