Skip to content

Commit

Permalink
Merge pull request #33 from wayfair-incubator/mfaga-support-cors-options
Browse files Browse the repository at this point in the history
  • Loading branch information
mjfaga authored Nov 2, 2023
2 parents 76c9190 + a39f3af commit 9ae05a8
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nodejs 14.17.3
nodejs 18.15.0
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ and this project adheres to

## [Unreleased]

## [v1.2.0] - 2023-11-01

### Added

- feat: allow explicit passing of cors options

## [v1.1.2] - 2023-08-21

### Fixed
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wayfair/gqmock",
"version": "1.1.2",
"version": "1.2.0",
"description": "GQMock - GraphQL Mocking Service",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -49,6 +49,7 @@
"@faker-js/faker": "^7.6.0",
"@graphql-tools/mock": "^8.7.12",
"chalk": "4.1.2",
"cors": "^2.8.5",
"escape-string-regexp": "^4.0.0",
"express": "^4.18.1",
"graphql": "^16.6.0",
Expand All @@ -61,6 +62,7 @@
"@babel/core": "^7.18.13",
"@babel/preset-env": "^7.18.10",
"@babel/preset-typescript": "^7.18.6",
"@types/cors": "^2.8.15",
"@types/jest": "^29.1.2",
"@typescript-eslint/eslint-plugin": "^5.35.1",
"@typescript-eslint/parser": "^5.35.1",
Expand Down
2 changes: 2 additions & 0 deletions src/GraphqlMockingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type GraphqlMockingServiceOptions = {
port?: number;
subgraph?: boolean;
graphQLIDE?: GraphQLIDE;
corsOptions?: any; // eslint-disable-line
};

export default class GraphqlMockingService {
Expand All @@ -15,6 +16,7 @@ export default class GraphqlMockingService {
this.server = new MockServer({
port: options.port,
graphQLIDE: options.graphQLIDE,
corsOptions: options.corsOptions,
});
this.subgraph = options.subgraph || false;
}
Expand Down
10 changes: 9 additions & 1 deletion src/MockServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {GraphQLIDE} from './GraphQLIDE';
type MockServerOptions = {
port?: number;
graphQLIDE?: GraphQLIDE;
corsOptions?: any; // eslint-disable-line
};

type SchemaRegistrationOptions = {
Expand All @@ -15,14 +16,21 @@ type SchemaRegistrationOptions = {
class MockServer {
readonly port: number;
readonly graphQLIDE: GraphQLIDE;
readonly corsOptions: any; // eslint-disable-line

private appServer;
constructor(options: MockServerOptions) {
this.port = options.port || 5000;
this.graphQLIDE = options.graphQLIDE || GraphQLIDE.ApolloSandbox;
this.corsOptions = options.corsOptions;
}

async start(): Promise<void> {
const app = createApp({graphQLIDE: this.graphQLIDE, port: this.port});
const app = createApp({
graphQLIDE: this.graphQLIDE,
port: this.port,
corsOptions: this.corsOptions,
});

this.appServer = await app.listen({port: this.port}, () =>
console.log(
Expand Down
5 changes: 4 additions & 1 deletion src/utilities/createApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@ import {GraphQLIDE} from '../GraphQLIDE';
/**
* @param {object} root0 - The root object
* @param {GraphQLIDE} root0.graphQLIDE - The type of GraphQL IDE to use
* @param {corsOptions} root0.corsOptions - Options for cors configuration

Check warning on line 13 in src/utilities/createApp.ts

View workflow job for this annotation

GitHub Actions / lint

The type 'corsOptions' is undefined

Check warning on line 13 in src/utilities/createApp.ts

View workflow job for this annotation

GitHub Actions / publish

The type 'corsOptions' is undefined
* @param {number} root0.port - The port to run the server on
* @returns {express.Express} An express server instance
*/
export default function createApp({
graphQLIDE,
port,
corsOptions,
}: {
graphQLIDE: GraphQLIDE;
port: number;
corsOptions: any; // eslint-disable-line
}): express.Express {
const app = express();
const seedManager = new SeedManager();
const apolloServerManager = new ApolloServerManager();

app.use(express.json({limit: '5mb'}));
app.use(cors());
app.use(corsOptions ? cors(corsOptions) : cors());
app.use(
express.urlencoded({
extended: true,
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1668,6 +1668,13 @@
dependencies:
"@types/node" "*"

"@types/cors@^2.8.15":
version "2.8.15"
resolved "https://registry.yarnpkg.com/@types/cors/-/cors-2.8.15.tgz#eb143aa2f8807ddd78e83cbff141bbedd91b60ee"
integrity sha512-n91JxbNLD8eQIuXDIChAN1tCKNWCEgpceU9b7ZMbFA+P+Q4yIeh80jizFLEvolRPc1ES0VdwFlGv+kJTSirogw==
dependencies:
"@types/node" "*"

"@types/express-serve-static-core@^4.17.18", "@types/express-serve-static-core@^4.17.30":
version "4.17.31"
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.31.tgz#a1139efeab4e7323834bb0226e62ac019f474b2f"
Expand Down

0 comments on commit 9ae05a8

Please sign in to comment.