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.

`mutate` function leaves `data` and `variables` un-typed.

See original GitHub issue

Intended outcome: apollo-client uses strong typing. This issue is specifically addressing mutate’s variables and data.

Actual outcome: The results of mutate are untyped, as are the variables in the options.

How to reproduce the issue: While this issue covers multiple projects’ types, the problem is exposed by usage of apollo-client mutate.

apollo-client.mutate is virtually un-typed (e.g. data and variables in typescript). The types that need parameterized adjustments cross apollo-client, apollo-link, and graphql projects. Specifically the data result and variables are unsealed/unchecked and need to be cast if the user wants to use them after fetching.

  • apollo-client mutate:
mutate<T>(options: MutationOptions<T>): Promise<FetchResult<T>>
  • apollo-link FetchResult:
export declare type FetchResult<C = Record<string, any>, E = Record<string, any>> = ExecutionResult & {
    extensions?: E;
    context?: C;
};
  • graphql ExecutionResult:
export interface ExecutionResult {
    data?: { [key: string]: any };
    extensions?: { [key: string]: any };
    errors?: GraphQLError[];
}

Summary

  1. data - The mutate<T> from apollo-client doesn’t type the result, it types FetchResult<T> position 1, which is the context. The result is in ExecutionResult which is entirely unsealed/untyped data?: { [key: string]: any }
  2. variables - MutationOptions<T> doesn’t allow for typing of variables - they are explicitly any
export interface MutationBaseOptions<T = {
    [key: string]: any;
}> {
    optimisticResponse?: Object | Function;
    updateQueries?: MutationQueryReducersMap<T>;
    refetchQueries?: ((result: ExecutionResult) => RefetchQueryDescription) | RefetchQueryDescription;
    update?: MutationUpdaterFn<T>;
    errorPolicy?: ErrorPolicy;
    variables?: any;
}
export interface MutationOptions<T = {
    [key: string]: any;
}> extends MutationBaseOptions<T> {
    mutation: DocumentNode;
    context?: any;
}
  1. I suggest any parameterized type named T be renamed for clarity to expose issues such as this.

Version

  • apollo-client@2.0.4

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:17
  • Comments:21 (10 by maintainers)

github_iconTop GitHub Comments

5reactions
rosskevincommented, Sep 24, 2018

I have tested the PR locally via augmentation with:

// https://github.com/apollographql/apollo-link/pull/804
// https://github.com/apollographql/apollo-client/issues/2795#issuecomment-424124181

import { ExecutionResult } from 'graphql'

declare module 'apollo-link' {
  export type FetchResult<
    TData = { [key: string]: any },
    C = Record<string, any>,
    E = Record<string, any>
  > = ExecutionResult<TData> & {
    extensions?: E
    context?: C
  }
}

It seems to be working out of the box since apollo-client was trying to pass through the the TData as position 1, albeit errantly as it was applied to C. By adding the above in the PR, it-just-works. Hopefully the apollo-link PR is merged/released.

3reactions
alexbjorligcommented, Mar 28, 2019

Our team is having issues with this bug, no typings on mutation. We are using apollo-link 1.2.11. Created a stackblitz to illustrate the issue: https://stackblitz.com/edit/apollo-no-typings-on-mutation

@rosskevin are we not expecting typings to work here?

Screenshot 2019-03-28 at 10 21 36

Read more comments on GitHub >

github_iconTop Results From Across the Web

variables lose their labels after mutate() function is applied in R
When I apply mutate() functions on any column, that column loses its label. library(dplyr) df2 <- df1 %>% mutate_if(is.character ...
Read more >
Create, modify, and delete columns — mutate • dplyr
mutate () adds new variables and preserves existing ones; transmute() adds new variables and drops existing ones. New variables overwrite existing variables ......
Read more >
Understanding type annotation in Python - LogRocket Blog
With type hints, you can annotate variables and functions with datatypes. Tools like mypy, pyright, pytypes, or pyre perform the functions ...
Read more >
Real world advice for writing maintainable Go programs
Functions or methods that begin with Write traditonally take non string values and write them out as binary data. Collection variables, maps ...
Read more >
How to Use Mutate function in R - R-bloggers
The dplyr library has the following functions that can be used to add additional variables to a data frame. mutate() – adds new...
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