Skip to content

Commit

Permalink
apollo v4
Browse files Browse the repository at this point in the history
  • Loading branch information
gmbronco committed Jan 15, 2025
1 parent 64f33bc commit c0b8122
Show file tree
Hide file tree
Showing 15 changed files with 195 additions and 103 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-houses-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'backend': minor
---

apollo v4
54 changes: 54 additions & 0 deletions apps/api/apollo/sentry-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { captureException } from '@sentry/node';
import { ApolloServerPlugin } from '@apollo/server';
import { ResolverContext } from '../gql/resolver-context';

export const apolloSentryPlugin: ApolloServerPlugin<ResolverContext> = {
async requestDidStart({ request }) {
return {
// This will send any errors captured by Apollo Server to Sentry
async didEncounterErrors(ctx) {
for (const err of ctx.errors) {
// Ignore syntax errors
if (
[
'GRAPHQL_PARSE_FAILED',
'GRAPHQL_VALIDATION_FAILED',
'BAD_USER_INPUT',
'ACCOUNT_ADDRESS_REQUIRED',
'ACCESS_DENIED',
'NOT_FOUND',
].includes(err.extensions?.code as string)
) {
continue;
}

// Ignore specific errors
if (err.message && err.message === 'SOR queryBatchSwap failed') {
continue;
}

// Potentially set transaction name to the operation name,
// add tags and fingerprint to group errors

// Sentry.withScope((scope) => {
// let name = request.operationName;
// if (!name) {
// name = request.query
// ?.substring(
// request.query?.indexOf('{') + 1,
// request.query?.indexOf('(') || request.query?.length,
// )
// .replace(/\n/g, '')
// .replace(/\s/g, '');
// }
// scope.setTransactionName(`POST /graphql ${name}`);
// Sentry.captureException(err);
// });

captureException(err);
}
},
async willSendResponse({}) {},
};
},
};
5 changes: 4 additions & 1 deletion apps/api/gql/resolvers/content.resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Resolvers } from '../../../../schema';
import { headerChain } from '../../../../modules/context/header-chain';
import { SanityContentService } from '../../../../modules/content/sanity-content.service';
import { GraphQLError } from 'graphql';

