-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add and query EBO subgraph with logging
- Loading branch information
hopeyen
committed
Sep 8, 2022
1 parent
ea132db
commit 5f9af3f
Showing
18 changed files
with
400 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import axios, { AxiosInstance, AxiosResponse } from 'axios' | ||
import { Logger } from '@graphprotocol/common-ts' | ||
import { DocumentNode, print } from 'graphql' | ||
import { OperationResult, CombinedError } from '@urql/core' | ||
|
||
export interface BlockOracleSubgraphCreateOptions { | ||
logger: Logger | ||
endpoint: string | ||
} | ||
|
||
interface BlockOracleSubgraphOptions { | ||
logger: Logger | ||
endpoint: string | ||
} | ||
|
||
export type QueryResult<Data> = Pick< | ||
OperationResult<Data>, | ||
'error' | 'data' | 'extensions' | ||
> | ||
|
||
export class BlockOracleSubgraph { | ||
logger: Logger | ||
endpointClient: AxiosInstance | ||
|
||
private constructor(options: BlockOracleSubgraphOptions) { | ||
this.logger = options.logger | ||
|
||
this.endpointClient = axios.create({ | ||
baseURL: options.endpoint, | ||
headers: { 'content-type': 'application/json' }, | ||
|
||
// Don't parse responses as JSON | ||
responseType: 'text', | ||
|
||
// Don't transform responses | ||
transformResponse: (data) => data, | ||
}) | ||
} | ||
|
||
public static async create({ | ||
logger: parentLogger, | ||
endpoint, | ||
}: BlockOracleSubgraphCreateOptions): Promise<BlockOracleSubgraph> { | ||
const logger = parentLogger.child({ | ||
component: 'BlockOracleSubgraph', | ||
endpoint, | ||
}) | ||
|
||
// Create the EpochBlock subgraph instance | ||
const blockOracleSubgraph = new BlockOracleSubgraph({ | ||
logger, | ||
endpoint, | ||
}) | ||
// Any checks to be made after creating? | ||
|
||
return blockOracleSubgraph | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
async query<Data = any>( | ||
query: DocumentNode, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
variables?: Record<string, any>, | ||
): Promise<QueryResult<Data>> { | ||
const response = await this.endpointClient.post('', { | ||
query: print(query), | ||
variables, | ||
}) | ||
const data = JSON.parse(response.data) | ||
if (data.errors) { | ||
return { error: new CombinedError({ graphQLErrors: data.errors }) } | ||
} | ||
return data | ||
} | ||
|
||
async queryRaw(body: string): Promise<AxiosResponse> { | ||
return await this.endpointClient.post('', body) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.