Skip to content

Commit

Permalink
feat: support read long type integer. (#59)
Browse files Browse the repository at this point in the history
* feat: support read long type integer (less than 2^52 - 1) by graphql, add special support for "point" type property.

* fix: normalize return value of 'binary' type on graphql.

* feat: normalize json-deserialization for binary type.

* chore: code cleanup.
  • Loading branch information
richardo2016 authored May 14, 2023
1 parent 2877a13 commit dfb8421
Show file tree
Hide file tree
Showing 16 changed files with 691 additions and 263 deletions.
11 changes: 10 additions & 1 deletion demo/defs/fields_type/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ module.exports = db => {
binary1: Buffer,
binary2: {
type: 'binary'
}
},
point: {
type: 'point'
},
longInSafeNumber: {
type: 'integer',
big: true, // this field not supported by mysql
size: 8,
},
// TODO: support value greater than Number.MAX_SAFE_INTEGER such as 1111222233334444555566
});
};
384 changes: 193 additions & 191 deletions demo/diagram.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
187 changes: 187 additions & 0 deletions demo/test/graphql-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
const test = require('test');
test.setup();

const jsonModule = require('json');
const util = require('util');

// const BigNumber = require('bignumber.js');

const { runServer } = require('../test/_utils');

const tappInfo = require('../test/support/spec_helper').getRandomSqliteBasedApp();
const tSrvInfo = require('../test/support/spec_helper').mountAppToSrv(tappInfo.app, {appPath: '/api'});
runServer(tSrvInfo.server, () => void 0)

const http = require('http');

var ids = [];

var testData = {
binary1: Buffer.from('binary1'),
binary2: Buffer.from('binary2'),
}

function init_data () {
// const safeLongValue = new BigNumber('9007199254740991');
var rep = http.post(tSrvInfo.appUrlBase + '/test_fields_type', {
json: [
{
name1: 'name1',
name2: 'name2',
profile: { foo: 'bar' },
// in real world, according to clients,
// binary data would be JSON-type, like `{ type: "Buffer", data: [...] }`
// or Array-type, like `[...]`
binary1: testData.binary1,
binary2: testData.binary2,
point: { x: 51.5177, y: -0.0968 },
longInSafeNumber: 9007199254740991,
}
]
});

rep.json().forEach(r => ids.push(r.id));
}

describe("graphql-types", () => {
after(() => tappInfo.utils.cleanLocalDB())

before(() => {
tappInfo.utils.dropModelsSync();

init_data();
})

it('get', () => {
var rep = http.post(tSrvInfo.appUrlBase + ``, {
headers: {
'Content-Type': 'application/graphql'
},
body: `{
test_fields_type(id:"${ids[0]}"){
id,
name1
name2
profile
binary1
binary2
point
longInSafeNumber
}
}`
});

assert.equal(rep.statusCode, 200);

var result = rep.json();
assert.deepEqual(result, {
"data": {
"test_fields_type": {
"id": ids[0],
"name1": "name1",
"name2": "name2",
"profile": { foo: 'bar' },
"binary1": jsonModule.decode(jsonModule.encode(testData.binary1)),
"binary2": jsonModule.decode(jsonModule.encode(testData.binary2)),
"point": { x: 51.5177, y: -0.0968 },
"longInSafeNumber": 9007199254740991
}
}
});
});

it('get by rest', () => {
var rep = http.get(tSrvInfo.appUrlBase + `/test_fields_type/${ids[0]}`, {
headers: {
'Content-Type': 'application/json'
},
});
assert.equal(rep.statusCode, 200);
var result = util.omit(rep.json(), 'createdAt', 'updatedAt');
assert.deepEqual(result, {
"id": ids[0],
"name1": "name1",
"name2": "name2",
"profile": { foo: 'bar' },
"binary1": jsonModule.decode(jsonModule.encode(testData.binary1)),
"binary2": jsonModule.decode(jsonModule.encode(testData.binary2)),
"point": { x: 51.5177, y: -0.0968 },
"longInSafeNumber": 9007199254740991
});
});

it('find', () => {
var rep = http.post(tSrvInfo.appUrlBase + ``, {
headers: {
'Content-Type': 'application/graphql'
},
body: `{
find_test_fields_type(
where:{
id: {
eq: "${ids[0]}"
}
}
){
id,
name1
name2
profile
binary1
binary2
point
longInSafeNumber
}
}`
});

assert.equal(rep.statusCode, 200);

const result = rep.json();
assert.deepEqual(result, {
"data": {
"find_test_fields_type": [
{
"id": ids[0],
"name1": "name1",
"name2": "name2",
"profile": { foo: 'bar' },
"binary1": jsonModule.decode(jsonModule.encode(testData.binary1)),
"binary2": jsonModule.decode(jsonModule.encode(testData.binary2)),
"point": { x: 51.5177, y: -0.0968 },
"longInSafeNumber": 9007199254740991
}
]
}
});
});

it('find by rest', () => {
var rep = http.get(tSrvInfo.appUrlBase + `/test_fields_type`, {
headers: {
'Content-Type': 'application/json'
},
});
assert.equal(rep.statusCode, 200);
var results = rep.json().map(item => {
return util.omit(item, 'createdAt', 'updatedAt')
});
assert.deepEqual(results, [
{
"id": ids[0],
"name1": "name1",
"name2": "name2",
"profile": { foo: 'bar' },
"binary1": jsonModule.decode(jsonModule.encode(testData.binary1)),
"binary2": jsonModule.decode(jsonModule.encode(testData.binary2)),
"point": { x: 51.5177, y: -0.0968 },
"longInSafeNumber": 9007199254740991
}
]);
});
});

if (require.main === module) {
test.run(console.DEBUG);
process.exit();
}
1 change: 1 addition & 0 deletions demo/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('fib-app', function () {
require('./reverse');

require('./acl');
require('./graphql-types');
require('./graphql');
require('./nographql');
require('./query-filter');
Expand Down
70 changes: 35 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
],
"dependencies": {
"@fibjs/types": "^0.35.0",
"@fxjs/orm": "^1.16.0",
"@fxjs/orm": "^1.16.6",
"fib-graphql": "^1.0.0",
"fib-pool": "^1.6.0",
"fib-rpc": "^0.5.3",
Expand Down
Loading

0 comments on commit dfb8421

Please sign in to comment.