diff --git a/packages/gatsby/package.json b/packages/gatsby/package.json index 3e1d4e17a85ab..aab6464e48560 100644 --- a/packages/gatsby/package.json +++ b/packages/gatsby/package.json @@ -83,7 +83,7 @@ "event-source-polyfill": "1.0.25", "execa": "^5.1.1", "express": "^4.17.1", - "express-graphql": "^0.12.0", + "graphql-http": "^1.7.0", "express-http-proxy": "^1.6.3", "fastest-levenshtein": "^1.0.12", "fastq": "^1.13.0", diff --git a/packages/gatsby/src/utils/start-server.ts b/packages/gatsby/src/utils/start-server.ts index 71a075e94ef79..f1e6661c63bee 100644 --- a/packages/gatsby/src/utils/start-server.ts +++ b/packages/gatsby/src/utils/start-server.ts @@ -4,9 +4,10 @@ import got, { Method } from "got" import webpack from "webpack" import express from "express" import compression from "compression" -import { graphqlHTTP, OptionsData } from "express-graphql" +import { createHandler as createGraphqlEndpointHandler } from "graphql-http/lib/use/express" +import type { OperationContext } from "graphql-http" import graphiqlExplorer from "gatsby-graphiql-explorer" -import { FragmentDefinitionNode, GraphQLFormattedError, Kind } from "graphql" +import { FragmentDefinitionNode, GraphQLError, Kind } from "graphql" import { slash, uuid } from "gatsby-core-utils" import http from "http" import https from "https" @@ -185,38 +186,38 @@ export async function startServer( app.use( graphqlEndpoint, - graphqlHTTP((): OptionsData => { - const { schema, schemaCustomization } = store.getState() - - if (!schemaCustomization.composer) { - throw new Error( - `A schema composer was not created in time. This is likely a gatsby bug. If you experienced this please create an issue.` - ) - } - return { - schema, - graphiql: false, - extensions(): { [key: string]: unknown } { - return { - enableRefresh: process.env.ENABLE_GATSBY_REFRESH_ENDPOINT, - refreshToken: process.env.GATSBY_REFRESH_TOKEN, - } - }, - context: withResolverContext({ - schema, - schemaComposer: schemaCustomization.composer, + createGraphqlEndpointHandler({ + schema() { + return store.getState().schema + }, + context() { + return withResolverContext({ + schema: store.getState().schema, + schemaComposer: store.getState().schemaCustomization.composer, context: {}, - customContext: schemaCustomization.context, - }), - customFormatErrorFn(err): GraphQLFormattedError { - return { - ...err.toJSON(), - extensions: { - stack: err.stack ? err.stack.split(`\n`) : [], - }, - } - }, - } + customContext: store.getState().schemaCustomization.context, + }) as unknown as OperationContext + }, + onOperation(_req, _args, result) { + if (result.errors) { + result.errors = result.errors.map( + err => + ({ + ...err.toJSON(), + extensions: { + stack: err.stack ? err.stack.split(`\n`) : [], + }, + } as unknown as GraphQLError) + ) + } + + result.extensions = { + enableRefresh: process.env.ENABLE_GATSBY_REFRESH_ENDPOINT, + refreshToken: process.env.GATSBY_REFRESH_TOKEN, + } + + return result + }, }) ) diff --git a/yarn.lock b/yarn.lock index 492c13de2bd38..29d2ae20cec50 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5193,7 +5193,7 @@ abortcontroller-polyfill@^1.1.9: resolved "https://registry.yarnpkg.com/abortcontroller-polyfill/-/abortcontroller-polyfill-1.7.3.tgz#1b5b487bd6436b5b764fd52a612509702c3144b5" integrity sha512-zetDJxd89y3X99Kvo4qFx8GKlt6GsvN3UcRZHwU6iFA/0KiOmhkTVhe8oRoTBiTVPZu09x3vCra47+w8Yz1+2Q== -accepts@^1.3.7, accepts@~1.3.4, accepts@~1.3.5, accepts@~1.3.8: +accepts@~1.3.4, accepts@~1.3.5, accepts@~1.3.8: version "1.3.8" resolved "https://registry.yarnpkg.com/accepts/-/accepts-1.3.8.tgz#0bf0be125b67014adcb0b0921e62db7bffe16b2e" integrity sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw== @@ -7765,7 +7765,7 @@ content-disposition@0.5.4: dependencies: safe-buffer "5.2.1" -content-type@^1.0.4, content-type@~1.0.4: +content-type@~1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/content-type/-/content-type-1.0.4.tgz#e138cc75e040c727b1966fe5e5f8c9aee256fe3b" @@ -9077,7 +9077,7 @@ depd@2.0.0, depd@~2.0.0: resolved "https://registry.yarnpkg.com/depd/-/depd-2.0.0.tgz#b696163cc757560d09cf22cc8fad1571b79e76df" integrity sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw== -depd@^1.1.2, depd@~1.1.2: +depd@^1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9" @@ -10474,16 +10474,6 @@ expect@^27.4.2: jest-message-util "^27.4.2" jest-regex-util "^27.4.0" -express-graphql@^0.12.0: - version "0.12.0" - resolved "https://registry.yarnpkg.com/express-graphql/-/express-graphql-0.12.0.tgz#58deabc309909ca2c9fe2f83f5fbe94429aa23df" - integrity sha512-DwYaJQy0amdy3pgNtiTDuGGM2BLdj+YO2SgbKoLliCfuHv3VVTt7vNG/ZqK2hRYjtYHE2t2KB705EU94mE64zg== - dependencies: - accepts "^1.3.7" - content-type "^1.0.4" - http-errors "1.8.0" - raw-body "^2.4.1" - express-http-proxy@^1.6.3: version "1.6.3" resolved "https://registry.yarnpkg.com/express-http-proxy/-/express-http-proxy-1.6.3.tgz#f3ef139ffd49a7962e7af0462bbcca557c913175" @@ -11929,6 +11919,11 @@ graphql-executor@0.0.23: resolved "https://registry.yarnpkg.com/graphql-executor/-/graphql-executor-0.0.23.tgz#205c1764b39ee0fcf611553865770f37b45851a2" integrity sha512-3Ivlyfjaw3BWmGtUSnMpP/a4dcXCp0mJtj0PiPG14OKUizaMKlSEX+LX2Qed0LrxwniIwvU6B4w/koVjEPyWJg== +graphql-http@^1.7.0: + version "1.7.0" + resolved "https://registry.yarnpkg.com/graphql-http/-/graphql-http-1.7.0.tgz#075af931b12245f32ce3cf3ef034714c7d5bc4f8" + integrity sha512-GNv1CVAFTblzf5Kenx0TBtEfk7wvQLVoxDv/AgNX1ahDI7aU9ysxzH/B2ar4qej604mMYArsKMz/ENX7Zm8ISQ== + graphql-language-service@5.0.6: version "5.0.6" resolved "https://registry.yarnpkg.com/graphql-language-service/-/graphql-language-service-5.0.6.tgz#7fd1e6479e5c3074b070c760fa961d9ad1ed7c72" @@ -12578,17 +12573,6 @@ http-cache-semantics@^4.0.0, http-cache-semantics@^4.1.0: resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-4.1.0.tgz#49e91c5cbf36c9b94bcfcd71c23d5249ec74e390" integrity sha512-carPklcUh7ROWRK7Cv27RPtdhYhUsela/ue5/jKzjegVvXDqM2ILE9Q2BGn9JZJh1g87cp56su/FgQSzcWS8cQ== -http-errors@1.8.0: - version "1.8.0" - resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.8.0.tgz#75d1bbe497e1044f51e4ee9e704a62f28d336507" - integrity sha512-4I8r0C5JDhT5VkvI47QktDW75rNlGVsUf/8hzjCC/wkWI/jdTRmBb9aI7erSG82r1bjKY3F6k28WnsVxB1C73A== - dependencies: - depd "~1.1.2" - inherits "2.0.4" - setprototypeof "1.2.0" - statuses ">= 1.5.0 < 2" - toidentifier "1.0.0" - http-errors@2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-2.0.0.tgz#b7774a1486ef73cf7667ac9ae0858c012c57b9d3" @@ -19866,7 +19850,7 @@ range-parser@^1.2.1, range-parser@~1.2.1: resolved "https://registry.yarnpkg.com/range-parser/-/range-parser-1.2.1.tgz#3cf37023d199e1c24d1a55b84800c2f3e6468031" integrity sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg== -raw-body@2.5.1, raw-body@^2.3.0, raw-body@^2.4.1: +raw-body@2.5.1, raw-body@^2.3.0: version "2.5.1" resolved "https://registry.yarnpkg.com/raw-body/-/raw-body-2.5.1.tgz#fe1b1628b181b700215e5fd42389f98b71392857" integrity sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig== @@ -22912,10 +22896,6 @@ statuses@2.0.1, statuses@^2.0.0: resolved "https://registry.yarnpkg.com/statuses/-/statuses-2.0.1.tgz#55cb000ccf1d48728bd23c685a063998cf1a1b63" integrity sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ== -"statuses@>= 1.5.0 < 2": - version "1.5.0" - resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c" - stealthy-require@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/stealthy-require/-/stealthy-require-1.1.1.tgz#35b09875b4ff49f26a777e509b3090a3226bf24b" @@ -23970,10 +23950,6 @@ toggle-selection@^1.0.6: resolved "https://registry.yarnpkg.com/toggle-selection/-/toggle-selection-1.0.6.tgz#6e45b1263f2017fa0acc7d89d78b15b8bf77da32" integrity sha1-bkWxJj8gF/oKzH2J14sVuL932jI= -toidentifier@1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.0.tgz#7e1be3470f1e77948bc43d94a3c8f4d7752ba553" - toidentifier@1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.1.tgz#3be34321a88a820ed1bd80dfaa33e479fbb8dd35"