import Fastify, { type FastifyInstance } from 'fastify';
import { SubgraphCheckExtensionPayload, SubgraphCheckExtensionReply } from './types';
import { verifySignature } from './utils';
const YOUR_SECRET = '...';
const server: FastifyInstance = Fastify({ logger: true });
server.post<{
Body: SubgraphCheckExtensionPayload,
Reply: SubgraphCheckExtensionReply,
}>(
'/check-schema',
{
config: { rawBody: true }
},
async (req, res) => {
if (!req.body) {
res.code(400);
return { errorMessage: 'Bad request' };
}
const { body } = req;
const cosmoSignature = req.headers['x-cosmo-signature-256'];
if (!verifySignature(body, cosmoSignature, YOUR_SECRET)) {
res.code(400);
return { errorMessage: 'Bad request' };
}
// We can run checks conditionally
if (body.namespace.name !== 'default' || body.subgraph?.name !== 'family') {
// We only want to overwrite the lint issues for the subgraph `family` on the namespace `default`
res.code(204);
return;
}
return {
lintIssues: [
{
lintRuleType: 'TYPE_SUFFIX',
severity: 1,
message: 'Type names should use the prefix `Type`',
issueLocation: {
line: 9,
column: 6,
endLine: 9,
endColumn: 11,
},
},
],
};
},
);
const start = async () => {
try {
await server.listen({ port: 4000 })
const address = server.server.address()
const port = typeof address === 'string' ? address : address?.port
} catch (err) {
server.log.error(err)
process.exit(1)
}
};
start()