Skip to content

Commit

Permalink
New internal methods + tests + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mrspartak committed May 18, 2020
1 parent a41c473 commit 6a447e2
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 48 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "Hasura object mapping",
"main": "src/index.js",
"scripts": {
"test": "nyc --reporter=html --reporter=lcovonly ava",
"test_dev": "nyc --reporter=html --reporter=text ava --watch",
"test": "nyc --reporter=html --reporter=lcovonly ava --verbose",
"test_dev": "nyc --reporter=html --reporter=text ava --watch --verbose",
"release": "npm-github-release",
"coverage": "nyc report --reporter=text-lcov | coveralls"
},
Expand All @@ -17,7 +17,9 @@
],
"require": [
"esm"
]
],
"failWithoutAssertions": false,
"concurrency": 16
},
"husky": {
"hooks": {
Expand Down
4 changes: 2 additions & 2 deletions src/gql/fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class Fragment {
return this._gqlFields;
}

toString() {
build() {
let fragmentName = `${this.params.name}_fragment_${this.params.table}`;
let fields = this._gqlFields;
let fields = this.gqlFields();

return {
name: fragmentName,
Expand Down
12 changes: 8 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Hasura {
if (err) throw err;

data.forEach((row) => {
this.tables[row.table_name] = new Table({
this.createTable({
name: row.table_name,
type: row.table_type,
});
Expand All @@ -52,7 +52,7 @@ class Hasura {
if (err) throw err;

data.forEach((row) => {
this.tables[row.table_name].setField({
this.table(row.table_name).setField({
name: row.column_name,
type: row.data_type,
udt: row.udt_name,
Expand All @@ -77,14 +77,14 @@ class Hasura {
if (err) throw err;

data.forEach((row) => {
this.tables[row.table_name].setPrimarykey({
this.table(row.table_name).setPrimarykey({
name: row.key_column,
position: row.position,
});
});

Object.keys(this.tables).forEach((tableName) => {
this.tables[tableName].init();
this.table(tableName).init();
});

this.INITIATED = true;
Expand All @@ -96,6 +96,10 @@ class Hasura {
return this.tables[name];
}

createTable({ name, type } = {}) {
this.tables[name] = new Table({ name, type });
}

/*
{
[table_name]: {
Expand Down
41 changes: 16 additions & 25 deletions src/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,24 @@ class Table {
}

init() {
this.createFragment();
this.createFragment('base');
}

fragment(name) {
fragment(name = 'base') {
if (typeof this.fragments[name] == 'undefined') throw new Error(`fragment ${name} not found`);

return this.fragments[name];
}

createFragment(name = 'base', fields = false) {
if (Object.keys(this.fields).length == 0 && !fields) return false;

this.fragments[name] = new Fragment({
table: this.params.name,
name,
fields: fields ? fields : this.fields,
});
return this.fragments[name];
}

/*
Expand Down Expand Up @@ -325,29 +328,17 @@ class Table {
fragmentName = '';
let fields = params.fields ? fieldsToGql(params.fields) : false;
if (!fields) {
if (params.fragment) {
if (params.fragment instanceof Fragment) {
let fragmentObject = params.fragment.toString();
fragment = fragmentObject.raw;
fragmentName = fragmentObject.name;
fields = `...${fragmentObject.name}`;
} else if (typeof this.fragments[params.fragment] != 'undefined') {
let fragmentObject = this.fragments[params.fragment].toString();
fragment = fragmentObject.raw;
fragmentName = fragmentObject.name;
fields = `...${fragmentObject.name}`;
} else {
let fragmentObject = this.fragments.base.toString();
fragment = fragmentObject.raw;
fragmentName = fragmentObject.name;
fields = `...${fragmentObject.name}`;
}
} else {
let fragmentObject = this.fragments.base.toString();
fragment = fragmentObject.raw;
fragmentName = fragmentObject.name;
fields = `...${fragmentObject.name}`;
}
let fInstance = null;
if (typeof params.fragment == 'string') fInstance = this.fragment(params.fragment);
else if (params.fragment instanceof Fragment) fInstance = params.fragment;
else fInstance = this.fragment('base');

if (!fInstance) throw new Error('table do not contain any fragment');

let fragmentObject = fInstance.build();
fragment = fragmentObject.raw;
fragmentName = fragmentObject.name;
fields = `...${fragmentObject.name}`;
}
if (!fields) throw new Error('no returning fields specified');

Expand Down
73 changes: 67 additions & 6 deletions tests/fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ test('getting fragment name', (t) => {
});

//right naming using table and fragment names
var { name } = fragment.toString();
var { name } = fragment.build();
t.is(name, 'base_fragment_test');

fragment.params.table = 'test2';
fragment.params.name = 'new';
var { name } = fragment.toString();
var { name } = fragment.build();
t.is(name, 'new_fragment_test2');
});

Expand All @@ -57,7 +57,7 @@ test('checking fragment decalration', (t) => {
},
],
});
var { raw } = fragment.toString();
var { raw } = fragment.build();
t.deepEqual(gql(raw), testFragment);

//declaring fileds with string
Expand All @@ -71,7 +71,7 @@ test('checking fragment decalration', (t) => {
}
`,
});
var { raw } = fragment.toString();
var { raw } = fragment.build();
t.deepEqual(gql(raw), testFragment);

//declaring fileds with object
Expand All @@ -85,7 +85,7 @@ test('checking fragment decalration', (t) => {
},
},
});
var { raw } = fragment.toString();
var { raw } = fragment.build();
t.deepEqual(gql(raw), testFragment);
});

Expand Down Expand Up @@ -143,7 +143,7 @@ test('checking big declaration', (t) => {
},
],
});
var { raw } = fragment.toString();
var { raw } = fragment.build();
t.deepEqual(gql(raw), testFragment);
});

Expand Down Expand Up @@ -208,3 +208,64 @@ test('check for incopatable fields format', (t) => {
{ instanceOf: Error },
);
});

test('check extension', (t) => {
const testFragment = gql`
fragment main_fragment_test on test {
id
name
logo {
host
path
}
}
`;

let baseTestFragment = new Fragment({
name: 'base',
table: 'test',
fields: ['id', 'name'],
});
let baseLogoFragment = new Fragment({
name: 'base',
table: 'logo',
fields: ['host', 'path'],
});

var mainFragment = new Fragment({
name: 'main',
table: 'test',
fields: [
baseTestFragment.gqlFields(),
{
key: 'logo',
values: baseLogoFragment.gqlFields(),
},
],
});
var { raw } = mainFragment.build();
t.deepEqual(gql(raw), testFragment);

var mainFragment = new Fragment({
name: 'main',
table: 'test',
fields: `
${baseTestFragment.gqlFields()}
logo {
${baseLogoFragment.gqlFields()}
}
`,
});
var { raw } = mainFragment.build();
t.deepEqual(gql(raw), testFragment);
});

test('check gqlFields function', (t) => {
let baseTestFragment = new Fragment({
name: 'base',
table: 'test',
fields: ['id', 'name'],
});

t.is(typeof baseTestFragment.gqlFields(), 'string');
});
42 changes: 37 additions & 5 deletions tests/hasura.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require('dotenv').config();
const test = require('ava');
const { Hasura } = require('../src/');
const { Hasura, Table } = require('../src/');

test('throws without params', (t) => {
t.throws(
Expand All @@ -9,9 +9,7 @@ test('throws without params', (t) => {
},
{ instanceOf: Error },
);
});

test('still throws without params', (t) => {
t.throws(
() => {
const orm = new Hasura({
Expand All @@ -20,9 +18,7 @@ test('still throws without params', (t) => {
},
{ instanceOf: Error },
);
});

test('graphqlUrl is not an url', (t) => {
t.throws(
() => {
const orm = new Hasura({
Expand All @@ -32,3 +28,39 @@ test('graphqlUrl is not an url', (t) => {
{ instanceOf: Error },
);
});

test('succesful contructor', (t) => {
let orm = new Hasura({
graphqlUrl: 'efwehfwiefjwopeif',
adminSecret: 'qwdqwdqwdqwd',
});

t.true(orm instanceof Hasura);
});

test('getting table', (t) => {
let orm = new Hasura({
graphqlUrl: 'efwehfwiefjwopeif',
adminSecret: 'qwdqwdqwdqwd',
});

t.throws(
() => {
orm.table('do_not_exist');
},
{ instanceOf: Error },
);

t.throws(
() => {
orm.createTable();
},
{ instanceOf: Error },
);

orm.createTable({
name: 'test',
type: 'BASE TABLE',
});
t.true(orm.table('test') instanceof Table);
});
Loading

0 comments on commit 6a447e2

Please sign in to comment.