Skip to content

Commit

Permalink
feat(experimental): create an RPC GraphQL (#1660)
Browse files Browse the repository at this point in the history
* refactor(experimental): init rpc-graphql package

* refactor(experimental): create an RPC GraphQL
  • Loading branch information
buffalojoec authored Oct 6, 2023
1 parent 4189b37 commit 4366e2e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/rpc-graphql/src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { SolanaRpcMethods } from '@solana/rpc-core';
import { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';

export interface RpcGraphQLContext {
rpc: Rpc<SolanaRpcMethods>;
}

export function createSolanaGraphQLContext(rpc: Rpc<SolanaRpcMethods>): RpcGraphQLContext {
return { rpc };
}
1 change: 1 addition & 0 deletions packages/rpc-graphql/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './rpc';
39 changes: 39 additions & 0 deletions packages/rpc-graphql/src/rpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { SolanaRpcMethods } from '@solana/rpc-core';
import { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import { graphql, GraphQLObjectType, GraphQLSchema, Source } from 'graphql';

import { createSolanaGraphQLContext, RpcGraphQLContext } from './context';

export interface RpcGraphQL {
context: RpcGraphQLContext;
query(
source: string | Source,
variableValues?: { readonly [variable: string]: unknown }
): ReturnType<typeof graphql>;
schema: GraphQLSchema;
}

export function createRpcGraphQL(rpc: Rpc<SolanaRpcMethods>): RpcGraphQL {
const context = createSolanaGraphQLContext(rpc);
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
fields: {
/** Root queries */
},
name: 'RootQuery',
}),
types: [],
});
return {
context,
async query(source: string | Source, variableValues?: { readonly [variable: string]: unknown }) {
return graphql({
contextValue: this.context,
schema: this.schema,
source,
variableValues,
});
},
schema,
};
}

0 comments on commit 4366e2e

Please sign in to comment.