From 4afa24ed1840eccad35c8e1ab9503367216347cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claus-Peter=20H=C3=BCbner?= Date: Fri, 30 Dec 2022 02:35:18 +0100 Subject: [PATCH] add graphql-client for requestGetPublicKey --- backend/package.json | 1 + .../federation/client/1_0/FederationClient.ts | 50 +++++++++++++++++++ .../graphql/1_0/model/FdCommunity.ts | 41 +++++++++++++++ backend/yarn.lock | 29 ++++++++++- 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 backend/src/federation/client/1_0/FederationClient.ts create mode 100644 backend/src/federation/graphql/1_0/model/FdCommunity.ts diff --git a/backend/package.json b/backend/package.json index 69a436563..33926f45b 100644 --- a/backend/package.json +++ b/backend/package.json @@ -30,6 +30,7 @@ "email-templates": "^10.0.1", "express": "^4.17.1", "graphql": "^15.5.1", + "graphql-request": "5.0.0", "i18n": "^0.15.1", "jest": "^27.2.4", "jsonwebtoken": "^8.5.1", diff --git a/backend/src/federation/client/1_0/FederationClient.ts b/backend/src/federation/client/1_0/FederationClient.ts new file mode 100644 index 000000000..12b5fdac0 --- /dev/null +++ b/backend/src/federation/client/1_0/FederationClient.ts @@ -0,0 +1,50 @@ +import { GraphQLClient, gql } from 'graphql-request' +import { backendLogger as logger } from '@/server/logger' +import { FdCommunity } from '@/federation/graphql/1_0/model/FdCommunity' + +export async function requestGetPublicKey(fdCom: FdCommunity): Promise { + let endpoint = fdCom.url.endsWith('/') ? fdCom.url : fdCom.url + '/' + endpoint = `${endpoint}graphql/${fdCom.apiVersion}/getPublicKey` + logger.info(`requestGetPublicKey with endpoint='${endpoint}'...`) + + const graphQLClient = new GraphQLClient(endpoint, { + method: 'GET', + jsonSerializer: { + parse: JSON.parse, + stringify: JSON.stringify, + }, + }) + logger.info(`graphQLClient=${JSON.stringify(graphQLClient)}`) + const query = gql` + query { + getPublicKey { + publicKey + } + } + ` + + const variables = {} + + try { + const { data, errors, extensions, headers, status } = await graphQLClient.rawRequest( + query, + variables, + ) + logger.debug( + `Response-Data: ${JSON.stringify( + { data, errors, extensions, headers, status }, + undefined, + 2, + )}`, + ) + if (data) { + logger.debug(`Response-PublicKey: ${data.getPublicKey.publicKey}`) + logger.info(`requestGetPublicKey processed successfully`) + return data.getPublicKey.publicKey + } + logger.warn(`requestGetPublicKey processed without response data`) + return undefined + } catch (err) { + logger.error(`Request-Error: ${JSON.stringify(err)}`) + } +} diff --git a/backend/src/federation/graphql/1_0/model/FdCommunity.ts b/backend/src/federation/graphql/1_0/model/FdCommunity.ts new file mode 100644 index 000000000..cf0ce3388 --- /dev/null +++ b/backend/src/federation/graphql/1_0/model/FdCommunity.ts @@ -0,0 +1,41 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ +import { Community as DbCommunity } from '@entity/Community' +import { ObjectType, Field } from 'type-graphql' + +@ObjectType() +export class FdCommunity { + constructor(dbCommunity: DbCommunity) { + this.apiVersion = dbCommunity.apiVersion + this.createdAt = dbCommunity.createdAt + this.id = dbCommunity.id + this.lastAnnouncedAt = dbCommunity.lastAnnouncedAt + this.publicKey = dbCommunity.publicKey.toString('hex') + this.updatedAt = dbCommunity.updatedAt ? dbCommunity.updatedAt : null + this.url = dbCommunity.endPoint + } + + @Field(() => Number, { nullable: true }) + id: number + + @Field(() => String) + publicKey: string + + @Field(() => String) + apiVersion: string + + @Field(() => String) + url: string + + @Field(() => Date) + createdAt: Date + + @Field(() => Date, { nullable: true }) + lastAnnouncedAt: Date | null + + @Field(() => Date, { nullable: true }) + verifiedAt: Date | null + + @Field(() => Date, { nullable: true }) + updatedAt: Date | null +} diff --git a/backend/yarn.lock b/backend/yarn.lock index 82bcd6b1f..72ee7518a 100644 --- a/backend/yarn.lock +++ b/backend/yarn.lock @@ -404,6 +404,11 @@ minimatch "^3.0.4" strip-json-comments "^3.1.1" +"@graphql-typed-document-node/core@^3.1.1": + version "3.1.1" + resolved "https://registry.yarnpkg.com/@graphql-typed-document-node/core/-/core-3.1.1.tgz#076d78ce99822258cf813ecc1e7fa460fa74d052" + integrity sha512-NQ17ii0rK1b34VZonlmT2QMJFI70m0TRwbknO/ihlbatXyaktDhN/98vBiUU6kNBPljqGqyIrl2T4nY2RpFANg== + "@hapi/boom@^10.0.0": version "10.0.0" resolved "https://registry.yarnpkg.com/@hapi/boom/-/boom-10.0.0.tgz#3624831d0a26b3378423b246f50eacea16e04a08" @@ -2193,6 +2198,13 @@ cross-env@^7.0.3: dependencies: cross-spawn "^7.0.1" +cross-fetch@^3.1.5: + version "3.1.5" + resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.1.5.tgz#e1389f44d9e7ba767907f7af8454787952ab534f" + integrity sha512-lvb1SBsI0Z7GDwmuid+mU3kWVBwTVUbe7S0H52yaaAdQOXq2YktTCZdlAcNKFzE6QtRz0snpw9bNiPeOIkkQvw== + dependencies: + node-fetch "2.6.7" + cross-spawn@^6.0.0: version "6.0.5" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4" @@ -2985,6 +2997,11 @@ express@^4.17.1: utils-merge "1.0.1" vary "~1.1.2" +extract-files@^9.0.0: + version "9.0.0" + resolved "https://registry.yarnpkg.com/extract-files/-/extract-files-9.0.0.tgz#8a7744f2437f81f5ed3250ed9f1550de902fe54a" + integrity sha512-CvdFfHkC95B4bBBk36hcEmvdR2awOdhhVUYH6S/zrVj3477zven/fJMYg7121h4T1xHZC+tetUpubpAhxwI7hQ== + faker@^5.5.3: version "5.5.3" resolved "https://registry.yarnpkg.com/faker/-/faker-5.5.3.tgz#c57974ee484431b25205c2c8dc09fda861e51e0e" @@ -3340,6 +3357,16 @@ graphql-query-complexity@^0.7.0: dependencies: lodash.get "^4.4.2" +graphql-request@5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/graphql-request/-/graphql-request-5.0.0.tgz#7504a807d0e11be11a3c448e900f0cc316aa18ef" + integrity sha512-SpVEnIo2J5k2+Zf76cUkdvIRaq5FMZvGQYnA4lUWYbc99m+fHh4CZYRRO/Ff4tCLQ613fzCm3SiDT64ubW5Gyw== + dependencies: + "@graphql-typed-document-node/core" "^3.1.1" + cross-fetch "^3.1.5" + extract-files "^9.0.0" + form-data "^3.0.0" + graphql-subscriptions@^1.0.0, graphql-subscriptions@^1.1.0: version "1.2.1" resolved "https://registry.yarnpkg.com/graphql-subscriptions/-/graphql-subscriptions-1.2.1.tgz#2142b2d729661ddf967b7388f7cf1dd4cf2e061d" @@ -4977,7 +5004,7 @@ nice-try@^1.0.4: resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366" integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ== -node-fetch@^2.6.0: +node-fetch@2.6.7, node-fetch@^2.6.0: version "2.6.7" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad" integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==