Remove hard-coded record types #52
@ -10,15 +10,14 @@ const attributeField = `
|
|||||||
attributes {
|
attributes {
|
||||||
key
|
key
|
||||||
value {
|
value {
|
||||||
null
|
... on BooleanValue { bool: value }
|
||||||
int
|
... on IntValue { int: value }
|
||||||
float
|
... on FloatValue { float: value }
|
||||||
string
|
... on StringValue { string: value }
|
||||||
boolean
|
... on BytesValue { bytes: value }
|
||||||
json
|
... on LinkValue { link: value }
|
||||||
reference {
|
... on ArrayValue { array: value { __typename } }
|
||||||
id
|
... on MapValue { map: value { key mapping: value { __typename } } }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
@ -257,9 +256,11 @@ export class RegistryClient {
|
|||||||
attributes: Util.toGQLAttributes(attributes),
|
attributes: Util.toGQLAttributes(attributes),
|
||||||
all
|
all
|
||||||
};
|
};
|
||||||
|
console.debug("[DEBUG] variables", variables);
|
||||||
|
|
||||||
let result = (await this._graph(query)(variables))['queryRecords'];
|
let result = (await this._graph(query)(variables))['queryRecords'];
|
||||||
result = RegistryClient.prepareAttributes('attributes')(result);
|
result = RegistryClient.prepareAttributes('attributes')(result);
|
||||||
|
console.debug("[DEBUG] prepared result", result);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -50,18 +50,18 @@ describe('Querying', () => {
|
|||||||
expect(records.length).toBeGreaterThanOrEqual(1);
|
expect(records.length).toBeGreaterThanOrEqual(1);
|
||||||
|
|
||||||
const { attributes: { repo_registration_record_cid: record_repo_registration_record_cid } } = records[0];
|
const { attributes: { repo_registration_record_cid: record_repo_registration_record_cid } } = records[0];
|
||||||
expect(repo_registration_record_cid).toBe(record_repo_registration_record_cid);
|
expect(repo_registration_record_cid).toStrictEqual(record_repo_registration_record_cid);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Query records by attributes.', async () => {
|
test('Query records by attributes.', async () => {
|
||||||
const { version, name } = watcher.record;
|
const { version, url } = watcher.record;
|
||||||
const records = await registry.queryRecords({ version, name }, true);
|
const records = await registry.queryRecords({ version, url }, true);
|
||||||
expect(records.length).toBe(1);
|
expect(records.length).toBe(1);
|
||||||
|
|
||||||
[ watcher ] = records;
|
[ watcher ] = records;
|
||||||
const { attributes: { version: recordVersion, name: recordName } } = watcher;
|
const { attributes: { version: recordVersion, url: recordName } } = watcher;
|
||||||
expect(recordVersion).toBe(version);
|
expect(recordVersion).toBe(version);
|
||||||
expect(recordName).toBe(name);
|
expect(recordName).toBe(url);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Query records by id.', async () => {
|
test('Query records by id.', async () => {
|
||||||
@ -75,6 +75,7 @@ describe('Querying', () => {
|
|||||||
expect(record.id).toBe(watcher.id);
|
expect(record.id).toBe(watcher.id);
|
||||||
// temp fix
|
// temp fix
|
||||||
expect(record.attributes.repo_registration_record_cid).toBeDefined();
|
expect(record.attributes.repo_registration_record_cid).toBeDefined();
|
||||||
expect(record.attributes.repo_registration_record_cid).toHaveLength(46);
|
expect(record.attributes.repo_registration_record_cid).toHaveProperty("/");
|
||||||
|
expect(record.attributes.repo_registration_record_cid["/"]).toHaveLength(46);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
record:
|
record:
|
||||||
type: WebsiteRegistrationRecord
|
type: WebsiteRegistrationRecord
|
||||||
url: 'https://cerc.io'
|
url: 'https://cerc.io'
|
||||||
repo_registration_record_cid: QmSnuWmxptJZdLJpKRarxBMS2Ju2oANVrgbr2xWbie9b2D
|
repo_registration_record_cid:
|
||||||
build_artifact_cid: QmP8jTG1m9GSDJLCbeWhVSVgEzCPPwXRdCRuJtQ5Tz9Kc9
|
/: QmSnuWmxptJZdLJpKRarxBMS2Ju2oANVrgbr2xWbie9b2D
|
||||||
tls_cert_cid: QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR
|
build_artifact_cid:
|
||||||
|
/: QmP8jTG1m9GSDJLCbeWhVSVgEzCPPwXRdCRuJtQ5Tz9Kc9
|
||||||
|
tls_cert_cid:
|
||||||
|
/: QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR
|
||||||
version: 1.0.23
|
version: 1.0.23
|
||||||
|
88
src/util.ts
88
src/util.ts
@ -10,21 +10,21 @@ export class Util {
|
|||||||
/**
|
/**
|
||||||
* Sorts JSON object.
|
* Sorts JSON object.
|
||||||
*/
|
*/
|
||||||
static sortJSON(object: any) {
|
static sortJSON(obj: any) {
|
||||||
if (object instanceof Array) {
|
if (obj instanceof Array) {
|
||||||
for (let i = 0; i < object.length; i++) {
|
for (let i = 0; i < obj.length; i++) {
|
||||||
object[i] = Util.sortJSON(object[i]);
|
obj[i] = Util.sortJSON(obj[i]);
|
||||||
}
|
}
|
||||||
return object;
|
return obj;
|
||||||
}
|
}
|
||||||
if (typeof object !== 'object' || object === null) return object;
|
if (typeof obj !== 'object' || obj === null) return obj;
|
||||||
|
|
||||||
let keys = Object.keys(object);
|
let keys = Object.keys(obj);
|
||||||
keys = keys.sort();
|
keys = keys.sort();
|
||||||
const newObject: {[key: string]: any} = {};
|
const newObject: {[key: string]: any} = {};
|
||||||
|
|
||||||
for (let i = 0; i < keys.length; i++) {
|
for (let i = 0; i < keys.length; i++) {
|
||||||
newObject[keys[i]] = Util.sortJSON(object[keys[i]]);
|
newObject[keys[i]] = Util.sortJSON(obj[keys[i]]);
|
||||||
}
|
}
|
||||||
return newObject;
|
return newObject;
|
||||||
}
|
}
|
||||||
@ -32,31 +32,41 @@ export class Util {
|
|||||||
/**
|
/**
|
||||||
* Marshal object into gql 'attributes' variable.
|
* Marshal object into gql 'attributes' variable.
|
||||||
*/
|
*/
|
||||||
static toGQLAttributes(object: any) {
|
static toGQLAttributes(obj: any) {
|
||||||
|
console.log("[DEBUG] toGQLAttributes", obj);
|
||||||
const vars: any[] = [];
|
const vars: any[] = [];
|
||||||
|
Object.keys(obj).forEach(key => {
|
||||||
Object.keys(object).forEach(key => {
|
vars.push({ key, value: this.toGQLValue(obj[key]) });
|
||||||
let type: string = typeof object[key];
|
|
||||||
if (object[key] === null) {
|
|
||||||
vars.push({ key, value: { 'null': true } });
|
|
||||||
} else if (type === 'number') {
|
|
||||||
type = (object[key] % 1 === 0) ? 'int' : 'float';
|
|
||||||
vars.push({ key, value: { [type]: object[key] } });
|
|
||||||
} else if (type === 'string') {
|
|
||||||
vars.push({ key, value: { 'string': object[key] } });
|
|
||||||
} else if (type === 'boolean') {
|
|
||||||
vars.push({ key, value: { 'boolean': object[key] } });
|
|
||||||
} else if (type === 'object') {
|
|
||||||
const nestedObject = object[key];
|
|
||||||
if (nestedObject['/'] !== undefined) {
|
|
||||||
vars.push({ key, value: { 'reference': { id: nestedObject['/'] } } });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return vars;
|
return vars;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static toGQLValue(obj: any) {
|
||||||
|
if (obj === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
let type: string = typeof obj;
|
||||||
|
switch (type) {
|
||||||
|
case 'number':
|
||||||
|
type = (obj % 1 === 0) ? 'int' : 'float';
|
||||||
|
return { [type]: obj };
|
||||||
|
case 'string':
|
||||||
|
return { 'string': obj };
|
||||||
|
case 'boolean':
|
||||||
|
return { 'boolean': obj };
|
||||||
|
case 'object':
|
||||||
|
if (obj['/'] !== undefined) {
|
||||||
|
return { 'link': obj['/'] };
|
||||||
|
}
|
||||||
|
if (obj instanceof Array) {
|
||||||
|
return { 'array': obj };
|
||||||
|
}
|
||||||
|
return { 'map': obj };
|
||||||
|
default:
|
||||||
|
throw new Error(`Unknown object type '${type}': ${obj}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unmarshal attributes array to object.
|
* Unmarshal attributes array to object.
|
||||||
*/
|
*/
|
||||||
@ -64,24 +74,20 @@ export class Util {
|
|||||||
const res: {[key: string]: any} = {};
|
const res: {[key: string]: any} = {};
|
||||||
|
|
||||||
attributes.forEach(attr => {
|
attributes.forEach(attr => {
|
||||||
if (attr.value.null) {
|
res[attr.key] = this.fromGQLValue(attr.value);
|
||||||
res[attr.key] = null;
|
|
||||||
} else if (attr.value.json) {
|
|
||||||
res[attr.key] = JSON.parse(attr.value.json);
|
|
||||||
} else if (attr.value.reference) {
|
|
||||||
// Convert GQL reference to IPLD style link.
|
|
||||||
const ref = attr.value.reference;
|
|
||||||
res[attr.key] = { '/': ref.id };
|
|
||||||
} else {
|
|
||||||
const { values, null: n, ...types } = attr.value;
|
|
||||||
const value = Object.values(types).find(v => v !== null);
|
|
||||||
res[attr.key] = value;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static fromGQLValue(obj: any) {
|
||||||
|
const present = Object.keys(obj).find(k => obj[k] !== null);
|
||||||
|
if (present === undefined) {
|
||||||
|
throw new Error('Object has no non-null values');
|
||||||
|
}
|
||||||
|
return obj[present];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get record content ID.
|
* Get record content ID.
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user