Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Data and Variable types to executer operation #6384

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/server-types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,19 @@ export interface GraphQLRequest {

export type VariableValues = { [name: string]: any };

// Its not possible for this to be Record<string, any> without casting either the
// result of executeOperation in ApolloServer.ts or the result of sendResponse and
// sendErrorResponse in requestPipeline.ts.
// By leaving this open as the wider object type it fulfills what a user can pass
// in which is an extension of Record<string, any>.
type GraphQLResponseData = Record<string | number | symbol, any>;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be changed to Record<string, any> as outlined in the comment by casting further into the call stack or at the end of the call stack.

I prefer not to cast so this works for the use case happy to update if you feel otherwise


// TODO(AS4): does this differ in an interesting way from GraphQLExecutionResult
// and graphql-js ExecutionResult? It does have `http` but perhaps this can be an
// "extends". Ah, the difference is about formatted vs throwable errors? Let's
// make sure we at least understand it.
export interface GraphQLResponse {
data?: Record<string, any> | null;
export interface GraphQLResponse<TData = GraphQLResponseData> {
data?: TData | null;
errors?: ReadonlyArray<GraphQLFormattedError>;
extensions?: Record<string, any>;
// TODO(AS4): Seriously consider whether this type makes sense at all or whether
Expand Down
25 changes: 19 additions & 6 deletions packages/server/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1040,25 +1040,38 @@ export class ApolloServer<TContext extends BaseContext = BaseContext> {
* The second object will be the `contextValue` object available in resolvers.
*/
// TODO(AS4): document this
public async executeOperation(
public async executeOperation<
TData = Record<string, any>,
TVariables = Record<string, any>,
>(
this: ApolloServer<BaseContext>,
request: Omit<GraphQLRequest, 'query'> & {
query?: string | DocumentNode;
variables?: TVariables;
},
): Promise<GraphQLResponse>;
public async executeOperation(
): Promise<GraphQLResponse<TData>>;

public async executeOperation<
TData = Record<string, any>,
TVariables = Record<string, any>,
>(
request: Omit<GraphQLRequest, 'query'> & {
query?: string | DocumentNode;
variables?: TVariables;
},
contextValue: TContext,
): Promise<GraphQLResponse>;
): Promise<GraphQLResponse<TData>>;

async executeOperation(
async executeOperation<
TData = Record<string, any>,
TVariables = Record<string, any>,
>(
request: Omit<GraphQLRequest, 'query'> & {
query?: string | DocumentNode;
variables?: TVariables;
},
contextValue?: TContext,
): Promise<GraphQLResponse> {
): Promise<GraphQLResponse<TData>> {
// Since this function is mostly for testing, you don't need to explicitly
// start your server before calling it. (That also means you can use it with
// `apollo-server` which doesn't support `start()`.)
Expand Down