feat: schema version header check (#4563)

closes https://github.com/twentyhq/twenty/issues/4479

tried to catch the error inside various places including
https://github.com/twentyhq/twenty/blob/main/packages/twenty-server/src/engine/integrations/exception-handler/exception-handler.service.ts
but it seems like the error never reaches the GraphQL module 😮

any idea where we could intercept such an error `Cannot query field`?

---------

Co-authored-by: Jérémy Magrin <jeremy.magrin@gmail.com>
This commit is contained in:
rostaklein
2024-04-04 09:52:45 +02:00
committed by GitHub
parent eab65f34f9
commit 306ef1df9c
20 changed files with 182 additions and 87 deletions

View File

@ -39,6 +39,23 @@ export type ApiKeyToken = {
token: Scalars['String']['output'];
};
export type AppToken = {
__typename?: 'AppToken';
createdAt: Scalars['DateTime']['output'];
expiresAt: Scalars['DateTime']['output'];
id: Scalars['ID']['output'];
type: Scalars['String']['output'];
updatedAt: Scalars['DateTime']['output'];
};
export type AppTokenEdge = {
__typename?: 'AppTokenEdge';
/** Cursor for this node. */
cursor: Scalars['ConnectionCursor']['output'];
/** The node containing the AppToken */
node: AppToken;
};
export type AuthProviders = {
__typename?: 'AuthProviders';
google: Scalars['Boolean']['output'];
@ -63,6 +80,11 @@ export type AuthTokens = {
tokens: AuthTokenPair;
};
export type AuthorizeApp = {
__typename?: 'AuthorizeApp';
redirectUrl: Scalars['String']['output'];
};
export type Billing = {
__typename?: 'Billing';
billingFreeTrialDurationInDays?: Maybe<Scalars['Float']['output']>;
@ -110,6 +132,10 @@ export type ClientConfig = {
telemetry: Telemetry;
};
export type CreateAppTokenInput = {
expiresAt: Scalars['DateTime']['input'];
};
export type CreateFieldInput = {
defaultValue?: InputMaybe<Scalars['JSON']['input']>;
description?: InputMaybe<Scalars['String']['input']>;
@ -138,6 +164,11 @@ export type CreateObjectInput = {
nameSingular: Scalars['String']['input'];
};
export type CreateOneAppTokenInput = {
/** The record to create */
appToken: CreateAppTokenInput;
};
export type CreateOneFieldMetadataInput = {
/** The record to create */
field: CreateFieldInput;
@ -148,20 +179,11 @@ export type CreateOneObjectInput = {
object: CreateObjectInput;
};
export type CreateOneRefreshTokenInput = {
/** The record to create */
refreshToken: CreateRefreshTokenInput;
};
export type CreateOneRelationInput = {
/** The record to create */
relation: CreateRelationInput;
};
export type CreateRefreshTokenInput = {
expiresAt: Scalars['DateTime']['input'];
};
export type CreateRelationInput = {
description?: InputMaybe<Scalars['String']['input']>;
fromDescription?: InputMaybe<Scalars['String']['input']>;
@ -215,6 +237,13 @@ export type EmailPasswordResetLink = {
success: Scalars['Boolean']['output'];
};
export type ExchangeAuthCode = {
__typename?: 'ExchangeAuthCode';
accessToken: AuthToken;
loginToken: AuthToken;
refreshToken: AuthToken;
};
export type FeatureFlag = {
__typename?: 'FeatureFlag';
id: Scalars['ID']['output'];
@ -338,11 +367,12 @@ export type LoginToken = {
export type Mutation = {
__typename?: 'Mutation';
activateWorkspace: Workspace;
authorizeApp: AuthorizeApp;
challenge: LoginToken;
checkoutSession: SessionEntity;
createOneAppToken: AppToken;
createOneField: Field;
createOneObject: Object;
createOneRefreshToken: RefreshToken;
createOneRelation: Relation;
createOneRemoteServer: RemoteServer;
deleteCurrentWorkspace: Workspace;
@ -378,6 +408,12 @@ export type MutationActivateWorkspaceArgs = {
};
export type MutationAuthorizeAppArgs = {
clientId: Scalars['String']['input'];
codeChallenge: Scalars['String']['input'];
};
export type MutationChallengeArgs = {
email: Scalars['String']['input'];
password: Scalars['String']['input'];
@ -390,6 +426,11 @@ export type MutationCheckoutSessionArgs = {
};
export type MutationCreateOneAppTokenArgs = {
input: CreateOneAppTokenInput;
};
export type MutationCreateOneFieldArgs = {
input: CreateOneFieldMetadataInput;
};
@ -400,11 +441,6 @@ export type MutationCreateOneObjectArgs = {
};
export type MutationCreateOneRefreshTokenArgs = {
input: CreateOneRefreshTokenInput;
};
export type MutationCreateOneRelationArgs = {
input: CreateOneRelationInput;
};
@ -457,7 +493,7 @@ export type MutationImpersonateArgs = {
export type MutationRenewTokenArgs = {
refreshToken: Scalars['String']['input'];
appToken: Scalars['String']['input'];
};
@ -576,6 +612,7 @@ export type Query = {
clientConfig: ClientConfig;
currentUser: User;
currentWorkspace: Workspace;
exchangeAuthorizationCode: ExchangeAuthCode;
field: Field;
fields: FieldConnection;
findAvailableRemoteTablesByServerId: Array<RemoteTable>;
@ -610,6 +647,12 @@ export type QueryCheckWorkspaceInviteHashIsValidArgs = {
};
export type QueryExchangeAuthorizationCodeArgs = {
authorizationCode: Scalars['String']['input'];
codeVerifier: Scalars['String']['input'];
};
export type QueryFieldArgs = {
id: Scalars['ID']['input'];
};
@ -699,22 +742,6 @@ export type QueryValidatePasswordResetTokenArgs = {
passwordResetToken: Scalars['String']['input'];
};
export type RefreshToken = {
__typename?: 'RefreshToken';
createdAt: Scalars['DateTime']['output'];
expiresAt: Scalars['DateTime']['output'];
id: Scalars['ID']['output'];
updatedAt: Scalars['DateTime']['output'];
};
export type RefreshTokenEdge = {
__typename?: 'RefreshTokenEdge';
/** Cursor for this node. */
cursor: Scalars['ConnectionCursor']['output'];
/** The node containing the RefreshToken */
node: RefreshToken;
};
export type RelationConnection = {
__typename?: 'RelationConnection';
/** Array of edges. */
@ -1027,6 +1054,7 @@ export type Workspace = {
billingSubscriptions?: Maybe<Array<BillingSubscription>>;
createdAt: Scalars['DateTime']['output'];
currentBillingSubscription?: Maybe<BillingSubscription>;
currentCacheVersion?: Maybe<Scalars['String']['output']>;
deletedAt?: Maybe<Scalars['DateTime']['output']>;
displayName?: Maybe<Scalars['String']['output']>;
domainName?: Maybe<Scalars['String']['output']>;

View File

@ -831,6 +831,7 @@ export type Workspace = {
billingSubscriptions?: Maybe<Array<BillingSubscription>>;
createdAt: Scalars['DateTime'];
currentBillingSubscription?: Maybe<BillingSubscription>;
currentCacheVersion?: Maybe<Scalars['String']>;
deletedAt?: Maybe<Scalars['DateTime']>;
displayName?: Maybe<Scalars['String']>;
domainName?: Maybe<Scalars['String']>;
@ -2393,6 +2394,7 @@ export const GetCurrentUserDocument = gql`
value
workspaceId
}
currentCacheVersion
currentBillingSubscription {
status
interval

View File

@ -1,8 +1,9 @@
import { useMemo, useRef } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { InMemoryCache, NormalizedCacheObject } from '@apollo/client';
import { useRecoilState } from 'recoil';
import { useRecoilState, useRecoilValue } from 'recoil';
import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState';
import { previousUrlState } from '@/auth/states/previousUrlState';
import { tokenPairState } from '@/auth/states/tokenPairState';
import { isDebugModeState } from '@/client-config/states/isDebugModeState';
@ -17,6 +18,7 @@ import { ApolloFactory, Options } from '../services/apollo.factory';
export const useApolloFactory = (options: Partial<Options<any>> = {}) => {
// eslint-disable-next-line @nx/workspace-no-state-useref
const apolloRef = useRef<ApolloFactory<NormalizedCacheObject> | null>(null);
const currentWorkspace = useRecoilValue(currentWorkspaceState);
const [isDebugMode] = useRecoilState(isDebugModeState);
const navigate = useNavigate();
@ -29,6 +31,11 @@ export const useApolloFactory = (options: Partial<Options<any>> = {}) => {
apolloRef.current = new ApolloFactory({
uri: `${REACT_APP_SERVER_BASE_URL}/graphql`,
cache: new InMemoryCache(),
headers: {
...(currentWorkspace?.currentCacheVersion && {
'X-Schema-Version': currentWorkspace.currentCacheVersion,
}),
},
defaultOptions: {
query: {
fetchPolicy: 'cache-first',
@ -60,7 +67,7 @@ export const useApolloFactory = (options: Partial<Options<any>> = {}) => {
return apolloRef.current.getClient();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [setTokenPair, isDebugMode, setPreviousUrl]);
}, [setTokenPair, isDebugMode, currentWorkspace?.currentCacheVersion, setPreviousUrl]);
useUpdateEffect(() => {
if (isDefined(apolloRef.current)) {

View File

@ -60,6 +60,7 @@ export class ApolloFactory<TCacheShape> implements ApolloManager<TCacheShape> {
return {
headers: {
...headers,
...options.headers,
authorization: this.tokenPair?.accessToken.token
? `Bearer ${this.tokenPair?.accessToken.token}`
: '',

View File

@ -13,6 +13,7 @@ export type CurrentWorkspace = Pick<
| 'subscriptionStatus'
| 'activationStatus'
| 'currentBillingSubscription'
| 'currentCacheVersion'
>;
export const currentWorkspaceState = createState<CurrentWorkspace | null>({

View File

@ -35,6 +35,7 @@ export const GET_CURRENT_USER = gql`
value
workspaceId
}
currentCacheVersion
currentBillingSubscription {
status
interval