Add CORS support to the reference agent

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4844
Co-authored-by: Vishnu Bharathi <4211715+scriptnull@users.noreply.github.com>
GitOrigin-RevId: e5605786732322768c235957c31f29ab6129c658
This commit is contained in:
Daniel Chambers 2022-06-29 09:59:55 +10:00 committed by hasura-bot
parent 5348163d6d
commit e3d8221f0c
3 changed files with 54 additions and 2 deletions

View File

@ -7,9 +7,9 @@
"": {
"name": "dc-agent-reference",
"version": "1.0.0",
"hasInstallScript": true,
"license": "Apache-2.0",
"dependencies": {
"@fastify/cors": "^7.0.0",
"fastify": "^3.29.0",
"openapi3-ts": "^2.0.2",
"pino-pretty": "^8.0.0",
@ -43,6 +43,15 @@
"ajv": "^6.12.6"
}
},
"node_modules/@fastify/cors": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/@fastify/cors/-/cors-7.0.0.tgz",
"integrity": "sha512-nlo6ScwagBNJacAZD3KX90xjWLIoV0vN9QqoX1wUE9ZeZMdvkVkMZCGlxEtr00NshV0X5wDge4w5rwox7rRzSg==",
"dependencies": {
"fastify-plugin": "^3.0.0",
"vary": "^1.1.2"
}
},
"node_modules/@fastify/error": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/@fastify/error/-/error-2.0.0.tgz",
@ -403,6 +412,11 @@
"tiny-lru": "^8.0.1"
}
},
"node_modules/fastify-plugin": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/fastify-plugin/-/fastify-plugin-3.0.1.tgz",
"integrity": "sha512-qKcDXmuZadJqdTm6vlCqioEbyewF60b/0LOFCcYN1B6BIZGlYJumWWOYs70SFYLDAH4YqdE1cxH/RKMG7rFxgA=="
},
"node_modules/fastq": {
"version": "1.13.0",
"resolved": "https://registry.npmjs.org/fastq/-/fastq-1.13.0.tgz",
@ -921,6 +935,14 @@
"integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==",
"dev": true
},
"node_modules/vary": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
"integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==",
"engines": {
"node": ">= 0.8"
}
},
"node_modules/wrappy": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
@ -987,6 +1009,15 @@
"ajv": "^6.12.6"
}
},
"@fastify/cors": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/@fastify/cors/-/cors-7.0.0.tgz",
"integrity": "sha512-nlo6ScwagBNJacAZD3KX90xjWLIoV0vN9QqoX1wUE9ZeZMdvkVkMZCGlxEtr00NshV0X5wDge4w5rwox7rRzSg==",
"requires": {
"fastify-plugin": "^3.0.0",
"vary": "^1.1.2"
}
},
"@fastify/error": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/@fastify/error/-/error-2.0.0.tgz",
@ -1289,6 +1320,11 @@
"tiny-lru": "^8.0.1"
}
},
"fastify-plugin": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/fastify-plugin/-/fastify-plugin-3.0.1.tgz",
"integrity": "sha512-qKcDXmuZadJqdTm6vlCqioEbyewF60b/0LOFCcYN1B6BIZGlYJumWWOYs70SFYLDAH4YqdE1cxH/RKMG7rFxgA=="
},
"fastq": {
"version": "1.13.0",
"resolved": "https://registry.npmjs.org/fastq/-/fastq-1.13.0.tgz",
@ -1680,6 +1716,11 @@
"integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==",
"dev": true
},
"vary": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
"integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg=="
},
"wrappy": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",

View File

@ -21,6 +21,7 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@fastify/cors": "^7.0.0",
"fastify": "^3.29.0",
"openapi3-ts": "^2.0.2",
"pino-pretty": "^8.0.0",

View File

@ -1,4 +1,5 @@
import Fastify from 'fastify';
import Fastify from 'fastify';
import FastifyCors from '@fastify/cors';
import { SchemaResponse } from './types/schema';
import { ProjectedRow, QueryRequest } from './types/query';
import { filterAvailableTables, getSchema, loadStaticData } from './data';
@ -10,6 +11,15 @@ const port = Number(process.env.PORT) || 8100;
const server = Fastify({ logger: { prettyPrint: true } });
let staticData = {};
server.register(FastifyCors, {
// Accept all origins of requests. This must be modified in
// a production setting to be specific allowable list
// See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
origin: true,
methods: ["GET", "POST", "OPTIONS"],
allowedHeaders: ["X-Hasura-DataConnector-Config", "X-Hasura-DataConnector-SourceName"]
});
server.get<{ Reply: CapabilitiesResponse }>("/capabilities", async (request, _response) => {
server.log.info({ headers: request.headers, query: request.body, }, "capabilities.request");
return capabilitiesResponse;