-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Graphql refactor (split large index files) (#290)
- Loading branch information
Showing
17 changed files
with
571 additions
and
522 deletions.
There are no files selected for viewing
File renamed without changes.
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,29 @@ | ||
import { Resolver, Query, Arg, Int } from 'type-graphql'; | ||
import dataSource from '../../db'; | ||
import { Epoch } from '../../models/Epoch'; | ||
import { EpochType } from '../types'; | ||
import { mapEpochToType } from './mappers'; | ||
|
||
@Resolver(() => EpochType) | ||
export class EpochResolver { | ||
@Query(() => [EpochType]) | ||
async epochs( | ||
@Arg('marketId', () => Int, { nullable: true }) marketId?: number | ||
): Promise<EpochType[]> { | ||
try { | ||
const where: { market?: { id: number } } = {}; | ||
if (marketId) { | ||
where.market = { id: marketId }; | ||
} | ||
|
||
const epochs = await dataSource.getRepository(Epoch).find({ | ||
where, | ||
}); | ||
|
||
return epochs.map(mapEpochToType); | ||
} catch (error) { | ||
console.error('Error fetching epochs:', error); | ||
throw new Error('Failed to fetch epochs'); | ||
} | ||
} | ||
} |
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,53 @@ | ||
import { Resolver, Query, Arg, Int, FieldResolver, Root } from 'type-graphql'; | ||
import dataSource from '../../db'; | ||
import { Market } from '../../models/Market'; | ||
import { Epoch } from '../../models/Epoch'; | ||
import { MarketType, EpochType } from '../types'; | ||
import { mapMarketToType, mapEpochToType } from './mappers'; | ||
|
||
@Resolver(() => MarketType) | ||
export class MarketResolver { | ||
@Query(() => [MarketType]) | ||
async markets(): Promise<MarketType[]> { | ||
try { | ||
const markets = await dataSource.getRepository(Market).find(); | ||
return markets.map(mapMarketToType); | ||
} catch (error) { | ||
console.error('Error fetching markets:', error); | ||
throw new Error('Failed to fetch markets'); | ||
} | ||
} | ||
|
||
@Query(() => MarketType, { nullable: true }) | ||
async market( | ||
@Arg('chainId', () => Int) chainId: number, | ||
@Arg('address', () => String) address: string | ||
): Promise<MarketType | null> { | ||
try { | ||
const market = await dataSource.getRepository(Market).findOne({ | ||
where: { chainId, address }, | ||
}); | ||
|
||
if (!market) return null; | ||
|
||
return mapMarketToType(market); | ||
} catch (error) { | ||
console.error('Error fetching market:', error); | ||
throw new Error('Failed to fetch market'); | ||
} | ||
} | ||
|
||
@FieldResolver(() => [EpochType]) | ||
async epochs(@Root() market: Market): Promise<EpochType[]> { | ||
try { | ||
const epochs = await dataSource.getRepository(Epoch).find({ | ||
where: { market: { id: market.id } }, | ||
}); | ||
|
||
return epochs.map(mapEpochToType); | ||
} catch (error) { | ||
console.error('Error fetching epochs:', error); | ||
throw new Error('Failed to fetch epochs'); | ||
} | ||
} | ||
} |
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,51 @@ | ||
import { Resolver, Query, Arg, Int } from 'type-graphql'; | ||
import dataSource from '../../db'; | ||
import { Position } from '../../models/Position'; | ||
import { PositionType } from '../types'; | ||
import { hydrateTransactions } from '../../helpers/hydrateTransactions'; | ||
import { mapPositionToType } from './mappers'; | ||
@Resolver(() => PositionType) | ||
export class PositionResolver { | ||
@Query(() => [PositionType]) | ||
async positions( | ||
@Arg('owner', () => String, { nullable: true }) owner?: string, | ||
@Arg('chainId', () => Int, { nullable: true }) chainId?: number, | ||
@Arg('marketAddress', () => String, { nullable: true }) marketAddress?: string | ||
): Promise<PositionType[]> { | ||
try { | ||
const where: any = {}; | ||
if (owner) { | ||
where.owner = owner; | ||
} | ||
if (chainId && marketAddress) { | ||
where.epoch = { | ||
market: { | ||
chainId, | ||
address: marketAddress, | ||
}, | ||
}; | ||
} | ||
|
||
const positions = await dataSource.getRepository(Position).find({ | ||
where, | ||
relations: [ | ||
'epoch', | ||
'epoch.market', | ||
'epoch.market.resource', | ||
'transactions', | ||
'transactions.event' | ||
], | ||
}); | ||
|
||
const hydratedPositions = positions.map((position) => { | ||
const hydratedTransactions = hydrateTransactions(position.transactions, false); | ||
return { ...position, transactions: hydratedTransactions }; | ||
}); | ||
|
||
return hydratedPositions.map(mapPositionToType); | ||
} catch (error) { | ||
console.error('Error fetching positions:', error); | ||
throw new Error('Failed to fetch positions'); | ||
} | ||
} | ||
} |
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,38 @@ | ||
import { Resolver, Query, Arg } from 'type-graphql'; | ||
import dataSource from '../../db'; | ||
import { Resource } from '../../models/Resource'; | ||
import { ResourceType } from '../types'; | ||
import { mapResourceToType } from './mappers'; | ||
|
||
@Resolver(() => ResourceType) | ||
export class ResourceResolver { | ||
@Query(() => [ResourceType]) | ||
async resources(): Promise<ResourceType[]> { | ||
try { | ||
const resources = await dataSource.getRepository(Resource).find(); | ||
return resources.map(mapResourceToType); | ||
} catch (error) { | ||
console.error('Error fetching resources:', error); | ||
throw new Error('Failed to fetch resources'); | ||
} | ||
} | ||
|
||
@Query(() => ResourceType, { nullable: true }) | ||
async resource( | ||
@Arg('slug', () => String) slug: string | ||
): Promise<ResourceType | null> { | ||
try { | ||
const resource = await dataSource.getRepository(Resource).findOne({ | ||
where: { slug }, | ||
relations: ['markets', 'resourcePrices'], | ||
}); | ||
|
||
if (!resource) return null; | ||
|
||
return mapResourceToType(resource); | ||
} catch (error) { | ||
console.error('Error fetching resource:', error); | ||
throw new Error('Failed to fetch resource'); | ||
} | ||
} | ||
} |
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,31 @@ | ||
import { Resolver, Query, Arg, Int } from 'type-graphql'; | ||
import dataSource from '../../db'; | ||
import { Transaction } from '../../models/Transaction'; | ||
import { TransactionType } from '../types'; | ||
import { hydrateTransactions } from '../../helpers/hydrateTransactions'; | ||
import { mapTransactionToType } from './mappers'; | ||
@Resolver(() => TransactionType) | ||
export class TransactionResolver { | ||
@Query(() => [TransactionType]) | ||
async transactions( | ||
@Arg('positionId', () => Int, { nullable: true }) positionId?: number | ||
): Promise<TransactionType[]> { | ||
try { | ||
const where: any = {}; | ||
if (positionId) { | ||
where.position = { id: positionId }; | ||
} | ||
|
||
const transactions = await dataSource.getRepository(Transaction).find({ | ||
where, | ||
relations: ['event', 'position'], | ||
}); | ||
|
||
const hydratedTransactions = hydrateTransactions(transactions, false); | ||
return hydratedTransactions.map(mapTransactionToType); | ||
} catch (error) { | ||
console.error('Error fetching transactions:', error); | ||
throw new Error('Failed to fetch transactions'); | ||
} | ||
} | ||
} |
Oops, something went wrong.