const contentResolvers: Resolvers = {
Query: {
Expand All @@ -9,7 +10,9 @@ const contentResolvers: Resolvers = {
if (!chain && currentChain) {
chain = currentChain;
} else if (!chain) {
throw new Error('contentGetNewsItems error: Provide "chain" param');
throw new GraphQLError('Provide "chain" param', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}
const sanityContent = new SanityContentService();
return sanityContent.getNewsItems(chain);
Expand Down
33 changes: 25 additions & 8 deletions apps/api/gql/resolvers/pool.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
FXPoolsController,
} from '../../../../modules/controllers';
import { chainIdToChain } from '../../../../modules/network/chain-id-to-chain';
import { GraphQLError } from 'graphql';

const balancerResolvers: Resolvers = {
Query: {
Expand All @@ -19,7 +20,9 @@ const balancerResolvers: Resolvers = {
if (!chain && currentChain) {
chain = currentChain;
} else if (!chain) {
throw new Error('poolGetPool error: Provide "chain" param');
throw new GraphQLError('Provide "chain" param', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}
return poolService.getGqlPool(id, chain, userAddress ? userAddress : undefined);
},
Expand All @@ -38,7 +41,9 @@ const balancerResolvers: Resolvers = {
if (!args.where?.chainIn && currentChain) {
args.where = { ...args.where, chainIn: [currentChain] };
} else if (!args.where?.chainIn) {
throw new Error('poolGetSwaps error: Provide "where.chainIn" param');
throw new GraphQLError('Provide "chainIn" param', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}
return poolService.getPoolSwaps(args);
},
Expand All @@ -48,7 +53,9 @@ const balancerResolvers: Resolvers = {
if (!args.where?.chainIn && currentChain) {
args.where = { ...args.where, chainIn: [currentChain] };
} else if (!args.where?.chainIn) {
throw new Error('poolGetBatchSwaps error: Provide "where.chainIn" param');
throw new GraphQLError('Provide "chainIn" param', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}
return poolService.getPoolBatchSwaps(args);
},
Expand All @@ -58,7 +65,9 @@ const balancerResolvers: Resolvers = {
if (!args.where?.chainIn && currentChain) {
args.where = { ...args.where, chainIn: [currentChain] };
} else if (!args.where?.chainIn) {
throw new Error('poolGetJoinExits error: Provide "where.chainIn" param');
throw new GraphQLError('Provide "chainIn" param', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}
return poolService.getPoolJoinExits(args);
},
Expand All @@ -80,7 +89,9 @@ const balancerResolvers: Resolvers = {
if (!chains && currentChain) {
chains = [currentChain];
} else if (!chains) {
throw new Error('poolGetFeaturedPoolGroups error: Provide "chains" param');
throw new GraphQLError('Provide "chains" param', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}
return poolService.getFeaturedPoolGroups(chains);
},
Expand All @@ -92,7 +103,9 @@ const balancerResolvers: Resolvers = {
if (!chain && currentChain) {
chain = currentChain;
} else if (!chain) {
throw new Error('poolGetSnapshots error: Provide "chain" param');
throw new GraphQLError('Provide "chain" param', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}
const snapshots = await poolService.getSnapshotsForPool(id, chain, range);

Expand All @@ -118,7 +131,9 @@ const balancerResolvers: Resolvers = {
const chain = headerChain();

if (!chain) {
throw new Error('poolSyncAllPoolsFromSubgraph error: Provide chain header');
throw new GraphQLError('Provide "chainId" header', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}

return PoolController().addPoolsV2(chain);
Expand All @@ -135,7 +150,9 @@ const balancerResolvers: Resolvers = {

const currentChain = headerChain();
if (!currentChain) {
throw new Error('poolReloadStakingForAllPools error: Provide chain header');
throw new GraphQLError('Provide "chainId" header', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}

await poolService.reloadStakingForAllPools(args.stakingTypes, currentChain);
Expand Down
9 changes: 7 additions & 2 deletions apps/api/gql/resolvers/protocol.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { GqlLatestSyncedBlocks, Resolvers } from '../../../../schema';
import { protocolService } from '../../../../modules/protocol/protocol.service';
import { networkContext } from '../../../../modules/network/network-context.service';
import { headerChain } from '../../../../modules/context/header-chain';
import { GraphQLError } from 'graphql';

const protocolResolvers: Resolvers = {
Query: {
Expand All @@ -10,7 +11,9 @@ const protocolResolvers: Resolvers = {
if (!chain && currentChain) {
chain = currentChain;
} else if (!chain) {
throw new Error('poolGetPool error: Provide "chain" param');
throw new GraphQLError('Provide "chain" param', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}
return protocolService.getMetrics(chain);
},
Expand All @@ -19,7 +22,9 @@ const protocolResolvers: Resolvers = {
if (!chains && currentChain) {
chains = [currentChain];
} else if (!chains) {
throw new Error('tokenGetTokens error: Provide "chains" param');
throw new GraphQLError('Provide "chains" param', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}
return protocolService.getAggregatedMetrics(chains);
},
Expand Down
5 changes: 4 additions & 1 deletion apps/api/gql/resolvers/sor.resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Resolvers } from '../../../../schema';
import { sorService } from '../../../../modules/sor/sor.service';
import { headerChain } from '../../../../modules/context/header-chain';
import { GraphQLError } from 'graphql';

const balancerSdkResolvers: Resolvers = {
Query: {
Expand All @@ -9,7 +10,9 @@ const balancerSdkResolvers: Resolvers = {
if (!args.chain && currentChain) {
args.chain = currentChain;
} else if (!args.chain) {
throw new Error('sorGetSwaps error: Provide "chain" param');
throw new GraphQLError('Provide "chain" param', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}

return sorService.getSorSwaps(args);
Expand Down
37 changes: 28 additions & 9 deletions apps/api/gql/resolvers/token.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { syncLatestFXPrices } from '../../../../modules/token/latest-fx-price';
import { AllNetworkConfigsKeyedOnChain } from '../../../../modules/network/network-config';
import moment from 'moment';
import { TokenController } from '../../../../modules/controllers/token-controller';
import { GraphQLError } from 'graphql';

const resolvers: Resolvers = {
Query: {
Expand All @@ -15,7 +16,9 @@ const resolvers: Resolvers = {
if (!args.chains && currentChain) {
args.chains = [currentChain];
} else if (!args.chains) {
throw new Error('tokenGetTokens error: Provide "chains" param');
throw new GraphQLError('Provide "chains" param', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}
return tokenService.getTokenDefinitions(args);
},
Expand All @@ -24,7 +27,9 @@ const resolvers: Resolvers = {
if (!chains && currentChain) {
chains = [currentChain];
} else if (!chains) {
throw new Error('tokenGetCurrentPrices error: Provide "chains" param');
throw new GraphQLError('Provide "chains" param', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}
const prices = await tokenService.getCurrentTokenPrices(chains);

Expand Down Expand Up @@ -61,7 +66,9 @@ const resolvers: Resolvers = {
if (!chain && currentChain) {
chain = currentChain;
} else if (!chain) {
throw new Error('tokenGetTokenDynamicData error: Provide "chain" param');
throw new GraphQLError('Provide "chain" param', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}
const data = await tokenService.getTokenDynamicData(address, chain);

Expand All @@ -80,7 +87,9 @@ const resolvers: Resolvers = {
if (!chain && currentChain) {
chain = currentChain;
} else if (!chain) {
throw new Error('tokenGetTokensDynamicData error: Provide "chain" param');
throw new GraphQLError('Provide "chain" param', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}
const items = await tokenService.getTokensDynamicData(addresses, chain);

Expand All @@ -97,7 +106,9 @@ const resolvers: Resolvers = {
if (!chain && currentChain) {
chain = currentChain;
} else if (!chain) {
throw new Error('tokenGetPriceChartData error: Provide "chain" param');
throw new GraphQLError('Provide "chain" param', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}
const data = await tokenService.getTokenPriceForRange(address, range, chain);

Expand All @@ -112,7 +123,9 @@ const resolvers: Resolvers = {
if (!chain && currentChain) {
chain = currentChain;
} else if (!chain) {
throw new Error('tokenGetRelativePriceChartData error: Provide "chain" param');
throw new GraphQLError('Provide "chain" param', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}
const data = await tokenService.getRelativeDataForRange(tokenIn, tokenOut, range, chain);

Expand All @@ -127,7 +140,9 @@ const resolvers: Resolvers = {
if (!chain && currentChain) {
chain = currentChain;
} else if (!chain) {
throw new Error('tokenGetCandlestickChartData error: Provide "chain" param');
throw new GraphQLError('Provide "chain" param', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}
const data = await tokenService.getTokenPriceForRange(address, range, chain);

Expand All @@ -145,7 +160,9 @@ const resolvers: Resolvers = {
if (!chain && currentChain) {
chain = currentChain;
} else if (!chain) {
throw new Error('tokenGetTokenData error: Provide "chain" param');
throw new GraphQLError('Provide "chain" param', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}
const token = await tokenService.getToken(address, chain);
if (token) {
Expand All @@ -167,7 +184,9 @@ const resolvers: Resolvers = {
if (!chain && currentChain) {
chain = currentChain;
} else if (!chain) {
throw new Error('tokenGetProtocolTokenPrice error: Provide "chain" param');
throw new GraphQLError('Provide "chain" param', {
extensions: { code: 'GRAPHQL_VALIDATION_FAILED' },
});
}
return tokenService.getProtocolTokenPrice(chain);
},
Expand Down
Loading

0 comments on commit c0b8122

Please sign in to comment.