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

ctx.data() and ctx.error() types are incompatible with graphql package ExecutionResult result type #522

Closed
ericbiewener opened this issue Dec 26, 2020 · 2 comments · Fixed by #542
Assignees
Labels
bug Something isn't working scope:typescript

Comments

@ericbiewener
Copy link

Describe the bug

Passing the result of an executed graphql query to ctx.data() or ctx.errors() results in a TypeScript error.

Environment

  • msw: 0.24.2
  • nodejs: 14.11.2
  • graphql: 15.3.0
  • typescript: 4.1.2

To Reproduce

import { graphql as mswGraphql } from "msw";
import { graphql } from "graphql";
import { buildSchema } from "graphql/utilities";

mswGraphql.query("Foo", async (req, res, ctx) => {
  const rsp = await graphql(
    buildSchema(""),
    req.body?.query || "",
    {},
    req.variables
  );

  // Typescript errors thrown on both ctx.data() and ctx.errors()
  return res(ctx.data(rsp.data), ctx.errors(rsp.errors));
});

The return value of await graphql() is this ExecutionResult type, and that is incompatible with the types of ctx.data and ctx.error

ctx.data() error:

Argument of type '{ [key: string]: any; } | null | undefined' is not assignable to parameter of type 'Record<string, any>'.
  Type 'undefined' is not assignable to type 'Record<string, any>'.

ctx.errors() error:

Argument of type 'readonly GraphQLError[] | undefined' is not assignable to parameter of type 'Partial<GraphQLError>[] | null | undefined'.
  The type 'readonly GraphQLError[]' is 'readonly' and cannot be assigned to the mutable type 'Partial<GraphQLError>[]'.

Expected behavior

I would expect MSW's types to be compatible with graphql().

While simply fixing the types on those two methods would be sufficient, perhaps it would be worth introducing an additional ctx method that takes the return value of graphql() directly, e.g.

res(ctx.graphql(rsp))

This would allow it to handle any data/type transformations that may be required for compatibility with ctx.data/error(), while also adding a convenient API for users.

@ericbiewener ericbiewener added the bug Something isn't working label Dec 26, 2020
@kettanaito kettanaito added this to the GraphQL support milestone Jan 3, 2021
@kettanaito
Copy link
Member

kettanaito commented Jan 12, 2021

Hey, @ericbiewener. Thanks for reporting this.

Would this issue be solved if ctx.data could accept undefined as a value? You can try that be editing the node_modules/msw/lib/types/context/data.dts and changing its intput type:

import { ResponseTransformer } from '../response';
/**
 * Sets a given payload as a GraphQL response body.
 * @example
 * res(ctx.data({ user: { firstName: 'John' }}))
 * @see {@link https://mswjs.io/docs/api/context/data `ctx.data()`}
 */
-export declare const data: <T extends Record<string, any>>(payload: T) => ResponseTransformer;
+export declare const data: <T extends Record<string, any>>(payload: T | undefined) => ResponseTransformer;

You may need to also add null to the union type, as ExecutionResult can be null | undefined.

Could you let us know if this would indeed help?

@ericbiewener
Copy link
Author

This does solve ctx.data(), but not ctx.errors().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working scope:typescript
Projects
None yet
2 participants