Skip to content

Commit

Permalink
update endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Sep 27, 2024
1 parent dc5f80e commit a6f7e1d
Showing 6 changed files with 283 additions and 562 deletions.
58 changes: 1 addition & 57 deletions index.ts
Original file line number Diff line number Diff line change
@@ -78,7 +78,6 @@ async function AntelopeTokenAPI() {
// --------------------------
// --- REST API endpoints ---
// --------------------------

const createUsageEndpoint = (endpoint: UsageEndpoints) => app.get(
// Hono using different syntax than OpenAPI for path parameters
// `/{path_param}` (OpenAPI) VS `/:path_param` (Hono)
@@ -99,7 +98,7 @@ async function AntelopeTokenAPI() {

const path_params = path_params_schema.safeParse(ctx.req.param());
const query_params = query_params_schema.safeParse(ctx.req.query());

if (path_params.success && query_params.success) {
return makeUsageQuery(
ctx,
@@ -123,61 +122,6 @@ async function AntelopeTokenAPI() {
// Create all API endpoints interacting with DB
Object.values(usageOperationsToEndpointsMap).forEach(e => createUsageEndpoint(e));

// ------------------------
// --- GraphQL endpoint ---
// ------------------------

// TODO: Make GraphQL endpoint use the same $SERVER parameter as Swagger if set ?
const schema = buildSchema(await Bun.file("./static/@openapi-to-graphql/graphql/schema.graphql").text());
const filterFields: Array<keyof typeof usageOperationsToEndpointsMap> = ['metrics'];

// @ts-ignore Ignore private field warning for filtering out certain operations from the schema
filterFields.forEach(f => delete schema._queryType._fields[f]);

const rootResolver: RootResolver = async (ctx?: Context) => {
if (ctx) {
// GraphQL resolver uses the same SQL queries backend as the REST API (`makeUsageQuery`)
const createGraphQLUsageResolver = (endpoint: UsageEndpoints) =>
async (args: ValidUserParams<typeof endpoint>) => {
return await (await makeUsageQuery(ctx, endpoint, { ...args })).json();
};


return Object.keys(usageOperationsToEndpointsMap).reduce(
// SQL queries endpoints
(resolver, op) => Object.assign(
resolver,
{
[op]: createGraphQLUsageResolver(usageOperationsToEndpointsMap[op] as UsageEndpoints)
}
),
// Other endpoints
{
health: async () => {
const response = await client.ping();
return response.success ? "OK" : `[500] bad_database_response: ${response.error.message}`;
},
openapi: () => openapi,
metrics: async () => await prometheus.registry.metrics(),
version: () => APP_VERSION
}
);
}
};

// TODO: Find way to log GraphQL queries (need to workaround middleware consuming Request)
// See: https://github.com/honojs/middleware/issues/81
//app.use('/graphql', async (ctx: Context) => logger.trace(await ctx.req.json()))

app.use(
'/graphql',
graphqlServer({
schema,
rootResolver,
graphiql: true, // if `true`, presents GraphiQL when the GraphQL endpoint is loaded in a browser.
})
);

// -------------
// --- Miscs ---
// -------------
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "antelope-token-api",
"description": "Token balances, supply and transfers from the Antelope blockchains",
"version": "6.0.1",
"version": "6.1.0",
"homepage": "https://github.com/pinax-network/antelope-token-api",
"license": "MIT",
"authors": [
@@ -38,7 +38,7 @@
"lint": "export APP_VERSION=$(git rev-parse --short HEAD) && bun run tsc --noEmit --skipLibCheck --pretty",
"start": "export APP_VERSION=$(git rev-parse --short HEAD) && bun index.ts",
"test": "bun test --coverage",
"types": "bun run tsp compile ./src/typespec --output-dir static && bun run openapi-to-graphql ./static/@typespec/openapi3/openapi.json --save static/@openapi-to-graphql/graphql/schema.graphql --simpleNames --singularNames --no-viewer -H 'X-Api-Key:changeme' && bun run kubb",
"types": "bun run tsp compile ./src/typespec --output-dir static && bun run kubb",
"types:check": "bun run tsp compile ./src/typespec --no-emit --pretty --warn-as-error",
"types:format": "bun run tsp format src/typespec/**/*.tsp",
"types:watch": "bun run tsp compile ./src/typespec --watch --pretty --warn-as-error"
Loading

0 comments on commit a6f7e1d

Please sign in to comment.