From cdd56ba30ca763b0fd496cf3feb71e87d46d143f Mon Sep 17 00:00:00 2001 From: Tim Date: Mon, 20 May 2024 07:54:36 -0700 Subject: [PATCH] =?UTF-8?q?build:=20replace=20express-graphql=20with=20gra?= =?UTF-8?q?phql-http=20and=20graphql-playgrou=E2=80=A6=20(#1521)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * build: replace express-graphql with graphql-http and graphql-playground-middleware-express toward resolution of issue #1420 and #1508 * fix: go ahead and preempt provision of GET at /graphql by graphql-http with graphql-playground-middleware-express, rather than serving it from GET /gql-playground. This preserves functionality closer to as it was with express-graphql. * fix: whoops did not mean to modify database.json * Update backend/app.ts Co-authored-by: Cacie Prins * style: do not pollute type system with one-off inline types * fix: consume rename commited on github from reviewer suggestion * style: do not pollute Query.ts either with one-off inline types * build: empty commit to clear perhaps spurious circleCI failure on firefox all specs passing on firefox locally. --------- Co-authored-by: Cacie Prins --- backend/app.ts | 11 ++-- backend/gql-playground-routes.ts | 6 ++ backend/graphql/resolvers/Mutation.ts | 3 +- backend/graphql/resolvers/Query.ts | 4 +- cypress/tests/api/api-bankaccounts.spec.ts | 1 + package.json | 3 +- yarn.lock | 76 ++++++++++------------ 7 files changed, 54 insertions(+), 50 deletions(-) create mode 100644 backend/gql-playground-routes.ts diff --git a/backend/app.ts b/backend/app.ts index 8fa620f27..06cf8d68c 100644 --- a/backend/app.ts +++ b/backend/app.ts @@ -6,7 +6,7 @@ import session from "express-session"; import bodyParser from "body-parser"; import cors from "cors"; import paginate from "express-paginate"; -import { graphqlHTTP } from "express-graphql"; +import { createHandler as graphqlHandler } from "graphql-http/lib/use/express"; import { loadSchemaSync } from "@graphql-tools/load"; import { GraphQLFileLoader } from "@graphql-tools/graphql-file-loader"; import { addResolversToSchema } from "@graphql-tools/schema"; @@ -15,6 +15,7 @@ import auth from "./auth"; import userRoutes from "./user-routes"; import contactRoutes from "./contact-routes"; import bankAccountRoutes from "./bankaccount-routes"; +import gqlPlaygroundRoutes from "./gql-playground-routes"; import transactionRoutes from "./transaction-routes"; import likeRoutes from "./like-routes"; import commentRoutes from "./comment-routes"; @@ -97,14 +98,16 @@ if (process.env.VITE_GOOGLE) { app.use(checkGoogleJwt); } +app.use("/graphql", gqlPlaygroundRoutes); app.use( "/graphql", - graphqlHTTP({ + graphqlHandler({ schema: schemaWithResolvers, - graphiql: true, + context: async (req, _args) => { + return { user: req.raw.user }; + }, }) ); - app.use("/users", userRoutes); app.use("/contacts", contactRoutes); app.use("/bankAccounts", bankAccountRoutes); diff --git a/backend/gql-playground-routes.ts b/backend/gql-playground-routes.ts new file mode 100644 index 000000000..ca039a711 --- /dev/null +++ b/backend/gql-playground-routes.ts @@ -0,0 +1,6 @@ +import express from "express"; +import expressPlayground from "graphql-playground-middleware-express"; + +const router = express.Router(); +router.get("/", expressPlayground({ endpoint: "/graphql" })); +export default router; diff --git a/backend/graphql/resolvers/Mutation.ts b/backend/graphql/resolvers/Mutation.ts index 5cc8d96a8..64e4d9798 100644 --- a/backend/graphql/resolvers/Mutation.ts +++ b/backend/graphql/resolvers/Mutation.ts @@ -2,8 +2,7 @@ import { createBankAccountForUser, removeBankAccountById } from "../../database" const Mutation = { createBankAccount: (obj: any, args: any, ctx: any) => { - const account = createBankAccountForUser(ctx.user.id!, args); - return account; + return createBankAccountForUser(ctx.user.id, args); }, deleteBankAccount: (obj: any, args: any, ctx: any) => { removeBankAccountById(args.id); diff --git a/backend/graphql/resolvers/Query.ts b/backend/graphql/resolvers/Query.ts index 51e517275..ec8db8d19 100644 --- a/backend/graphql/resolvers/Query.ts +++ b/backend/graphql/resolvers/Query.ts @@ -4,9 +4,7 @@ const Query = { listBankAccount(obj: any, args: any, ctx: any) { /* istanbul ignore next */ try { - const accounts = getBankAccountsByUserId(ctx.user.id!); - - return accounts; + return getBankAccountsByUserId(ctx.user.id); /* istanbul ignore next */ } catch (err: any) { /* istanbul ignore next */ diff --git a/cypress/tests/api/api-bankaccounts.spec.ts b/cypress/tests/api/api-bankaccounts.spec.ts index 032d5e71f..5ef19ff53 100644 --- a/cypress/tests/api/api-bankaccounts.spec.ts +++ b/cypress/tests/api/api-bankaccounts.spec.ts @@ -101,6 +101,7 @@ describe("Bank Accounts API", function () { }`, }).then((response) => { expect(response.status).to.eq(200); + expect(JSON.stringify(response.body.errors || "notThere")).to.eq('"notThere"'); expect(response.body.data.listBankAccount[0].userId).to.eq(userId); }); }); diff --git a/package.json b/package.json index ddf0c03c4..77971585f 100644 --- a/package.json +++ b/package.json @@ -103,13 +103,14 @@ "eslint-plugin-cypress": "2.15.1", "eslint-plugin-prettier": "^5.0.0", "express": "4.19.2", - "express-graphql": "0.12.0", "express-jwt": "6.1.2", "express-paginate": "1.0.2", "express-session": "1.18.0", "express-validator": "6.15.0", "fuse.js": "6.5.3", "graphql": "16.8.1", + "graphql-http": "^1.22.0", + "graphql-playground-middleware-express": "^1.7.23", "graphql-tools": "8.2.7", "http-proxy-middleware": "0.19.1", "husky": "7.0.4", diff --git a/yarn.lock b/yarn.lock index d6a42b5a9..89fe2f697 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4446,7 +4446,7 @@ abort-controller@^3.0.0: dependencies: event-target-shim "^5.0.0" -accepts@^1.3.7, accepts@~1.3.8: +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== @@ -5529,7 +5529,7 @@ combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6: dependencies: delayed-stream "~1.0.0" -commander@^2.20.0: +commander@^2.20.0, commander@^2.20.3: version "2.20.3" resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33" integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ== @@ -5605,7 +5605,7 @@ content-disposition@0.5.4, 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.5: +content-type@~1.0.4, content-type@~1.0.5: version "1.0.5" resolved "https://registry.yarnpkg.com/content-type/-/content-type-1.0.5.tgz#8b773162656d1d1086784c8f23a54ce6d73d7918" integrity sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA== @@ -5753,6 +5753,11 @@ cssesc@^3.0.0: resolved "https://registry.yarnpkg.com/cssesc/-/cssesc-3.0.0.tgz#37741919903b868565e1c09ea747445cd18983ee" integrity sha512-/Tb/JcjK111nNScGob5MNtsntNM1aCNUDipB/TkwZFhyDrrE47SOx/18wF2bbjgc3ZzCSKW1T5nt5EbFoAz/Vg== +cssfilter@0.0.10: + version "0.0.10" + resolved "https://registry.yarnpkg.com/cssfilter/-/cssfilter-0.0.10.tgz#c6d2672632a2e5c83e013e6864a42ce8defd20ae" + integrity sha512-FAaLDaplstoRsDR8XGYH51znUN0UY7nMc6Z9/fvE8EXGwvJE9hu7W2vHwx1+bd6gCYnln9nLbzxFTrcO9YQDZw== + cssstyle@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/cssstyle/-/cssstyle-3.0.0.tgz#17ca9c87d26eac764bb8cfd00583cff21ce0277a" @@ -6020,11 +6025,6 @@ depd@2.0.0, 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: - version "1.1.2" - resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9" - integrity sha512-7emPTl6Dpo6JRXOXjLRxck+FlLRX5847cLKEn00PLAgc3g2hTZZgr+e4c2v6QpSmLeFP3n5yUo7ft6avBK/5jQ== - dequal@^2.0.3: version "2.0.3" resolved "https://registry.yarnpkg.com/dequal/-/dequal-2.0.3.tgz#2644214f1997d39ed0ee0ece72335490a7ac67be" @@ -6793,16 +6793,6 @@ exponential-backoff@^3.1.1: resolved "https://registry.yarnpkg.com/exponential-backoff/-/exponential-backoff-3.1.1.tgz#64ac7526fe341ab18a39016cd22c787d01e00bf6" integrity sha512-dX7e/LHVJ6W3DE1MHWi9S1EYzDESENfLrYohG2G++ovZrYOkm4Knwa0mc1cn84xJOR4KEU0WSchhLbd0UklbHw== -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-jwt@6.1.2: version "6.1.2" resolved "https://registry.yarnpkg.com/express-jwt/-/express-jwt-6.1.2.tgz#4a6cc11d1dcff6f23126dd79ec5b2b441333e78b" @@ -7526,6 +7516,25 @@ graphemer@^1.4.0: resolved "https://registry.yarnpkg.com/graphemer/-/graphemer-1.4.0.tgz#fb2f1d55e0e3a1849aeffc90c4fa0dd53a0e66c6" integrity sha512-EtKwoO6kxCL9WO5xipiHTZlSzBm7WLT627TqC/uVRd0HKmq8NXyebnNYxDoBi7wt8eTWrUrKXCOVaFq9x1kgag== +graphql-http@^1.22.0: + version "1.22.0" + resolved "https://registry.yarnpkg.com/graphql-http/-/graphql-http-1.22.0.tgz#967bad279747ba5e1c9dd85b644c6b4f3dfa88f2" + integrity sha512-9RBUlGJWBFqz9LwfpmAbjJL/8j/HCNkZwPBU5+Bfmwez+1Ay43DocMNQYpIWsWqH0Ftv6PTNAh2aRnnMCBJgLw== + +graphql-playground-html@^1.6.30: + version "1.6.30" + resolved "https://registry.yarnpkg.com/graphql-playground-html/-/graphql-playground-html-1.6.30.tgz#14c2a8eb7fc17bfeb1a746bbb28a11e34bf0b391" + integrity sha512-tpCujhsJMva4aqE8ULnF7/l3xw4sNRZcSHu+R00VV+W0mfp+Q20Plvcrp+5UXD+2yS6oyCXncA+zoQJQqhGCEw== + dependencies: + xss "^1.0.6" + +graphql-playground-middleware-express@^1.7.23: + version "1.7.23" + resolved "https://registry.yarnpkg.com/graphql-playground-middleware-express/-/graphql-playground-middleware-express-1.7.23.tgz#95aba44d801ff3c08b2246917d2901d2e7c35d3d" + integrity sha512-M/zbTyC1rkgiQjFSgmzAv6umMHOphYLNWZp6Ye5QrD77WfGOOoSqDsVmGUczc2pDkEPEzzGB/bvBO5rdzaTRgw== + dependencies: + graphql-playground-html "^1.6.30" + graphql-tag@^2.12.3: version "2.12.6" resolved "https://registry.yarnpkg.com/graphql-tag/-/graphql-tag-2.12.6.tgz#d441a569c1d2537ef10ca3d1633b48725329b5f1" @@ -7701,17 +7710,6 @@ http-cache-semantics@^4.1.1: resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-4.1.1.tgz#abe02fcb2985460bf0323be664436ec3476a6d5a" integrity sha512-er295DKPVsV82j5kw1Gjt+ADA/XYHsajl82cGNQG2eyoPkvgUhX+nDIyelzhIWbbsXP39EHcI6l5tYs2FYqYXQ== -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" @@ -10647,7 +10645,7 @@ 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.2, raw-body@^2.4.1: +raw-body@2.5.2: version "2.5.2" resolved "https://registry.yarnpkg.com/raw-body/-/raw-body-2.5.2.tgz#99febd83b90e08975087e8f1f9419a149366b68a" integrity sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA== @@ -11556,11 +11554,6 @@ statuses@2.0.1: 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" - integrity sha512-OpZ3zP+jT1PI7I8nemJX4AKmAX070ZkYPVWV/AaKTJl+tXCTGyVdC1a4SL8RUQYEwk/f34ZX8UTykN68FwrqAA== - std-env@^3.3.3: version "3.3.3" resolved "https://registry.yarnpkg.com/std-env/-/std-env-3.3.3.tgz#a54f06eb245fdcfef53d56f3c0251f1d5c3d01fe" @@ -11911,11 +11904,6 @@ to-regex@^3.0.1, to-regex@^3.0.2: regex-not "^1.0.2" safe-regex "^1.1.0" -toidentifier@1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.0.tgz#7e1be3470f1e77948bc43d94a3c8f4d7752ba553" - integrity sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw== - toidentifier@1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.1.tgz#3be34321a88a820ed1bd80dfaa33e479fbb8dd35" @@ -12726,6 +12714,14 @@ xmlchars@^2.2.0: resolved "https://registry.yarnpkg.com/xmlchars/-/xmlchars-2.2.0.tgz#060fe1bcb7f9c76fe2a17db86a9bc3ab894210cb" integrity sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw== +xss@^1.0.6: + version "1.0.15" + resolved "https://registry.yarnpkg.com/xss/-/xss-1.0.15.tgz#96a0e13886f0661063028b410ed1b18670f4e59a" + integrity sha512-FVdlVVC67WOIPvfOwhoMETV72f6GbW7aOabBC3WxN/oUdoEMDyLz4OgRv5/gck2ZeNqEQu+Tb0kloovXOfpYVg== + dependencies: + commander "^2.20.3" + cssfilter "0.0.10" + xstate@4.38.3: version "4.38.3" resolved "https://registry.yarnpkg.com/xstate/-/xstate-4.38.3.tgz#4e15e7ad3aa0ca1eea2010548a5379966d8f1075"