laconic-console/src/resolvers.js
Prathamesh Musale 139fb37bef Implement pagination for registry records table (#60)
Part of [Create a public laconicd testnet](https://www.notion.so/Create-a-public-laconicd-testnet-896a11bdd8094eff8f1b49c0be0ca3b8)
Handles cerc-io/laconic-console#59
Requires cerc-io/registry-sdk#27

![image](/attachments/095cf131-19ef-4acc-9ffe-bcbe2f9dad77)
![image](/attachments/684722d3-b9df-44ae-8622-5bacd2dc2a3f)

Co-authored-by: IshaVenikar <ishavenikar7@gmail.com>
Reviewed-on: cerc-io/laconic-console#60
Co-authored-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
Co-committed-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
2024-09-06 05:23:17 +00:00

113 lines
2.7 KiB
JavaScript

//
// Copyright 2020 DXOS.org
//
import debug from 'debug';
import { Registry } from '@cerc-io/registry-sdk';
import { getServiceUrl } from './util/config';
const log = debug('laconic:console:client:resolvers');
const timestamp = () => new Date().toUTCString();
const MAX_LOG_LENGTH = 200;
/**
* Resolvers
* https://www.apollographql.com/docs/tutorial/local-state/#local-resolvers
* @param config
*/
export const createResolvers = config => {
const endpoint = getServiceUrl(config, 'wns.server', { absolute: true });
const registry = new Registry(endpoint);
// TODO(burdon): Errors swallowed!
// Oldest to latest.
let cachedLog = [];
return {
Query: {
wns_status: async () => {
log('WNS status...');
const data = await registry.getStatus();
return {
__typename: 'JSONResult',
timestamp: timestamp(),
// NOTE: Hack since this should be a string according to the schema.
json: data
};
},
wns_records: async (_, { attributes }) => {
log('WNS records...');
const {limit, offset, ...queryAttributes } = attributes;
const data = await registry.queryRecords(queryAttributes, false, false, limit, offset);
return {
__typename: 'JSONResult',
timestamp: timestamp(),
// NOTE: Hack since this should be a string according to the schema.
json: JSON.stringify(data)
};
},
wns_log: async () => {
log('WNS log...');
// Cache and merge previous state.
const data = await registry.getLogs();
let i = data.findIndex(line => line === cachedLog[cachedLog.length - 1]);
if (i === -1) {
cachedLog = data;
} else {
i++;
for (; i < data.length - 1; i++) {
cachedLog.push(data[i]);
}
// Trim.
cachedLog.splice(0, cachedLog.length - MAX_LOG_LENGTH);
}
return {
__typename: 'JSONLog',
timestamp: timestamp(),
log: [...cachedLog].reverse()
};
},
signal_status: async () => {
log('Signal status...');
const url = getServiceUrl(config, 'signal.api', { path: 'status' });
const res = await fetch(url);
return {
__typename: 'JSONResult',
timestamp: timestamp(),
// NOTE: Hack since this should be a string according to the schema.
json: res.json()
};
},
signal_log: async () => {
log('Signal log...');
return {
__typename: 'JSONLog',
timestamp: timestamp(),
log: []
};
}
}
};
};