feat: Introduce reset and verify connection mutations

This commit is contained in:
Faruk AYDIN
2021-10-22 15:55:25 +02:00
committed by Ali BARIN
parent de82dbfe29
commit 44fbaeee82
9 changed files with 168 additions and 18 deletions

View File

@@ -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}"
}
]
}
]
}

View File

@@ -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}"
}
]
}
]
}

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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
}
});

View File

@@ -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,
};

View File

@@ -0,0 +1,9 @@
import { gql } from '@apollo/client';
export const RESET_CONNECTION = gql`
mutation ResetConnection($id: String!) {
resetConnection(id: $id) {
id
}
}
`;

View File

@@ -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
}
}
}
`;