Skip to content

Commit

Permalink
refactor(experimental): graphql: group schema type resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed May 6, 2024
1 parent 566c7cb commit e14dfd1
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 212 deletions.
4 changes: 2 additions & 2 deletions packages/rpc-graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -21,7 +21,7 @@ export function createRpcGraphQL(
maxMultipleAccountsBatchSize: config?.maxMultipleAccountsBatchSize ?? 100,
};
const schema = makeExecutableSchema({
resolvers: createSolanaGraphQLResolvers(),
resolvers: createSolanaGraphQLTypeResolvers(),
typeDefs: createSolanaGraphQLTypeDefs(),
});
return {
Expand Down
158 changes: 1 addition & 157 deletions packages/rpc-graphql/src/resolvers/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type AccountResult = Partial<Omit<AccountLoaderValue, 'data'>> & {
ownerProgram?: Address;
};

const resolveAccountData = () => {
export const resolveAccountData = () => {
return (
parent: AccountResult | null,
args: {
Expand Down Expand Up @@ -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'),
},
};
9 changes: 1 addition & 8 deletions packages/rpc-graphql/src/resolvers/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { BlockLoaderValue, cacheKeyFn } from '../loaders';
import { buildBlockLoaderArgSetFromResolveInfo, onlyFieldsRequested } from './resolve-info';
import { mapJsonParsedInnerInstructions, mapJsonParsedInstructions, TransactionResult } from './transaction';

type BlockResult = Partial<BlockLoaderValue> & {
export type BlockResult = Partial<BlockLoaderValue> & {
slot: Slot;
transactionResults?: { [i: number]: TransactionResult };
};
Expand Down Expand Up @@ -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,
},
};
19 changes: 0 additions & 19 deletions packages/rpc-graphql/src/resolvers/index.ts

This file was deleted.

15 changes: 0 additions & 15 deletions packages/rpc-graphql/src/resolvers/root.ts

This file was deleted.

8 changes: 1 addition & 7 deletions packages/rpc-graphql/src/resolvers/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function mapJsonParsedInnerInstructions(
}));
}

const resolveTransactionData = () => {
export const resolveTransactionData = () => {
return (
parent: TransactionResult | null,
args: {
Expand Down Expand Up @@ -164,9 +164,3 @@ export function resolveTransaction(fieldName?: string) {
return null;
};
}

export const transactionResolvers = {
Transaction: {
data: resolveTransactionData(),
},
};

0 comments on commit e14dfd1

Please sign in to comment.