Skip to content

Commit

Permalink
fix(subgraph): update types of ApiContext (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 authored Jan 9, 2024
1 parent f6251a7 commit 755a01b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cowprotocol/cow-sdk",
"version": "4.0.6",
"version": "4.0.7",
"license": "(MIT OR Apache-2.0)",
"files": [
"/dist"
Expand Down
37 changes: 23 additions & 14 deletions src/subgraph/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ import { LastDaysVolumeQuery, LastHoursVolumeQuery, TotalsQuery } from './graphq
import { LAST_DAYS_VOLUME_QUERY, LAST_HOURS_VOLUME_QUERY, TOTALS_QUERY } from './queries'
import { DocumentNode } from 'graphql/index'
import { request, Variables } from 'graphql-request'
import { ApiContext, CowEnv, DEFAULT_COW_API_CONTEXT, PartialApiContext } from '../common/configs'
import { ApiContext, CowEnv, DEFAULT_COW_API_CONTEXT } from '../common/configs'
import { SupportedChainId } from '../common/chains'

const SUBGRAPH_BASE_URL = 'https://api.thegraph.com/subgraphs/name/cowprotocol'

type SubgraphApiBaseUrls = Record<SupportedChainId, string | null>

interface SubgraphApiContext extends Omit<ApiContext, 'baseUrls'> {
baseUrls?: SubgraphApiBaseUrls
}

type PartialSubgraphApiContext = Partial<SubgraphApiContext>

/**
* CoW Protocol Production Subgraph API configuration.
* @see {@link https://api.thegraph.com/subgraphs/name/cowprotocol/cow}
Expand Down Expand Up @@ -42,13 +48,13 @@ export const SUBGRAPH_STAGING_CONFIG: SubgraphApiBaseUrls = {
export class SubgraphApi {
API_NAME = 'CoW Protocol Subgraph'

public context: ApiContext
public context: SubgraphApiContext

/**
* Create a new CoW Protocol API instance.
* @param context Any properties of the {@link ApiContext} may be overridden by passing a {@link PartialApiContext}.
* @param context Any properties of the {@link SubgraphApiContext} may be overridden by passing a {@link PartialSubgraphApiContext}.
*/
constructor(context: PartialApiContext = {}) {
constructor(context: PartialSubgraphApiContext = {}) {
this.context = {
...DEFAULT_COW_API_CONTEXT,
...context,
Expand All @@ -60,43 +66,46 @@ export class SubgraphApi {
* @param contextOverride Override the context for this call only.
* @returns The totals for the CoW Protocol.
*/
async getTotals(contextOverride: PartialApiContext = {}): Promise<TotalsQuery['totals'][0]> {
async getTotals(contextOverride: PartialSubgraphApiContext = {}): Promise<TotalsQuery['totals'][0]> {
const response = await this.runQuery<TotalsQuery>(TOTALS_QUERY, undefined, contextOverride)
return response.totals[0]
}

/**
* Query the volume over the last N days from TheGraph for the CoW Protocol.
* @param {number} days The number of days to query.
* @param {PartialApiContext} contextOverride Override the context for this call only.
* @param {PartialSubgraphApiContext} contextOverride Override the context for this call only.
* @returns The volume for the last N days.
*/
async getLastDaysVolume(days: number, contextOverride: PartialApiContext = {}): Promise<LastDaysVolumeQuery> {
async getLastDaysVolume(days: number, contextOverride: PartialSubgraphApiContext = {}): Promise<LastDaysVolumeQuery> {
return this.runQuery<LastDaysVolumeQuery>(LAST_DAYS_VOLUME_QUERY, { days }, contextOverride)
}

/**
* Query the volume over the last N hours from TheGraph for the CoW Protocol.
* @param {number} hours The number of hours to query.
* @param {PartialApiContext} contextOverride Override the context for this call only.
* @param {PartialSubgraphApiContext} contextOverride Override the context for this call only.
* @returns The volume for the last N hours.
*/
async getLastHoursVolume(hours: number, contextOverride: PartialApiContext = {}): Promise<LastHoursVolumeQuery> {
async getLastHoursVolume(
hours: number,
contextOverride: PartialSubgraphApiContext = {}
): Promise<LastHoursVolumeQuery> {
return this.runQuery<LastHoursVolumeQuery>(LAST_HOURS_VOLUME_QUERY, { hours }, contextOverride)
}

/**
* Run a query against the CoW Protocol Subgraph.
* @param {string | DocumentNode} query GQL query string or DocumentNode.
* @param {Variables | undefined} variables To be passed to the query.
* @param {PartialApiContext} contextOverride Override the context for this call only.
* @param {PartialSubgraphApiContext} contextOverride Override the context for this call only.
* @returns Results of the query.
* @throws {@link CowError} if the query fails.
*/
async runQuery<T>(
query: string | DocumentNode,
variables: Variables | undefined = undefined,
contextOverride: PartialApiContext = {}
contextOverride: PartialSubgraphApiContext = {}
): Promise<T> {
const { chainId, env } = this.getContextWithOverride(contextOverride)
const baseUrl = this.getEnvConfigs(env)[chainId]
Expand All @@ -117,10 +126,10 @@ export class SubgraphApi {

/**
* Override parts of the context for a specific call.
* @param {PartialApiContext} contextOverride Override the context for this call only.
* @returns {ApiContext} The context with the override applied.
* @param {PartialSubgraphApiContext} contextOverride Override the context for this call only.
* @returns {SubgraphApiContext} The context with the override applied.
*/
private getContextWithOverride(contextOverride: PartialApiContext = {}): ApiContext {
private getContextWithOverride(contextOverride: PartialSubgraphApiContext = {}): SubgraphApiContext {
return { ...this.context, ...contextOverride }
}

Expand Down

0 comments on commit 755a01b

Please sign in to comment.