makeGatewayGraphQLRequestContext.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. export function makeGatewayGraphQLRequestContext(as4RequestContext, server, internals) {
  2. const request = {};
  3. if ('query' in as4RequestContext.request) {
  4. request.query = as4RequestContext.request.query;
  5. }
  6. if ('operationName' in as4RequestContext.request) {
  7. request.operationName = as4RequestContext.request.operationName;
  8. }
  9. if ('variables' in as4RequestContext.request) {
  10. request.variables = as4RequestContext.request.variables;
  11. }
  12. if ('extensions' in as4RequestContext.request) {
  13. request.extensions = as4RequestContext.request.extensions;
  14. }
  15. if (as4RequestContext.request.http) {
  16. const as4http = as4RequestContext.request.http;
  17. const needQuestion = as4http.search !== '' && !as4http.search.startsWith('?');
  18. request.http = {
  19. method: as4http.method,
  20. url: `https://unknown-url.invalid/${needQuestion ? '?' : ''}${as4http.search}`,
  21. headers: new FetcherHeadersForHeaderMap(as4http.headers),
  22. };
  23. }
  24. const response = {
  25. http: {
  26. headers: new FetcherHeadersForHeaderMap(as4RequestContext.response.http.headers),
  27. get status() {
  28. return as4RequestContext.response.http.status;
  29. },
  30. set status(newStatus) {
  31. as4RequestContext.response.http.status = newStatus;
  32. },
  33. },
  34. };
  35. return {
  36. request,
  37. response,
  38. logger: server.logger,
  39. schema: as4RequestContext.schema,
  40. schemaHash: 'schemaHash no longer exists in Apollo Server 4',
  41. context: as4RequestContext.contextValue,
  42. cache: server.cache,
  43. queryHash: as4RequestContext.queryHash,
  44. document: as4RequestContext.document,
  45. source: as4RequestContext.source,
  46. operationName: as4RequestContext.operationName,
  47. operation: as4RequestContext.operation,
  48. errors: as4RequestContext.errors,
  49. metrics: as4RequestContext.metrics,
  50. debug: internals.includeStacktraceInErrorResponses,
  51. overallCachePolicy: as4RequestContext.overallCachePolicy,
  52. requestIsBatched: as4RequestContext.requestIsBatched,
  53. };
  54. }
  55. class FetcherHeadersForHeaderMap {
  56. constructor(map) {
  57. this.map = map;
  58. }
  59. append(name, value) {
  60. if (this.map.has(name)) {
  61. this.map.set(name, this.map.get(name) + ', ' + value);
  62. }
  63. else {
  64. this.map.set(name, value);
  65. }
  66. }
  67. delete(name) {
  68. this.map.delete(name);
  69. }
  70. get(name) {
  71. return this.map.get(name) ?? null;
  72. }
  73. has(name) {
  74. return this.map.has(name);
  75. }
  76. set(name, value) {
  77. this.map.set(name, value);
  78. }
  79. entries() {
  80. return this.map.entries();
  81. }
  82. keys() {
  83. return this.map.keys();
  84. }
  85. values() {
  86. return this.map.values();
  87. }
  88. [Symbol.iterator]() {
  89. return this.map.entries();
  90. }
  91. }
  92. //# sourceMappingURL=makeGatewayGraphQLRequestContext.js.map