Skip to content

Commit

Permalink
Readme + fragment extending + new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrspartak committed May 17, 2020
1 parent b3ab99a commit a41c473
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 3 deletions.
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,90 @@ result = {
}
}
*/
```

The only control you have is fragments. So this library provides base fragments with all table fields without relations. Of course you need them, so you have many ways to do so.
```javascript

//here is an example of simple query
var [err, response] = orm.query({
user: {}
})

//So here some examples with fields key
var [err, response] = orm.query({
user: {
fields: `
name
posts {
title
}
`,

//or
fields: [
'name',
{
key: 'posts',
values: [
'title'
]
}
],

//or
fields: {
name: null,
posts: {
children: {
title: null
}
}
}
}
})

//or we can create new Fragment
let newFragment = new Fragment({
name: 'some_unique_name',
table: 'user',
fields: `
name
posts {
title
}
`//any from abobe
})
var [err, response] = orm.query({
user: {
fragment: newFragment
}
})

//or even better, we can extend user fragments and use it anytime
orm.table('user').createFragment('some_unique_name', `
name
posts {
title
}
`)
var [err, response] = orm.query({
user: {
fragment: 'some_unique_name'
}
})

//of course we can use other fragments to create new one
let baseUserFragment = orm.table('user').fragment('base')
let basePostFragment = orm.table('post').fragment('base')

orm.table('user').createFragment('some_unique_name', [
baseUserFragment.gqlFields(),
{
key: 'posts',
values: [
basePostFragment.gqlFields(),
]
}
])
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hasura-om",
"version": "0.0.2",
"version": "0.0.6",
"description": "Hasura object mapping",
"main": "src/index.js",
"scripts": {
Expand Down
8 changes: 6 additions & 2 deletions src/gql/fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ class Fragment {

if (!this.params.table) throw new Error('table is required');

this.gqlFields = fieldsToGql(this.params.fields);
this._gqlFields = fieldsToGql(this.params.fields);
}

gqlFields() {
return this._gqlFields;
}

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

return {
name: fragmentName,
Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ class Hasura {
this.INITIATED = true;
}

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

return this.tables[name];
}

/*
{
[table_name]: {
Expand Down
12 changes: 12 additions & 0 deletions src/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class Table {
this.fragments = {};
}

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

return this.fields[name];
}

setField(params) {
if (!params.name) throw new Error('name is required');
if (!params.type) throw new Error('type is required');
Expand All @@ -35,6 +41,12 @@ class Table {
this.createFragment();
}

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

return this.fragments[name];
}

createFragment(name = 'base', fields = false) {
this.fragments[name] = new Fragment({
table: this.params.name,
Expand Down
74 changes: 74 additions & 0 deletions tests/table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require('dotenv').config();
const test = require('ava');
const { Table } = require('../src/');

test('throws without params', (t) => {
t.throws(
() => {
const orm = new Table();
},
{ instanceOf: Error },
);
});

test('still throws without params', (t) => {
t.throws(
() => {
const orm = new Table({
name: 'ufhreioor',
});
},
{ instanceOf: Error },
);
});

test('succesful contructor', (t) => {
let table = new Table({
name: 'test',
type: 'BASE TABLE',
});

t.true(table instanceof Table);
});

test('throws adding field', (t) => {
let table = new Table({
name: 'test',
type: 'BASE TABLE',
});

t.throws(
() => {
table.setField();
},
{ instanceOf: Error },
);
});

test('succesfuly add field', (t) => {
let table = new Table({
name: 'test',
type: 'BASE TABLE',
});

table.setField({
name: 'test',
type: 'Integer',
});

t.is(table.field('test').type, 'Integer');
});

test('field not found', (t) => {
let table = new Table({
name: 'test',
type: 'BASE TABLE',
});

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

0 comments on commit a41c473

Please sign in to comment.