From 44fbaeee82f4f2dba4ec9e0c57ee775192bf6f08 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Fri, 22 Oct 2021 15:55:25 +0200 Subject: [PATCH] feat: Introduce reset and verify connection mutations --- packages/backend/src/apps/flickr/info.json | 39 +++++++++++++++++-- packages/backend/src/apps/twitter/info.json | 39 +++++++++++++++++-- .../src/graphql/mutations/reset-connection.ts | 31 +++++++++++++++ .../graphql/mutations/update-connection.ts | 12 ------ .../graphql/mutations/verify-connection.ts | 35 +++++++++++++++++ packages/backend/src/graphql/root-mutation.ts | 4 ++ packages/web/src/graphql/mutations/index.ts | 4 ++ .../src/graphql/mutations/reset-connection.ts | 9 +++++ .../graphql/mutations/verify-connection.ts | 13 +++++++ 9 files changed, 168 insertions(+), 18 deletions(-) create mode 100644 packages/backend/src/graphql/mutations/reset-connection.ts create mode 100644 packages/backend/src/graphql/mutations/verify-connection.ts create mode 100644 packages/web/src/graphql/mutations/reset-connection.ts create mode 100644 packages/web/src/graphql/mutations/verify-connection.ts diff --git a/packages/backend/src/apps/flickr/info.json b/packages/backend/src/apps/flickr/info.json index 20e1eca9..3dcac7e7 100644 --- a/packages/backend/src/apps/flickr/info.json +++ b/packages/backend/src/apps/flickr/info.json @@ -110,12 +110,34 @@ ] } ] + }, + { + "step": 5, + "type": "mutation", + "name": "verifyConnection", + "fields": [ + { + "name": "id", + "value": "{createConnection.id}" + } + ] } ], "reconnectionSteps": [ { "step": 1, "type": "mutation", + "name": "resetConnection", + "fields": [ + { + "name": "id", + "value": "{connection.id}" + } + ] + }, + { + "step": 2, + "type": "mutation", "name": "updateConnection", "fields": [ { @@ -139,7 +161,7 @@ ] }, { - "step": 2, + "step": 3, "type": "mutation", "name": "createAuthLink", "fields": [ @@ -150,7 +172,7 @@ ] }, { - "step": 3, + "step": 4, "type": "openWithPopup", "name": "openAuthPopup", "fields": [ @@ -161,7 +183,7 @@ ] }, { - "step": 4, + "step": 5, "type": "mutation", "name": "updateConnection", "fields": [ @@ -180,6 +202,17 @@ ] } ] + }, + { + "step": 6, + "type": "mutation", + "name": "verifyConnection", + "fields": [ + { + "name": "id", + "value": "{connection.id}" + } + ] } ] } diff --git a/packages/backend/src/apps/twitter/info.json b/packages/backend/src/apps/twitter/info.json index 071d87a6..2ce73457 100644 --- a/packages/backend/src/apps/twitter/info.json +++ b/packages/backend/src/apps/twitter/info.json @@ -110,12 +110,34 @@ ] } ] + }, + { + "step": 5, + "type": "mutation", + "name": "verifyConnection", + "fields": [ + { + "name": "id", + "value": "{createConnection.id}" + } + ] } ], "reconnectionSteps": [ { "step": 1, "type": "mutation", + "name": "resetConnection", + "fields": [ + { + "name": "id", + "value": "{connection.id}" + } + ] + }, + { + "step": 2, + "type": "mutation", "name": "updateConnection", "fields": [ { @@ -139,7 +161,7 @@ ] }, { - "step": 2, + "step": 3, "type": "mutation", "name": "createAuthLink", "fields": [ @@ -150,7 +172,7 @@ ] }, { - "step": 3, + "step": 4, "type": "openWithPopup", "name": "openTwitterAuthPopup", "fields": [ @@ -161,7 +183,7 @@ ] }, { - "step": 4, + "step": 5, "type": "mutation", "name": "updateConnection", "fields": [ @@ -180,6 +202,17 @@ ] } ] + }, + { + "step": 6, + "type": "mutation", + "name": "verifyConnection", + "fields": [ + { + "name": "id", + "value": "{connection.id}" + } + ] } ] } diff --git a/packages/backend/src/graphql/mutations/reset-connection.ts b/packages/backend/src/graphql/mutations/reset-connection.ts new file mode 100644 index 00000000..06579b66 --- /dev/null +++ b/packages/backend/src/graphql/mutations/reset-connection.ts @@ -0,0 +1,31 @@ +import { GraphQLString, GraphQLNonNull } from 'graphql'; +import Connection from '../../models/connection'; +import connectionType from '../types/connection'; +import RequestWithCurrentUser from '../../types/express/request-with-current-user'; + +type Params = { + id: string +} + +const resetConnectionResolver = async (params: Params, req: RequestWithCurrentUser) => { + let connection = await Connection.query().findOne({ + user_id: req.currentUser.id, + id: params.id + }) + + connection = await connection.$query().patchAndFetch({ + data: { screenName: connection.data.screenName } + }) + + return connection; +} + +const resetConnection = { + type: connectionType, + args: { + id: { type: GraphQLNonNull(GraphQLString) }, + }, + resolve: (_: any, params: Params, req: RequestWithCurrentUser) => resetConnectionResolver(params, req) +}; + +export default resetConnection; diff --git a/packages/backend/src/graphql/mutations/update-connection.ts b/packages/backend/src/graphql/mutations/update-connection.ts index 00a6f44d..2b0b7521 100644 --- a/packages/backend/src/graphql/mutations/update-connection.ts +++ b/packages/backend/src/graphql/mutations/update-connection.ts @@ -21,18 +21,6 @@ const updateConnectionResolver = async (params: Params, req: RequestWithCurrentU } }) - // Not every updateConnection mutation can verify credentials as some need to reconnect - try { - const appClass = (await import(`../../apps/${connection.key}`)).default; - - const appInstance = new appClass(connection.data) - const verifiedCredentials = await appInstance.verifyCredentials(); - - connection = await connection.$query().patchAndFetch({ - data: verifiedCredentials - }) - } catch {} - return connection; } diff --git a/packages/backend/src/graphql/mutations/verify-connection.ts b/packages/backend/src/graphql/mutations/verify-connection.ts new file mode 100644 index 00000000..7305f634 --- /dev/null +++ b/packages/backend/src/graphql/mutations/verify-connection.ts @@ -0,0 +1,35 @@ +import { GraphQLString, GraphQLNonNull } from 'graphql'; +import Connection from '../../models/connection'; +import connectionType from '../types/connection'; +import RequestWithCurrentUser from '../../types/express/request-with-current-user'; + +type Params = { + id: string +} +const verifyConnectionResolver = async (params: Params, req: RequestWithCurrentUser) => { + let connection = await Connection.query().findOne({ + user_id: req.currentUser.id, + id: params.id + }) + + const appClass = (await import(`../../apps/${connection.key}`)).default; + + const appInstance = new appClass(connection.data) + const verifiedCredentials = await appInstance.verifyCredentials(); + + connection = await connection.$query().patchAndFetch({ + data: verifiedCredentials + }) + + return connection; +} + +const verifyConnection = { + type: connectionType, + args: { + id: { type: GraphQLNonNull(GraphQLString) } + }, + resolve: (_: any, params: Params, req: RequestWithCurrentUser) => verifyConnectionResolver(params, req) +}; + +export default verifyConnection; diff --git a/packages/backend/src/graphql/root-mutation.ts b/packages/backend/src/graphql/root-mutation.ts index 159727df..68d0c5b0 100644 --- a/packages/backend/src/graphql/root-mutation.ts +++ b/packages/backend/src/graphql/root-mutation.ts @@ -2,6 +2,8 @@ import { GraphQLObjectType } from 'graphql'; import createConnection from './mutations/create-connection'; import createAuthLink from './mutations/create-auth-link'; import updateConnection from './mutations/update-connection'; +import resetConnection from './mutations/reset-connection'; +import verifyConnection from './mutations/verify-connection'; import deleteConnection from './mutations/delete-connection'; const rootMutation = new GraphQLObjectType({ @@ -10,6 +12,8 @@ const rootMutation = new GraphQLObjectType({ createConnection, createAuthLink, updateConnection, + resetConnection, + verifyConnection, deleteConnection } }); diff --git a/packages/web/src/graphql/mutations/index.ts b/packages/web/src/graphql/mutations/index.ts index d2d3a47b..b1323191 100644 --- a/packages/web/src/graphql/mutations/index.ts +++ b/packages/web/src/graphql/mutations/index.ts @@ -1,5 +1,7 @@ import { CREATE_CONNECTION } from './create-connection'; import { UPDATE_CONNECTION } from './update-connection'; +import { VERIFY_CONNECTION } from './verify-connection'; +import { RESET_CONNECTION } from './reset-connection'; import { DELETE_CONNECTION } from './delete-connection'; import { CREATE_AUTH_LINK } from './create-auth-link'; @@ -10,6 +12,8 @@ type Mutations = { const mutations: Mutations = { createConnection: CREATE_CONNECTION, updateConnection: UPDATE_CONNECTION, + verifyConnection: VERIFY_CONNECTION, + resetConnection: RESET_CONNECTION, deleteConnection: DELETE_CONNECTION, createAuthLink: CREATE_AUTH_LINK, }; diff --git a/packages/web/src/graphql/mutations/reset-connection.ts b/packages/web/src/graphql/mutations/reset-connection.ts new file mode 100644 index 00000000..7b4f6686 --- /dev/null +++ b/packages/web/src/graphql/mutations/reset-connection.ts @@ -0,0 +1,9 @@ +import { gql } from '@apollo/client'; + +export const RESET_CONNECTION = gql` + mutation ResetConnection($id: String!) { + resetConnection(id: $id) { + id + } + } +`; diff --git a/packages/web/src/graphql/mutations/verify-connection.ts b/packages/web/src/graphql/mutations/verify-connection.ts new file mode 100644 index 00000000..7233798b --- /dev/null +++ b/packages/web/src/graphql/mutations/verify-connection.ts @@ -0,0 +1,13 @@ +import { gql } from '@apollo/client'; + +export const VERIFY_CONNECTION = gql` + mutation VerifyConnection($id: String!) { + verifyConnection(id: $id) { + id + verified + data { + screenName + } + } + } +`;