feat: check if company/person saved (chrome-extension) (#4280)

* add twenty icon

* rest api calls for company

* check if company exists

* refacto

* person/company saved call

* gql codegen init

* type defs

* build fix

* DB calls with gql codegen and apollo integration
This commit is contained in:
Aditya Pimpalkar
2024-03-26 13:37:36 +00:00
committed by GitHub
parent c54acb35b6
commit 5c5dcf5cb5
20 changed files with 6107 additions and 241 deletions

View File

@ -0,0 +1,18 @@
import { ApolloClient, InMemoryCache } from '@apollo/client';
const getApolloClient = async () => {
const { apiKey } = await chrome.storage.local.get('apiKey');
const { serverBaseUrl } = await chrome.storage.local.get('serverBaseUrl');
return new ApolloClient({
cache: new InMemoryCache(),
uri: `${
serverBaseUrl ? serverBaseUrl : import.meta.env.VITE_SERVER_BASE_URL
}/graphql`,
headers: {
Authorization: `Bearer ${apiKey}`,
},
});
};
export default getApolloClient;

View File

@ -1,31 +1,34 @@
const requestDb = async (query: string) => {
const { apiKey } = await chrome.storage.local.get('apiKey');
const { serverBaseUrl } = await chrome.storage.local.get('serverBaseUrl');
import { OperationVariables } from '@apollo/client';
import { DocumentNode } from 'graphql';
const options = {
method: 'POST',
body: JSON.stringify({ query }),
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${apiKey}`,
},
};
import getApolloClient from '~/utils/apolloClient';
const response = await fetch(
`${
serverBaseUrl ? serverBaseUrl : import.meta.env.VITE_SERVER_BASE_URL
}/graphql`,
options,
);
export const callQuery = async <T>(
query: DocumentNode,
variables?: OperationVariables,
): Promise<T | null> => {
const client = await getApolloClient();
if (!response.ok) {
// TODO: Handle error gracefully and remove the console statement.
/* eslint-disable no-console */
console.error(response);
}
const { data, error } = await client.query<T>({ query, variables });
return await response.json();
if (error) throw new Error(error.message);
if (data) return data;
return null;
};
export default requestDb;
export const callMutation = async <T>(
mutation: DocumentNode,
variables?: OperationVariables,
): Promise<T | null> => {
const client = await getApolloClient();
const { data, errors } = await client.mutate<T>({ mutation, variables });
if (errors) throw new Error(errors[0].message);
if (data) return data;
return null;
};