-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Make GraphQLResponse generic for e.g. type-safe integration tests #5464
Comments
I think in practice folks will want to use a code generator such as GraphQL Code Generator or (not actively maintained) Apollo Codegen to generate response types rather than relying on GraphQLResponse. GraphQLResponse is more designed for writing a server that has to handle any GraphQL operation rather than use with specific operations. It's also only sorta typesafe, in the sense that you're just basically asserting at compile time that the data you got back matches MyRecord without any runtime checks, right? So it's not much better than just doing an It does appear that changing I also think the change you suggested doesn't quite make sense. Different keys under export interface GraphQLResponse<Data extends Record<string, unknown> = Record<string, unknown>> {
data?: Data | null; |
@glasser Yes, your code fragment is actually what I was looking for. |
Hmm. So one problem with this is that all the uses of GraphQLResponse in the ApolloServer API would either still be hardcoded to the default I think this is a reasonable idea and would be interested in PRs implementing it. Probably we would want to leave the default using |
For testing it would be nice if export interface GraphQLResponse<TData = any> {
data?: TData | null;
errors?: ReadonlyArray<GraphQLFormattedError>;
extensions?: Record<string, any>;
http?: Pick<Response, 'headers'> & Partial<Pick<Mutable<Response>, 'status'>>;
}
async executeOperation<TData = any, TVariables = Record<string, any>>(
request: Omit<GraphQLRequest, 'query'> & {
query?: string | DocumentNode | TypedDocumentNode<TData, TVariables>;
},
integrationContextArgument?: ContextFunctionParams,
): Promise<GraphQLResponse<TData>> The typed document node is the direction the graphql code generator people are pushing, so supporting this would be awesome. |
Yes, I think in general it would be good to move in the direction with aligning the types in Apollo Server (both for operations and resolvers) with things that can be generated by |
Any update on this? Feels like this is a regression from |
As mentioned above I'd be interested in PRs that implement this. |
Resolved via #6960 |
I'd recommend to make
GraphQLResponse
generic so that e.g. integration tests will be type safe, i.e. at https://github.com/apollographql/apollo-server/blob/main/packages/apollo-server-types/src/index.ts#L102:Then integration tests with e.g. Axios could be written in TypeScript as follows:
The text was updated successfully, but these errors were encountered: