Make GraphQLResponse generic for e.g. type-safe integration tests
See original GitHub issueI’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:
export interface GraphQLResponse<T = any> {
data?: Record<string, T> | null;
...
Then integration tests with e.g. Axios could be written in TypeScript as follows:
const response: AxiosResponse<GraphQLResponse<MyRecord> = await axios.post(url, requestBody, config);
const myRecord = response.data.data; // Type MyRecord
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:9 (6 by maintainers)
Top Results From Across the Web
TypeScript with Apollo Client - Apollo GraphQL Docs
This type has the same JavaScript representation but enables APIs to infer the data and variable types (instead of making you specify types...
Read more >Java GraphQL Client - DGS Framework - Netflix Open Source
Instead of using a query String, you can use code generation to create a type-safe query builder API. The GraphQLResponse provides methods to...
Read more >GraphQL Code Libraries, Tools and Services
An extensible GraphQL client with modules for react, caching, request parsing, web workers, websockets and more... The example below installs and initializes ...
Read more >Type-safe API mocking with Mock Service Worker and ...
Tagged with testing, typescript, api, mocking. ... The same generics apply to any rest request handler: rest.get() , rest.post() ...
Read more >Resolvers | NestJS - A progressive Node.js framework
You can ensure type safety by combining inheritance and TypeScript generics. For example, to create a base class with a generic findAll query, ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
graphql-code-generator
.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
as X
in your code.It does appear that changing
any
tounknown
in thedata
andextensions
field definitions is a change that typechecks at least in the context of the apollo-server repo. I think that might be a more accurate type.I also think the change you suggested doesn’t quite make sense. Different keys under
data
will have different types. Did you mean something more like: