Skip to content

Commit

Permalink
fix(utils): support other map-like interfaces even if they don't impl…
Browse files Browse the repository at this point in the history
…ement WHATWG headers fully (#5239)

* fix(utils): support other map-like interfaces even if they don't implement WHATWG headers fully

* chore(dependencies): updated changesets for modified dependencies

* Tests

* req.headers might be a map

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
ardatan and github-actions[bot] authored Mar 21, 2023
1 parent 337ae46 commit 3f658a7
Show file tree
Hide file tree
Showing 29 changed files with 214 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_config-5239-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/config": patch
---
dependencies updates:
- Updated dependency [`@whatwg-node/fetch@^0.8.3` ↗︎](https://www.npmjs.com/package/@whatwg-node/fetch/v/0.8.3) (from `^0.8.0`, in `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_odata-5239-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/odata": patch
---
dependencies updates:
- Updated dependency [`@whatwg-node/fetch@^0.8.3` ↗︎](https://www.npmjs.com/package/@whatwg-node/fetch/v/0.8.3) (from `^0.8.0`, in `dependencies`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/plugin-http-cache": patch
---
dependencies updates:
- Updated dependency [`@whatwg-node/fetch@^0.8.3` ↗︎](https://www.npmjs.com/package/@whatwg-node/fetch/v/0.8.3) (from `0.8.1`, in `dependencies`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/plugin-http-details-extensions": patch
---
dependencies updates:
- Updated dependency [`@whatwg-node/fetch@^0.8.3` ↗︎](https://www.npmjs.com/package/@whatwg-node/fetch/v/0.8.3) (from `0.8.1`, in `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_plugin-snapshot-5239-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/plugin-snapshot": patch
---
dependencies updates:
- Updated dependency [`@whatwg-node/fetch@^0.8.3` ↗︎](https://www.npmjs.com/package/@whatwg-node/fetch/v/0.8.3) (from `0.8.1`, in `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_runtime-5239-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/runtime": patch
---
dependencies updates:
- Updated dependency [`@whatwg-node/fetch@^0.8.3` ↗︎](https://www.npmjs.com/package/@whatwg-node/fetch/v/0.8.3) (from `^0.8.0`, in `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@omnigraph_json-schema-5239-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@omnigraph/json-schema": patch
---
dependencies updates:
- Updated dependency [`@whatwg-node/fetch@^0.8.3` ↗︎](https://www.npmjs.com/package/@whatwg-node/fetch/v/0.8.3) (from `^0.8.0`, in `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/grumpy-trees-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-mesh/utils': patch
---

Support other Map-like interfaces even if they don't implement WHATWG Headers fully
5 changes: 5 additions & 0 deletions .changeset/json-machete-5239-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"json-machete": patch
---
dependencies updates:
- Updated dependency [`@whatwg-node/fetch@^0.8.3` ↗︎](https://www.npmjs.com/package/@whatwg-node/fetch/v/0.8.3) (from `^0.8.0`, in `dependencies`)
2 changes: 1 addition & 1 deletion examples/json-schema-subscriptions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@graphql-mesh/cli": "0.82.27",
"@graphql-mesh/json-schema": "0.37.21",
"@graphql-mesh/plugin-live-query": "0.2.17",
"@whatwg-node/fetch": "0.8.1",
"@whatwg-node/fetch": "^0.8.3",
"@whatwg-node/router": "0.3.0",
"graphql": "16.6.0"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs-apollo-example/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default async function createApolloServer() {
cache,
executor: async requestContext => {
const { schema, execute, contextFactory } = getEnveloped({
req: requestContext.request.http,
request: requestContext.request.http,
});

return execute({
Expand Down
2 changes: 1 addition & 1 deletion examples/openapi-subscriptions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dependencies": {
"@graphql-mesh/cli": "0.82.27",
"@graphql-mesh/openapi": "0.35.23",
"@whatwg-node/fetch": "0.8.1",
"@whatwg-node/fetch": "^0.8.3",
"@whatwg-node/router": "^0.3.0",
"concurrently": "7.6.0",
"graphql": "16.6.0",
Expand Down
6 changes: 1 addition & 5 deletions examples/openwhisk-example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ export const main = async function (params) {
params,
);

const headers = {};

response.headers.forEach((value, key) => {
headers[key] = value;
});
const headers = Object.fromEntries(response.headers.entries());

const body = await (headers['content-type'].includes('json') ? response.json() : response.text());

Expand Down
2 changes: 1 addition & 1 deletion packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@graphql-tools/graphql-file-loader": "7.5.16",
"@graphql-tools/load": "7.8.12",
"@graphql-tools/utils": "9.2.1",
"@whatwg-node/fetch": "^0.8.0",
"@whatwg-node/fetch": "^0.8.3",
"camel-case": "4.1.2",
"param-case": "3.0.4",
"pascal-case": "3.1.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/handlers/odata/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@graphql-mesh/utils": "0.43.17",
"@graphql-tools/delegate": "9.0.28",
"@graphql-tools/utils": "9.2.1",
"@whatwg-node/fetch": "^0.8.0",
"@whatwg-node/fetch": "^0.8.3",
"dataloader": "2.2.2",
"fast-xml-parser": "4.1.3",
"graphql-compose": "9.0.10",
Expand Down
2 changes: 1 addition & 1 deletion packages/json-machete/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@graphql-mesh/utils": "0.43.17",
"@graphql-tools/utils": "9.2.1",
"@json-schema-tools/meta-schema": "1.7.0",
"@whatwg-node/fetch": "^0.8.0",
"@whatwg-node/fetch": "^0.8.3",
"json-pointer": "0.6.2",
"to-json-schema": "0.2.5",
"tslib": "^2.4.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/loaders/json-schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@graphql-mesh/utils": "0.43.17",
"@graphql-tools/utils": "9.2.1",
"@json-schema-tools/meta-schema": "1.7.0",
"@whatwg-node/fetch": "^0.8.0",
"@whatwg-node/fetch": "^0.8.3",
"ajv": "8.12.0",
"ajv-formats": "2.1.1",
"dset": "3.1.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/loaders/soap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"graphql-scalars": "^1.20.4"
},
"devDependencies": {
"@whatwg-node/fetch": "0.8.1"
"@whatwg-node/fetch": "0.8.3"
},
"publishConfig": {
"access": "public",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/deduplicate-request/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"tslib": "^2.4.0"
},
"devDependencies": {
"@whatwg-node/fetch": "0.8.1"
"@whatwg-node/fetch": "0.8.3"
},
"publishConfig": {
"access": "public",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/http-cache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"dependencies": {
"@graphql-mesh/types": "0.91.9",
"@graphql-mesh/utils": "0.43.17",
"@whatwg-node/fetch": "0.8.1",
"@whatwg-node/fetch": "^0.8.3",
"http-cache-semantics": "4.1.1",
"tslib": "^2.4.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/http-details-extensions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@envelop/core": "3.0.6",
"@graphql-mesh/types": "0.91.9",
"@graphql-mesh/utils": "0.43.17",
"@whatwg-node/fetch": "0.8.1",
"@whatwg-node/fetch": "^0.8.3",
"tslib": "^2.4.0"
},
"publishConfig": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/snapshot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@graphql-mesh/string-interpolation": "0.4.2",
"@graphql-mesh/types": "0.91.9",
"@graphql-mesh/utils": "0.43.17",
"@whatwg-node/fetch": "0.8.1",
"@whatwg-node/fetch": "^0.8.3",
"minimatch": "7.3.0",
"tslib": "^2.4.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@graphql-tools/delegate": "9.0.28",
"@graphql-tools/utils": "9.2.1",
"@graphql-tools/wrap": "9.3.7",
"@whatwg-node/fetch": "^0.8.0",
"@whatwg-node/fetch": "^0.8.3",
"tslib": "^2.4.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/get-mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export async function getMesh(options: GetMeshOptions): Promise<MeshInstance> {
// Maybe Node-like environment
if (req?.headers) {
return {
headers: req.headers,
headers: getHeadersObj(req.headers),
};
}
// Fetch environment
Expand Down
4 changes: 3 additions & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@
"tslib": "^2.4.0"
},
"devDependencies": {
"@apollo/server": "4.5.0",
"@types/js-yaml": "4.0.5",
"@types/lodash.topath": "4.5.7",
"@types/object-hash": "3.0.2"
"@types/object-hash": "3.0.2",
"@whatwg-node/fetch": "0.8.3"
},
"publishConfig": {
"access": "public",
Expand Down
40 changes: 27 additions & 13 deletions packages/utils/src/getHeadersObj.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
function headersToJSON(headers: Headers): Record<string, string> {
const obj: Record<string, string> = {};
headers.forEach((value, key) => {
obj[key] = value;
});
return obj;
return Object.fromEntries(headers.entries());
}

export function getHeadersObj(headers: Headers): Record<string, string> {
if (headers == null || !('forEach' in headers)) {
function isHeaders(headers: any): headers is Headers {
return headers != null && 'get' in headers && typeof headers.get === 'function';
}

export function getHeadersObj(headers: Headers | Record<string, string>): Record<string, string> {
if (headers == null || !isHeaders(headers)) {
return headers as any;
}
return new Proxy(
Expand All @@ -17,7 +17,7 @@ export function getHeadersObj(headers: Headers): Record<string, string> {
if (name === 'toJSON') {
return () => headersToJSON(headers);
}
return headers.get(name.toString());
return headers.get(name.toString()) || undefined;
},
has(_target, name) {
if (name === 'toJSON') {
Expand All @@ -26,16 +26,30 @@ export function getHeadersObj(headers: Headers): Record<string, string> {
return headers.has(name.toString());
},
ownKeys(_target) {
const keys: string[] = [];
headers.forEach((_value, name) => {
keys.push(name);
});
return keys;
return [...headers.keys()];
},
set(_target, name, value) {
headers.set(name.toString(), value);
return true;
},
defineProperty(_target, name, descriptor) {
if (descriptor.value != null) {
headers.set(name.toString(), descriptor.value);
}
return true;
},
getOwnPropertyDescriptor(_target, name) {
const value = headers.get(name.toString());
if (value == null) {
return undefined;
}
return {
configurable: true,
enumerable: true,
value,
writable: true,
};
},
deleteProperty(_target, name) {
headers.delete(name.toString());
return true;
Expand Down
39 changes: 39 additions & 0 deletions packages/utils/test/get-headers-obj.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { HeaderMap } from '@apollo/server';
import { Headers } from '@whatwg-node/fetch';
import { getHeadersObj } from '../src/getHeadersObj';

describe('getHeadersObj', () => {
const headersTypes = {
[`WHATWG Headers`]: () => new Headers(),
[`Apollo's HeaderMap`]: () => new HeaderMap(),
[`Standard Map`]: () => new Map(),
};
for (const headersName in headersTypes) {
describe(headersName, () => {
const createHeaders = headersTypes[headersName];
it('should get props correctly from the proxy object after changes on the original object', () => {
const headers = createHeaders();
const headersObj = getHeadersObj(headers);
expect(headersObj).toEqual({});
expect(headersObj.a).toBeUndefined();
headers.set('a', '1');
expect(headersObj).toEqual({ a: '1' });
headers.set('b', '2');
expect(headersObj).toEqual({ a: '1', b: '2' });
headers.delete('a');
expect(headersObj).toEqual({ b: '2' });
});
it('should get props corrects from the original object after changes on the proxy object', () => {
const headers = createHeaders();
const headersObj = getHeadersObj(headers);
expect(headers.get('a')).toBeFalsy();
headersObj.a = '1';
expect(headers.get('a')).toEqual('1');
headersObj.b = '2';
expect(headers.get('b')).toEqual('2');
delete headersObj.a;
expect(headers.get('a')).toBeFalsy();
});
});
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,7 @@ export async function handler(
}
)

const responseHeaders: Record<string, string> = {}

response.headers.forEach((value, name) => {
responseHeaders[name] = value
})
const responseHeaders: Record<string, string> = Object.fromEntries(response.headers.entries())

return {
statusCode: response.status,
Expand Down
Loading

0 comments on commit 3f658a7

Please sign in to comment.