@apollo/client/core#ApolloClient TypeScript Examples
The following examples show how to use
@apollo/client/core#ApolloClient.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: entities-get.ts From amplication with Apache License 2.0 | 6 votes |
export async function getEntity(
client: ApolloClient<NormalizedCacheObject>,
id: string
): Promise<models.Entity> {
const { data } = await client.query<{
entity: models.Entity;
}>({
query: GET_ENTITY,
variables: {
id,
},
});
return data.entity;
}
Example #2
Source File: getTradingFeeApr.ts From beefy-api with MIT License | 6 votes |
getVariableTradingFeeApr = async (
client: ApolloClient<NormalizedCacheObject>,
pairAddresses: string[],
liquidityProviderFee: number[]
) => {
const [start, end] = getUtcSecondsFromDayRange(1, 2);
const pairAddressToAprMap: Record<string, BigNumber> = {};
try {
let {
data: { pairDayDatas },
}: { data: { pairDayDatas: PairDayData[] } } = await client.query({
query: pairDayDataQuery(addressesToLowercase(pairAddresses), start, end),
});
let i = 0;
for (const pairDayData of pairDayDatas) {
const pairAddress = pairDayData.id.split('-')[0].toLowerCase();
pairAddressToAprMap[pairAddress] = new BigNumber(pairDayData.dailyVolumeUSD)
.times(liquidityProviderFee[i])
.times(365)
.dividedBy(pairDayData.reserveUSD);
i++;
}
} catch (e) {
console.error('> getVariableTradingFeeApr error', pairAddresses[0]);
}
return pairAddressToAprMap;
}
Example #3
Source File: getTradingFeeApr.ts From beefy-api with MIT License | 6 votes |
getTradingFeeAprBalancer = async (
client: ApolloClient<NormalizedCacheObject>,
pairAddresses: string[],
liquidityProviderFee: number
) => {
const blockTime = await getBlockTime(250);
const currentBlock = await getBlockNumber(250);
const pastBlock = Math.floor(currentBlock - 86400 / blockTime);
const pairAddressesToAprMap: Record<string, BigNumber> = {};
try {
const queryCurrent = await client.query({
query: poolsDataQuery(addressesToLowercase(pairAddresses), currentBlock),
});
const queryPast = await client.query({
query: poolsDataQuery(addressesToLowercase(pairAddresses), pastBlock),
});
const poolDayDatas0 = queryCurrent.data.pools;
const poolDayDatas1 = queryPast.data.pools;
for (const pool of poolDayDatas0) {
const pair = pool.address.toLowerCase();
const pastPool = poolDayDatas1.filter(p => {
return p.address === pool.address;
})[0];
pairAddressesToAprMap[pair] = new BigNumber(pool.totalSwapFee)
.minus(pastPool.totalSwapFee)
.times(365)
.dividedBy(pool.totalLiquidity);
}
} catch (e) {
console.error('> getTradingFeeAprBalancer error', pairAddresses[0]);
}
return pairAddressesToAprMap;
}
Example #4
Source File: getTradingFeeApr.ts From beefy-api with MIT License | 6 votes |
getYearlyPlatformTradingFees = async (
client: ApolloClient<NormalizedCacheObject>,
liquidityProviderFee: number
) => {
let yearlyTradingFeesUsd = new BigNumber(0);
const timestamp = Date.now();
try {
let data = await client.query({ query: dayDataQuery(timestamp) });
const dailyVolumeUSD = new BigNumber(data.data.uniswapDayData.dailyVolumeUSD);
yearlyTradingFeesUsd = dailyVolumeUSD.times(liquidityProviderFee).times(365);
} catch (e) {
console.error('> getYearlyPlatformTradingFees error');
console.log(e);
}
return yearlyTradingFeesUsd;
}
Example #5
Source File: getTradingFeeApr.ts From beefy-api with MIT License | 6 votes |
getTradingFeeApr = async (
client: ApolloClient<NormalizedCacheObject>,
pairAddresses: string[],
liquidityProviderFee: number
) => {
const [start, end] = getUtcSecondsFromDayRange(1, 2);
const pairAddressToAprMap: Record<string, BigNumber> = {};
try {
let {
data: { pairDayDatas },
}: { data: { pairDayDatas: PairDayData[] } } = await client.query({
query: pairDayDataQuery(addressesToLowercase(pairAddresses), start, end),
});
for (const pairDayData of pairDayDatas) {
const pairAddress = pairDayData.id.split('-')[0].toLowerCase();
pairAddressToAprMap[pairAddress] = new BigNumber(pairDayData.dailyVolumeUSD)
.times(liquidityProviderFee)
.times(365)
.dividedBy(pairDayData.reserveUSD);
}
} catch (e) {
console.error('> getTradingFeeApr error', pairAddresses[0]);
}
return pairAddressToAprMap;
}
Example #6
Source File: test-runner.ts From balancer-subgraph-v2 with MIT License | 6 votes |
runTestCases = async (groupName: string, testCases: TestCase[]): void => {
const linkOptions = { uri: SUBGRAPH_QUERY_ENDPOINT, fetch };
const link = createHttpLink(linkOptions);
const cache = new InMemoryCache();
let aClient = new ApolloClient({ link, cache });
describe(`${groupName} resolvers`, () => {
for (let testCase of testCases) {
it(testCase.id, async () => {
const res = await aClient.query({
query: testCase.query,
variables: testCase.variables || {},
});
expect(res).toMatchSnapshot();
});
}
});
}
Example #7
Source File: getTradingFeeApr.ts From beefy-api with MIT License | 6 votes |
getYearlyJoePlatformTradingFees = async (
client: ApolloClient<NormalizedCacheObject>,
liquidityProviderFee: number
) => {
let yearlyTradingFeesUsd = new BigNumber(0);
const timestamp = Date.now();
try {
let data = await client.query({ query: joeDayDataQuery(timestamp) });
const dailyVolumeUSD = new BigNumber(data.data.dayData.volumeUSD);
yearlyTradingFeesUsd = dailyVolumeUSD.times(liquidityProviderFee).times(365);
} catch (e) {
console.error('> getYearlyJoePlatformTradingFees error');
}
return yearlyTradingFeesUsd;
}
Example #8
Source File: getTradingFeeApr.ts From beefy-api with MIT License | 6 votes |
getYearlyTradingFeesForSJOE = async (
client: ApolloClient<NormalizedCacheObject>,
liquidityProviderFee: number
) => {
let yearlyTradingFeesUsd = new BigNumber(0);
const [start0, end0] = getUtcSecondsFromDayRange(1, 8);
try {
let data = await client.query({ query: joeDayDataRangeQuery(start0, end0) });
const dayData = data.data.dayDatas.map(data => new BigNumber(data.volumeUSD));
const totalVolume = BigNumber.sum.apply(null, dayData);
const avgVolume = totalVolume.dividedBy(7);
const dailyTradingApr = avgVolume.times(liquidityProviderFee);
yearlyTradingFeesUsd = dailyTradingApr.times(365);
} catch (e) {
console.error('> getYearlyTradingFeesForSJOE error');
}
return yearlyTradingFeesUsd;
}
Example #9
Source File: getTradingFeeApr.ts From beefy-api with MIT License | 6 votes |
getYearlyTradingFeesForProtocols = async (
client: ApolloClient<NormalizedCacheObject>,
liquidityProviderFee: number
) => {
let yearlyTradingFeesUsd = new BigNumber(0);
const [start0, end0] = getUtcSecondsFromDayRange(1, 8);
try {
let data = await client.query({ query: protocolDayDataRangeQuery(start0, end0) });
const dayData = data.data.uniswapDayDatas.map(data => new BigNumber(data.dailyVolumeUSD));
const totalVolume = BigNumber.sum.apply(null, dayData);
const avgVolume = totalVolume.dividedBy(7);
const dailyTradingApr = avgVolume.times(liquidityProviderFee);
yearlyTradingFeesUsd = dailyTradingApr.times(365);
} catch (e) {
console.error('> getYearlyTradingFeesForProtocols error');
}
return yearlyTradingFeesUsd;
}
Example #10
Source File: fields-update.ts From amplication with Apache License 2.0 | 6 votes |
export async function updateField(
client: ApolloClient<NormalizedCacheObject>,
data: models.EntityFieldUpdateInput,
where: models.WhereUniqueInput,
relatedFieldName?: string | undefined,
relatedFieldDisplayName?: string | undefined
): Promise<models.EntityField> {
const { data: fieldData } = await client.mutate<{
updateEntityField: models.EntityField;
}>({
mutation: UPDATE_ENTITY_FIELD,
variables: {
data,
where,
relatedFieldName,
relatedFieldDisplayName,
},
});
if (!fieldData) {
throw new Error('no data');
}
return fieldData.updateEntityField;
}
Example #11
Source File: fields-list.ts From amplication with Apache License 2.0 | 6 votes |
export async function getFields(
client: ApolloClient<NormalizedCacheObject>,
entityId: string,
orderBy: string | undefined,
whereName: string | undefined
): Promise<models.EntityField[]> {
const { data } = await client.query<{
entity: models.Entity;
}>({
query: GET_FIELDS,
variables: {
entityId,
orderBy,
whereName,
},
});
return data.entity.fields || [];
}
Example #12
Source File: fields-create-by-display-name.ts From amplication with Apache License 2.0 | 6 votes |
export async function createFieldByDisplayName(
client: ApolloClient<NormalizedCacheObject>,
data: models.EntityFieldCreateByDisplayNameInput
): Promise<models.EntityField> {
const { data: fieldData } = await client.mutate<{
createEntityFieldByDisplayName: models.EntityField;
}>({
mutation: CREATE_ENTITY_FIELD,
variables: {
data,
},
});
if (!fieldData) {
throw new Error('no data');
}
return fieldData.createEntityFieldByDisplayName;
}
Example #13
Source File: entities-update.ts From amplication with Apache License 2.0 | 6 votes |
export async function updateEntity(
client: ApolloClient<NormalizedCacheObject>,
data: models.EntityUpdateInput,
where: models.WhereUniqueInput
): Promise<models.Entity> {
const { data: entityData } = await client.mutate<{
updateEntity: models.Entity;
}>({
mutation: UPDATE_ENTITY,
variables: {
data,
where,
},
});
if (!entityData) {
throw new Error('no data');
}
return entityData?.updateEntity;
}
Example #14
Source File: entities-list.ts From amplication with Apache License 2.0 | 6 votes |
export async function getEntities(
client: ApolloClient<NormalizedCacheObject>,
appId: string,
orderBy: string | undefined,
whereName: string | undefined
): Promise<models.Entity[]> {
const { data } = await client.query<{ entities: models.Entity[] }>({
query: GET_ENTITIES,
variables: {
appId,
orderBy,
whereName,
},
});
return data.entities;
}
Example #15
Source File: entities-create.ts From amplication with Apache License 2.0 | 6 votes |
export async function createEntity(
client: ApolloClient<NormalizedCacheObject>,
data: models.EntityCreateInput
): Promise<models.Entity> {
const { data: entityData } = await client.mutate<{
createOneEntity: models.Entity;
}>({
mutation: CREATE_ENTITY,
variables: {
data,
},
});
if (!entityData) {
throw new Error('no data');
}
return entityData?.createOneEntity;
}
Example #16
Source File: apps-update.ts From amplication with Apache License 2.0 | 6 votes |
export async function updateApp(
client: ApolloClient<NormalizedCacheObject>,
appId: string,
data: models.AppUpdateInput
): Promise<models.App> {
const { data: appData } = await client.mutate<{ updateApp: models.App }>({
mutation: UPDATE_APP,
variables: {
appId,
data,
},
});
if (!appData) {
throw new Error('no data');
}
return appData.updateApp;
}
Example #17
Source File: apps-list.ts From amplication with Apache License 2.0 | 6 votes |
export async function getApps(
client: ApolloClient<NormalizedCacheObject>
): Promise<models.App[]> {
const { data } = await client.query<{ apps: models.App[] }>({
query: GET_APPS,
});
return data.apps;
}
Example #18
Source File: apps-get.ts From amplication with Apache License 2.0 | 6 votes |
export async function getApp(
client: ApolloClient<NormalizedCacheObject>,
id: string
): Promise<models.App> {
const { data } = await client.query<{ app: models.App }>({
query: GET_APP,
variables: {
id,
},
});
return data.app;
}
Example #19
Source File: apps-create.ts From amplication with Apache License 2.0 | 6 votes |
export async function createApp(
client: ApolloClient<NormalizedCacheObject>,
name: string,
description: string
): Promise<models.App> {
const { data } = await client.mutate<{ createApp: models.App }>({
mutation: CREATE_APP,
variables: {
data: {
name,
description,
},
},
});
if (!data) {
throw new Error('no data');
}
return data.createApp;
}
Example #20
Source File: apps-commit.ts From amplication with Apache License 2.0 | 6 votes |
export async function commitChanges(
client: ApolloClient<NormalizedCacheObject>,
appId: string,
message: string
): Promise<models.Commit> {
const { data } = await client.mutate<{ commit: models.Commit }>({
mutation: COMMIT_CHANGES,
variables: {
message,
appId,
},
});
if (!data) {
throw new Error('no data');
}
return data.commit;
}
Example #21
Source File: getTradingFeeApr.ts From beefy-api with MIT License | 6 votes |
getYearlyBalancerPlatformTradingFees = async (
client: ApolloClient<NormalizedCacheObject>,
liquidityProviderFeeShare: number
) => {
const blockTime = await getBlockTime(250);
const currentBlock = await getBlockNumber(250);
const pastBlock = Math.floor(currentBlock - 86400 / blockTime);
let yearlyTradingFeesUsd = new BigNumber(0);
try {
const currentData = await client.query({ query: balancerDataQuery(currentBlock) });
const pastData = await client.query({ query: balancerDataQuery(pastBlock) });
const currentSwapFee = new BigNumber(currentData.data.balancers[0].totalSwapFee);
const pastSwapFee = new BigNumber(pastData.data.balancers[0].totalSwapFee);
const dailySwapFeeUsd = currentSwapFee.minus(pastSwapFee);
yearlyTradingFeesUsd = dailySwapFeeUsd.times(365).times(liquidityProviderFeeShare);
} catch (e) {
console.error('> getYearlyBalancerPlatformTradingFees error');
}
return yearlyTradingFeesUsd;
}
Example #22
Source File: nftx.service.ts From runebot with MIT License | 6 votes |
constructor(
protected readonly configService: AppConfigService,
protected readonly cacheService: CacheService,
protected readonly etherService: EthereumService,
protected readonly dataStoreService: DataStoreService,
) {
super();
this._nftxClient = new ApolloClient({
uri: this.configService.bot.nftxApi,
cache: new InMemoryCache(),
defaultOptions: this.apolloDefaultOptions,
});
this._sushiClient = new ApolloClient({
uri: this.configService.bot.sushiApi,
cache: new InMemoryCache(),
defaultOptions: this.apolloDefaultOptions,
});
}
Example #23
Source File: price-scan.ts From runebot with MIT License | 6 votes |
async function getVaultTokens() {
const ids = [];
const url = `https://api.thegraph.com/subgraphs/name/nftx-project/nftx-v2`;
const query = `
query {
vault(id:"0x87931e7ad81914e7898d07c68f145fc0a553d8fb") {
holdings (first: 1000) {
tokenId
}
}
}
`
const client = new ApolloClient({
uri: url,
cache: new InMemoryCache()
});
try {
const response = await client.query({
query: gql(query)
})
for (const wiz of response.data.vault.holdings) {
ids.push(wiz.tokenId);
}
} catch(error) {
console.log(error);
}
checkVaultIds(ids);
}
Example #24
Source File: client.ts From vvs-ui with GNU General Public License v3.0 | 5 votes |
blockClient = new ApolloClient({
link: new HttpLink({
fetch,
uri: process.env.BLOCKS_ENDPOINT,
}),
cache: new InMemoryCache(),
})
Example #25
Source File: api.service.ts From etherspot-sdk with MIT License | 5 votes |
protected onInit(): void {
const httpLink = new HttpLink({
fetch,
uri: buildApiUri(this.options, 'http'),
});
const wsLink = new WebSocketLink({
webSocketImpl: WebSocket,
uri: buildApiUri(this.options, 'ws', 'graphql'),
options: {
reconnect: true,
lazy: true,
},
});
const authLink = setContext(async () => {
const {
accountService, //
sessionService,
projectService,
} = this.services;
return {
headers: {
...accountService.headers,
...sessionService.headers,
...projectService.headers,
},
};
});
const link = split(
// split based on operation type
({ query }) => {
const definition = getMainDefinition(query);
return definition.kind === 'OperationDefinition' && definition.operation === 'subscription';
},
wsLink,
authLink.concat(httpLink),
);
this.apolloClient = new ApolloClient({
link,
cache: this.cache,
});
}
Example #26
Source File: api.service.ts From etherspot-sdk with MIT License | 5 votes |
private apolloClient: ApolloClient<NormalizedCacheObject>;
Example #27
Source File: client.ts From vvs-ui with GNU General Public License v3.0 | 5 votes |
client = new ApolloClient({
link: new HttpLink({
fetch,
uri: process.env.EXCHANGE_ENDPOINT,
}),
cache: new InMemoryCache(),
})
Example #28
Source File: graphql-client.ts From dt-mergebot with MIT License | 5 votes |
client = new ApolloClient({ cache, link })
Example #29
Source File: getTradingFeeApr.ts From beefy-api with MIT License | 5 votes |
getTradingFeeAprSushi = async (
client: ApolloClient<NormalizedCacheObject>,
pairAddresses: string[],
liquidityProviderFee: number
) => {
const [start0, end0] = getUtcSecondsFromDayRange(1, 2);
const [start1, end1] = getUtcSecondsFromDayRange(3, 4);
const pairAddressToAprMap: Record<string, BigNumber> = {};
try {
let queryResponse0 = await client.query({
query: pairDayDataSushiQuery(addressesToLowercase(pairAddresses), start0, end0),
});
let queryResponse1 = await client.query({
query: pairDayDataSushiQuery(addressesToLowercase(pairAddresses), start1, end1),
});
const pairDayDatas0 = queryResponse0.data.pairs.map(pair => pair.dayData[0]);
const pairDayDatas1 = queryResponse1.data.pairs.map(pair => pair.dayData[0]);
for (const pairDayData of zip([pairDayDatas0, pairDayDatas1])) {
if (pairDayData && pairDayData[0] && pairDayData[1]) {
const pairAddress = pairDayData[0].id.split('-')[0].toLowerCase();
const avgVol = new BigNumber(pairDayData[0].volumeUSD)
.plus(pairDayData[1].volumeUSD)
.dividedBy(2);
const avgReserve = new BigNumber(pairDayData[0].reserveUSD)
.plus(pairDayData[1].reserveUSD)
.dividedBy(2);
pairAddressToAprMap[pairAddress] = new BigNumber(avgVol)
.times(liquidityProviderFee)
.times(365)
.dividedBy(avgReserve);
}
}
} catch (e) {
console.error('> getTradingFeeAprSushi error', pairAddresses[0]);
}
return pairAddressToAprMap;
}