question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make GraphQLResponse generic for e.g. type-safe integration tests

See original GitHub issue

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:

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:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
glassercommented, Aug 23, 2021

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.

1reaction
glassercommented, Jul 13, 2021

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 to unknown in the data and extensions 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:

export interface GraphQLResponse<Data extends Record<string, unknown> = Record<string, unknown>> {
  data?: Data | null;
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found