diff --git a/packages/rpc-graphql/src/index.ts b/packages/rpc-graphql/src/index.ts index 1053054e692..57f7528241c 100644 --- a/packages/rpc-graphql/src/index.ts +++ b/packages/rpc-graphql/src/index.ts @@ -2,8 +2,8 @@ import { makeExecutableSchema } from '@graphql-tools/schema'; import { graphql } from 'graphql'; import { createSolanaGraphQLContext } from './context'; -import { createSolanaGraphQLResolvers } from './resolvers'; import { createSolanaGraphQLTypeDefs } from './schema/type-defs'; +import { createSolanaGraphQLTypeResolvers } from './schema/type-resolvers'; export interface RpcGraphQL { query( @@ -21,7 +21,7 @@ export function createRpcGraphQL( maxMultipleAccountsBatchSize: config?.maxMultipleAccountsBatchSize ?? 100, }; const schema = makeExecutableSchema({ - resolvers: createSolanaGraphQLResolvers(), + resolvers: createSolanaGraphQLTypeResolvers(), typeDefs: createSolanaGraphQLTypeDefs(), }); return { diff --git a/packages/rpc-graphql/src/resolvers/account.ts b/packages/rpc-graphql/src/resolvers/account.ts index 1c8d7dd2fa2..62de8fc01b0 100644 --- a/packages/rpc-graphql/src/resolvers/account.ts +++ b/packages/rpc-graphql/src/resolvers/account.ts @@ -24,7 +24,7 @@ export type AccountResult = Partial> & { ownerProgram?: Address; }; -const resolveAccountData = () => { +export const resolveAccountData = () => { return ( parent: AccountResult | null, args: { @@ -124,159 +124,3 @@ export const resolveAccount = (fieldName?: string) => { return null; }; }; - -function resolveAccountType(accountResult: AccountResult) { - const { jsonParsedConfigs } = accountResult; - if (jsonParsedConfigs) { - if (jsonParsedConfigs.programName === 'nonce') { - return 'NonceAccount'; - } - if (jsonParsedConfigs.accountType === 'mint' && jsonParsedConfigs.programName === 'spl-token') { - return 'MintAccount'; - } - if (jsonParsedConfigs.accountType === 'account' && jsonParsedConfigs.programName === 'spl-token') { - return 'TokenAccount'; - } - if (jsonParsedConfigs.programName === 'stake') { - return 'StakeAccount'; - } - if (jsonParsedConfigs.accountType === 'vote' && jsonParsedConfigs.programName === 'vote') { - return 'VoteAccount'; - } - if ( - jsonParsedConfigs.accountType === 'lookupTable' && - jsonParsedConfigs.programName === 'address-lookup-table' - ) { - return 'LookupTableAccount'; - } - if (jsonParsedConfigs.programName === 'sysvar') { - if (jsonParsedConfigs.accountType === 'clock') { - return 'SysvarClockAccount'; - } - if (jsonParsedConfigs.accountType === 'epochRewards') { - return 'SysvarEpochRewardsAccount'; - } - if (jsonParsedConfigs.accountType === 'epochSchedule') { - return 'SysvarEpochScheduleAccount'; - } - if (jsonParsedConfigs.accountType === 'fees') { - return 'SysvarFeesAccount'; - } - if (jsonParsedConfigs.accountType === 'lastRestartSlot') { - return 'SysvarLastRestartSlotAccount'; - } - if (jsonParsedConfigs.accountType === 'recentBlockhashes') { - return 'SysvarRecentBlockhashesAccount'; - } - if (jsonParsedConfigs.accountType === 'rent') { - return 'SysvarRentAccount'; - } - if (jsonParsedConfigs.accountType === 'slotHashes') { - return 'SysvarSlotHashesAccount'; - } - if (jsonParsedConfigs.accountType === 'slotHistory') { - return 'SysvarSlotHistoryAccount'; - } - if (jsonParsedConfigs.accountType === 'stakeHistory') { - return 'SysvarStakeHistoryAccount'; - } - } - } - return 'GenericAccount'; -} - -export const accountResolvers = { - Account: { - __resolveType: resolveAccountType, - data: resolveAccountData(), - }, - GenericAccount: { - data: resolveAccountData(), - ownerProgram: resolveAccount('ownerProgram'), - }, - LookupTableAccount: { - authority: resolveAccount('authority'), - data: resolveAccountData(), - ownerProgram: resolveAccount('ownerProgram'), - }, - MintAccount: { - data: resolveAccountData(), - freezeAuthority: resolveAccount('freezeAuthority'), - mintAuthority: resolveAccount('mintAuthority'), - ownerProgram: resolveAccount('ownerProgram'), - }, - NonceAccount: { - authority: resolveAccount('authority'), - data: resolveAccountData(), - ownerProgram: resolveAccount('ownerProgram'), - }, - StakeAccount: { - data: resolveAccountData(), - ownerProgram: resolveAccount('ownerProgram'), - }, - StakeAccountDataMetaAuthorized: { - staker: resolveAccount('staker'), - withdrawer: resolveAccount('withdrawer'), - }, - StakeAccountDataMetaLockup: { - custodian: resolveAccount('custodian'), - }, - StakeAccountDataStakeDelegation: { - voter: resolveAccount('voter'), - }, - SysvarClockAccount: { - data: resolveAccountData(), - ownerProgram: resolveAccount('ownerProgram'), - }, - SysvarEpochRewardsAccount: { - data: resolveAccountData(), - ownerProgram: resolveAccount('ownerProgram'), - }, - SysvarEpochScheduleAccount: { - data: resolveAccountData(), - ownerProgram: resolveAccount('ownerProgram'), - }, - SysvarFeesAccount: { - data: resolveAccountData(), - ownerProgram: resolveAccount('ownerProgram'), - }, - SysvarLastRestartSlotAccount: { - data: resolveAccountData(), - ownerProgram: resolveAccount('ownerProgram'), - }, - SysvarRecentBlockhashesAccount: { - data: resolveAccountData(), - ownerProgram: resolveAccount('ownerProgram'), - }, - SysvarRentAccount: { - data: resolveAccountData(), - ownerProgram: resolveAccount('ownerProgram'), - }, - SysvarSlotHashesAccount: { - data: resolveAccountData(), - ownerProgram: resolveAccount('ownerProgram'), - }, - SysvarSlotHistoryAccount: { - data: resolveAccountData(), - ownerProgram: resolveAccount('ownerProgram'), - }, - SysvarStakeHistoryAccount: { - data: resolveAccountData(), - ownerProgram: resolveAccount('ownerProgram'), - }, - TokenAccount: { - data: resolveAccountData(), - mint: resolveAccount('mint'), - owner: resolveAccount('owner'), - ownerProgram: resolveAccount('ownerProgram'), - }, - VoteAccount: { - authorizedWithdrawer: resolveAccount('authorizedWithdrawer'), - data: resolveAccountData(), - node: resolveAccount('nodePubkey'), - ownerProgram: resolveAccount('ownerProgram'), - }, - VoteAccountDataAuthorizedVoter: { - authorizedVoter: resolveAccount('authorizedVoter'), - }, -}; diff --git a/packages/rpc-graphql/src/resolvers/block.ts b/packages/rpc-graphql/src/resolvers/block.ts index 19b0961cceb..3c7594568e3 100644 --- a/packages/rpc-graphql/src/resolvers/block.ts +++ b/packages/rpc-graphql/src/resolvers/block.ts @@ -6,7 +6,7 @@ import { BlockLoaderValue, cacheKeyFn } from '../loaders'; import { buildBlockLoaderArgSetFromResolveInfo, onlyFieldsRequested } from './resolve-info'; import { mapJsonParsedInnerInstructions, mapJsonParsedInstructions, TransactionResult } from './transaction'; -type BlockResult = Partial & { +export type BlockResult = Partial & { slot: Slot; transactionResults?: { [i: number]: TransactionResult }; }; @@ -116,10 +116,3 @@ export const resolveBlock = (fieldName?: string) => { return null; }; }; - -export const blockResolvers = { - Block: { - transactions: (parent?: BlockResult) => - parent?.transactionResults ? Object.values(parent.transactionResults) : null, - }, -}; diff --git a/packages/rpc-graphql/src/resolvers/index.ts b/packages/rpc-graphql/src/resolvers/index.ts deleted file mode 100644 index a96ca227557..00000000000 --- a/packages/rpc-graphql/src/resolvers/index.ts +++ /dev/null @@ -1,19 +0,0 @@ -import type { makeExecutableSchema } from '@graphql-tools/schema'; - -import { accountResolvers } from './account'; -import { blockResolvers } from './block'; -import { instructionResolvers } from './instruction'; -import { rootResolvers } from './root'; -import { transactionResolvers } from './transaction'; -import { typeTypeResolvers } from './types'; - -export function createSolanaGraphQLResolvers(): Parameters[0]['resolvers'] { - return { - ...accountResolvers, - ...blockResolvers, - ...instructionResolvers, - ...rootResolvers, - ...transactionResolvers, - ...typeTypeResolvers, - }; -} diff --git a/packages/rpc-graphql/src/resolvers/root.ts b/packages/rpc-graphql/src/resolvers/root.ts deleted file mode 100644 index ce443532be8..00000000000 --- a/packages/rpc-graphql/src/resolvers/root.ts +++ /dev/null @@ -1,15 +0,0 @@ -import type { makeExecutableSchema } from '@graphql-tools/schema'; - -import { resolveAccount } from './account'; -import { resolveBlock } from './block'; -import { resolveProgramAccounts } from './program-accounts'; -import { resolveTransaction } from './transaction'; - -export const rootResolvers: Parameters[0]['resolvers'] = { - Query: { - account: resolveAccount(), - block: resolveBlock(), - programAccounts: resolveProgramAccounts(), - transaction: resolveTransaction(), - }, -}; diff --git a/packages/rpc-graphql/src/resolvers/transaction.ts b/packages/rpc-graphql/src/resolvers/transaction.ts index 2a34a4db079..3d3544c9c69 100644 --- a/packages/rpc-graphql/src/resolvers/transaction.ts +++ b/packages/rpc-graphql/src/resolvers/transaction.ts @@ -71,7 +71,7 @@ export function mapJsonParsedInnerInstructions( })); } -const resolveTransactionData = () => { +export const resolveTransactionData = () => { return ( parent: TransactionResult | null, args: { @@ -164,9 +164,3 @@ export function resolveTransaction(fieldName?: string) { return null; }; } - -export const transactionResolvers = { - Transaction: { - data: resolveTransactionData(), - }, -}; diff --git a/packages/rpc-graphql/src/schema/type-resolvers/account.ts b/packages/rpc-graphql/src/schema/type-resolvers/account.ts new file mode 100644 index 00000000000..234290f9a75 --- /dev/null +++ b/packages/rpc-graphql/src/schema/type-resolvers/account.ts @@ -0,0 +1,155 @@ +import { AccountResult, resolveAccount, resolveAccountData } from '../../resolvers/account'; + +export const accountTypeResolvers = { + Account: { + __resolveType: (accountResult: AccountResult) => { + const { jsonParsedConfigs } = accountResult; + if (jsonParsedConfigs) { + if (jsonParsedConfigs.programName === 'nonce') { + return 'NonceAccount'; + } + if (jsonParsedConfigs.accountType === 'mint' && jsonParsedConfigs.programName === 'spl-token') { + return 'MintAccount'; + } + if (jsonParsedConfigs.accountType === 'account' && jsonParsedConfigs.programName === 'spl-token') { + return 'TokenAccount'; + } + if (jsonParsedConfigs.programName === 'stake') { + return 'StakeAccount'; + } + if (jsonParsedConfigs.accountType === 'vote' && jsonParsedConfigs.programName === 'vote') { + return 'VoteAccount'; + } + if ( + jsonParsedConfigs.accountType === 'lookupTable' && + jsonParsedConfigs.programName === 'address-lookup-table' + ) { + return 'LookupTableAccount'; + } + if (jsonParsedConfigs.programName === 'sysvar') { + if (jsonParsedConfigs.accountType === 'clock') { + return 'SysvarClockAccount'; + } + if (jsonParsedConfigs.accountType === 'epochRewards') { + return 'SysvarEpochRewardsAccount'; + } + if (jsonParsedConfigs.accountType === 'epochSchedule') { + return 'SysvarEpochScheduleAccount'; + } + if (jsonParsedConfigs.accountType === 'fees') { + return 'SysvarFeesAccount'; + } + if (jsonParsedConfigs.accountType === 'lastRestartSlot') { + return 'SysvarLastRestartSlotAccount'; + } + if (jsonParsedConfigs.accountType === 'recentBlockhashes') { + return 'SysvarRecentBlockhashesAccount'; + } + if (jsonParsedConfigs.accountType === 'rent') { + return 'SysvarRentAccount'; + } + if (jsonParsedConfigs.accountType === 'slotHashes') { + return 'SysvarSlotHashesAccount'; + } + if (jsonParsedConfigs.accountType === 'slotHistory') { + return 'SysvarSlotHistoryAccount'; + } + if (jsonParsedConfigs.accountType === 'stakeHistory') { + return 'SysvarStakeHistoryAccount'; + } + } + } + return 'GenericAccount'; + }, + data: resolveAccountData(), + }, + GenericAccount: { + data: resolveAccountData(), + ownerProgram: resolveAccount('ownerProgram'), + }, + LookupTableAccount: { + authority: resolveAccount('authority'), + data: resolveAccountData(), + ownerProgram: resolveAccount('ownerProgram'), + }, + MintAccount: { + data: resolveAccountData(), + freezeAuthority: resolveAccount('freezeAuthority'), + mintAuthority: resolveAccount('mintAuthority'), + ownerProgram: resolveAccount('ownerProgram'), + }, + NonceAccount: { + authority: resolveAccount('authority'), + data: resolveAccountData(), + ownerProgram: resolveAccount('ownerProgram'), + }, + StakeAccount: { + data: resolveAccountData(), + ownerProgram: resolveAccount('ownerProgram'), + }, + StakeAccountDataMetaAuthorized: { + staker: resolveAccount('staker'), + withdrawer: resolveAccount('withdrawer'), + }, + StakeAccountDataMetaLockup: { + custodian: resolveAccount('custodian'), + }, + StakeAccountDataStakeDelegation: { + voter: resolveAccount('voter'), + }, + SysvarClockAccount: { + data: resolveAccountData(), + ownerProgram: resolveAccount('ownerProgram'), + }, + SysvarEpochRewardsAccount: { + data: resolveAccountData(), + ownerProgram: resolveAccount('ownerProgram'), + }, + SysvarEpochScheduleAccount: { + data: resolveAccountData(), + ownerProgram: resolveAccount('ownerProgram'), + }, + SysvarFeesAccount: { + data: resolveAccountData(), + ownerProgram: resolveAccount('ownerProgram'), + }, + SysvarLastRestartSlotAccount: { + data: resolveAccountData(), + ownerProgram: resolveAccount('ownerProgram'), + }, + SysvarRecentBlockhashesAccount: { + data: resolveAccountData(), + ownerProgram: resolveAccount('ownerProgram'), + }, + SysvarRentAccount: { + data: resolveAccountData(), + ownerProgram: resolveAccount('ownerProgram'), + }, + SysvarSlotHashesAccount: { + data: resolveAccountData(), + ownerProgram: resolveAccount('ownerProgram'), + }, + SysvarSlotHistoryAccount: { + data: resolveAccountData(), + ownerProgram: resolveAccount('ownerProgram'), + }, + SysvarStakeHistoryAccount: { + data: resolveAccountData(), + ownerProgram: resolveAccount('ownerProgram'), + }, + TokenAccount: { + data: resolveAccountData(), + mint: resolveAccount('mint'), + owner: resolveAccount('owner'), + ownerProgram: resolveAccount('ownerProgram'), + }, + VoteAccount: { + authorizedWithdrawer: resolveAccount('authorizedWithdrawer'), + data: resolveAccountData(), + node: resolveAccount('nodePubkey'), + ownerProgram: resolveAccount('ownerProgram'), + }, + VoteAccountDataAuthorizedVoter: { + authorizedVoter: resolveAccount('authorizedVoter'), + }, +}; diff --git a/packages/rpc-graphql/src/schema/type-resolvers/block.ts b/packages/rpc-graphql/src/schema/type-resolvers/block.ts new file mode 100644 index 00000000000..932f49891c9 --- /dev/null +++ b/packages/rpc-graphql/src/schema/type-resolvers/block.ts @@ -0,0 +1,8 @@ +import { BlockResult } from '../../resolvers/block'; + +export const blockTypeResolvers = { + Block: { + transactions: (parent?: BlockResult) => + parent?.transactionResults ? Object.values(parent.transactionResults) : null, + }, +}; diff --git a/packages/rpc-graphql/src/schema/type-resolvers/index.ts b/packages/rpc-graphql/src/schema/type-resolvers/index.ts new file mode 100644 index 00000000000..298ad7cc30d --- /dev/null +++ b/packages/rpc-graphql/src/schema/type-resolvers/index.ts @@ -0,0 +1,19 @@ +import type { makeExecutableSchema } from '@graphql-tools/schema'; + +import { accountTypeResolvers } from './account'; +import { blockTypeResolvers } from './block'; +import { instructionTypeResolvers } from './instruction'; +import { rootTypeResolvers } from './root'; +import { transactionTypeResolvers } from './transaction'; +import { typeTypeResolvers } from './types'; + +export function createSolanaGraphQLTypeResolvers(): Parameters[0]['resolvers'] { + return { + ...accountTypeResolvers, + ...blockTypeResolvers, + ...instructionTypeResolvers, + ...rootTypeResolvers, + ...transactionTypeResolvers, + ...typeTypeResolvers, + }; +} diff --git a/packages/rpc-graphql/src/resolvers/instruction.ts b/packages/rpc-graphql/src/schema/type-resolvers/instruction.ts similarity index 99% rename from packages/rpc-graphql/src/resolvers/instruction.ts rename to packages/rpc-graphql/src/schema/type-resolvers/instruction.ts index 604c08e5441..40aa033e0c7 100644 --- a/packages/rpc-graphql/src/resolvers/instruction.ts +++ b/packages/rpc-graphql/src/schema/type-resolvers/instruction.ts @@ -1,7 +1,7 @@ -import { resolveAccount } from './account'; -import { InstructionResult } from './transaction'; +import { resolveAccount } from '../../resolvers/account'; +import { InstructionResult } from '../../resolvers/transaction'; -export const instructionResolvers = { +export const instructionTypeResolvers = { AdvanceNonceAccountInstruction: { nonceAccount: resolveAccount('nonceAccount'), nonceAuthority: resolveAccount('nonceAuthority'), diff --git a/packages/rpc-graphql/src/schema/type-resolvers/root.ts b/packages/rpc-graphql/src/schema/type-resolvers/root.ts new file mode 100644 index 00000000000..9c82fd34ba7 --- /dev/null +++ b/packages/rpc-graphql/src/schema/type-resolvers/root.ts @@ -0,0 +1,15 @@ +import type { makeExecutableSchema } from '@graphql-tools/schema'; + +import { resolveAccount } from '../../resolvers/account'; +import { resolveBlock } from '../../resolvers/block'; +import { resolveProgramAccounts } from '../../resolvers/program-accounts'; +import { resolveTransaction } from '../../resolvers/transaction'; + +export const rootTypeResolvers: Parameters[0]['resolvers'] = { + Query: { + account: resolveAccount(), + block: resolveBlock(), + programAccounts: resolveProgramAccounts(), + transaction: resolveTransaction(), + }, +}; diff --git a/packages/rpc-graphql/src/schema/type-resolvers/transaction.ts b/packages/rpc-graphql/src/schema/type-resolvers/transaction.ts new file mode 100644 index 00000000000..255c0eb7842 --- /dev/null +++ b/packages/rpc-graphql/src/schema/type-resolvers/transaction.ts @@ -0,0 +1,7 @@ +import { resolveTransactionData } from '../../resolvers/transaction'; + +export const transactionTypeResolvers = { + Transaction: { + data: resolveTransactionData(), + }, +}; diff --git a/packages/rpc-graphql/src/resolvers/types.ts b/packages/rpc-graphql/src/schema/type-resolvers/types.ts similarity index 96% rename from packages/rpc-graphql/src/resolvers/types.ts rename to packages/rpc-graphql/src/schema/type-resolvers/types.ts index beaf0e143fc..41bcc3b645f 100644 --- a/packages/rpc-graphql/src/resolvers/types.ts +++ b/packages/rpc-graphql/src/schema/type-resolvers/types.ts @@ -1,6 +1,6 @@ import { Kind } from 'graphql'; -import { resolveAccount } from './account'; +import { resolveAccount } from '../../resolvers/account'; const stringScalarAlias = { __parseLiteral(ast: { kind: Kind; value: bigint | boolean | number | string }): string | null {