Skip to content

Commit

Permalink
fix(@whook/graphql): fix GraphQL modules
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed Aug 18, 2023
1 parent bdc557c commit 0357eac
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
34 changes: 22 additions & 12 deletions packages/whook-graphql/src/handlers/postGraphQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,31 +141,41 @@ async function postGraphQL<T extends Record<string, unknown>>(
}
}

return {
const response = {
status: httpGraphQLResponse.status || 200,
// Remove content related headers and lowercase them
headers: Object.keys(httpGraphQLResponse.headers || {})
.filter((key) => !/content-\w+/i.test(key))
.reduce(
(keptsHeaders, key) => ({
...keptsHeaders,
[key.toLowerCase()]: httpGraphQLResponse.headers?.[key],
}),
{},
),
headers: _cleanupGraphQLHeaders(httpGraphQLResponse.headers || {}),
body: JSON.parse(responseBody),
};

return response;
} catch (err) {
if ('HttpQueryError' === (err as Error).name) {
log('debug', '💥 - Got a GraphQL error!');
log('debug-stack', printStackTrace(err as Error));

return {
body: JSON.parse((err as Error).message),
status: (err as { statusCode: number }).statusCode,
headers: (err as YHTTPError).headers as WhookHeaders,
headers: _cleanupGraphQLHeaders(
((err as YHTTPError).headers as unknown as HeaderMap) || {},
),
};
}

throw YHTTPError.cast(err as Error, 500, 'E_GRAPH_QL');
}
}

// Remove content related headers and lowercase them
// Also remove identity symbol that ain't valid header
function _cleanupGraphQLHeaders(headers: HeaderMap): WhookHeaders {
return Object.keys(headers || {})
.filter((key) => key !== '__identity' && !/content-\w+/i.test(key))
.reduce(
(keptsHeaders, key) => ({
...keptsHeaders,
[key.toLowerCase()]: headers?.[key],
}),
{},
);
}
6 changes: 3 additions & 3 deletions packages/whook-graphql/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import type { AuthenticationService } from '@whook/authorization';
import type { WhookGraphQLContextFunction } from './index.js';
import type { Logger, TimeService } from 'common-services';

describe.skip('GraphQL server', () => {
describe('GraphQL server', () => {
const BASE_PATH = '/v1';
const PORT = 5555;
const HOST = 'localhost';
Expand Down Expand Up @@ -144,14 +144,15 @@ describe.skip('GraphQL server', () => {
async () => $autoload,
),
);
$.register(constant('PROJECT_DIR', '/home/whoami/projects'));
$.register(constant('BASE_PATH', BASE_PATH));
$.register(constant('API', API));
$.register(constant('ENV', {}));
$.register(constant('NODE_ENV', 'test'));
$.register(constant('APP_ENV', 'test'));
$.register(constant('PORT', PORT));
$.register(constant('HOST', HOST));
$.register(constant('DEBUG_NODE_ENVS', []));
$.register(constant('NODE_ENVS', ['test']));
$.register(constant('MECHANISMS', [BEARER_MECHANISM, BASIC_MECHANISM]));
$.register(constant('logger', logger as Logger));
$.register(constant('time', time));
Expand All @@ -163,7 +164,6 @@ describe.skip('GraphQL server', () => {
name: 'HANDLERS',
type: 'service',
inject: ['getGraphQL', 'postGraphQL'],
singleton: true,
},
async (services) => services,
),
Expand Down

0 comments on commit 0357eac

Please sign in to comment.