From d513e031386c2617dee83f96a26a83ce294c8cd2 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Wed, 27 Jul 2022 16:02:26 +0200 Subject: [PATCH] feat: show connection upon verification --- packages/web/src/graphql/cache.ts | 24 +++++++++++++++---- .../graphql/mutations/create-connection.ts | 3 --- .../graphql/mutations/verify-connection.ts | 5 ++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/packages/web/src/graphql/cache.ts b/packages/web/src/graphql/cache.ts index 8bd32ef2..bca0bbb4 100644 --- a/packages/web/src/graphql/cache.ts +++ b/packages/web/src/graphql/cache.ts @@ -1,6 +1,10 @@ import { InMemoryCache } from '@apollo/client'; import offsetLimitPagination from './pagination'; +interface IRef { + __ref: string; +} + const cache = new InMemoryCache({ typePolicies: { App: { @@ -9,9 +13,9 @@ const cache = new InMemoryCache({ Mutation: { mutationType: true, fields: { - createConnection: { - merge(existing, newConnection, { readField, cache }) { - const appKey = readField('key', newConnection); + verifyConnection: { + merge(existing, verifiedConnection, { readField, cache }) { + const appKey = readField('key', verifiedConnection); const appCacheId = cache.identify({ __typename: 'App', key: appKey, @@ -21,12 +25,22 @@ const cache = new InMemoryCache({ id: appCacheId, fields: { connections: (existingConnections) => { - return [...existingConnections, newConnection]; + const existingConnectionIndex = existingConnections.findIndex((connection: IRef) => { + return connection.__ref === verifiedConnection.__ref; + }); + const connectionExists = existingConnectionIndex !== -1; + + // newly created and verified connection + if (!connectionExists) { + return [verifiedConnection, ...existingConnections]; + } + + return existingConnections; } } }); - return newConnection; + return verifiedConnection; } } } diff --git a/packages/web/src/graphql/mutations/create-connection.ts b/packages/web/src/graphql/mutations/create-connection.ts index 9e89b3f6..948cb8d5 100644 --- a/packages/web/src/graphql/mutations/create-connection.ts +++ b/packages/web/src/graphql/mutations/create-connection.ts @@ -11,9 +11,6 @@ export const CREATE_CONNECTION = gql` formattedData { screenName } - app { - key - } } } `; diff --git a/packages/web/src/graphql/mutations/verify-connection.ts b/packages/web/src/graphql/mutations/verify-connection.ts index 7e4a7dbc..0099b9eb 100644 --- a/packages/web/src/graphql/mutations/verify-connection.ts +++ b/packages/web/src/graphql/mutations/verify-connection.ts @@ -4,10 +4,15 @@ export const VERIFY_CONNECTION = gql` mutation VerifyConnection($input: VerifyConnectionInput) { verifyConnection(input: $input) { id + key verified formattedData { screenName } + createdAt + app { + key + } } } `;