laconic-console/src/resolvers.js
Prathamesh Musale ef73da7ec5 Use registry-sdk and update minor version (#47)
Part of https://www.notion.so/Create-laconic-registry-SDK-d3a636d4aba44f7cbba3bd99b7146811?pvs=23

- Rename `crn` to `lrn`

Co-authored-by: neeraj <neeraj.rtly@gmail.com>
Reviewed-on: cerc-io/laconic-console#47
Co-authored-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
Co-committed-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
2024-03-18 13:23:29 +00:00

111 lines
2.6 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 data = await registry.queryRecords(attributes);
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: []
};
}
}
};
};