Relay-compiler-language-typescript: [experimental] No type-safe optimisticResponse when using useMutation

Created on 19 May 2020  路  17Comments  路  Source: relay-tools/relay-compiler-language-typescript

Versions in package.json

{
 "relay-compiler-language-typescript": "^12.0.2",
 "react": "^0.0.0-experimental-33c3af284",
 "react-dom": "^0.0.0-experimental-33c3af284",
 "react-relay": "^0.0.0-experimental-895a6fe0"
}

I'm creating a mutation function like this:

const [createGalleryItem, isCreating] = useMutation<
    MediaTabUploadMutation
  >(graphql`
    mutation MediaTabUploadMutation($input: GalleryItemCreateInput!) {
      createGalleryItem(input: $input) {
        galleryItem {
          ...MediaGalleryItem_galleryItem
        }
      }
    }
  `)

The mutation config typescript definition excepts something called rawResponse (I think the full response without masked fragments)

@types/react-relay/lib/relay-experimental/useMutation.d.ts

import { GraphQLTaggedNode, Disposable, MutationConfig, MutationParameters, IEnvironment, PayloadError, DeclarativeMutationConfig, SelectorStoreUpdater, UploadableMap } from "relay-runtime";

export interface UseMutationConfig<TMutation extends MutationParameters> {
    configs?: DeclarativeMutationConfig[];
    onError?: (error: Error) => void;
    onCompleted?: (
        response: TMutation["response"],
        errors: PayloadError[],
    ) => void;
    onUnsubscribe?: () => void;
    optimisticResponse?: TMutation["rawResponse"];
    optimisticUpdater?: SelectorStoreUpdater;
    updater?: SelectorStoreUpdater;
    uploadables?: UploadableMap;
    variables: TMutation["variables"];
}

// tslint:disable-next-line no-unnecessary-generics
export function useMutation<TMutation extends MutationParameters>(
    mutation: GraphQLTaggedNode,
    commitMutationFn?: (environment: IEnvironment, config: MutationConfig<TMutation>) => Disposable,
): [(config: UseMutationConfig<TMutation>) => Disposable, boolean];

But the MediaTabUploadMutation does not contain a rawResponse

import { ConcreteRequest } from "relay-runtime";
import { FragmentRefs } from "relay-runtime";
export type GalleryItemCreateInput = {
    title: string;
    file: unknown;
};
export type MediaTabUploadMutationVariables = {
    input: GalleryItemCreateInput;
};
export type MediaTabUploadMutationResponse = {
    readonly createGalleryItem: {
        readonly galleryItem: {
            readonly " $fragmentRefs": FragmentRefs<"MediaGalleryItem_galleryItem">;
        };
    };
};
export type MediaTabUploadMutation = {
    readonly response: MediaTabUploadMutationResponse;
    readonly variables: MediaTabUploadMutationVariables;
};

So my optimistic response it not type-safe a.t.m. I'm not really sure it belongs here but I think it does.

Most helpful comment

it is explicit in the docs https://relay.dev/docs/en/a-guided-tour-of-relay#optimistic-updates

that we need to use @raw_response_type when handling optimistic updates

thanks

All 17 comments

does Flow type emission has rawResponse?

Looking into it

why is not being generated?

I see the test are removed here: https://github.com/relay-tools/relay-compiler-language-typescript/commit/61992cf68ef645edc58dd8d7f4ff1a38e33e6906#diff-152db63141b41dd84d03a80fda984dcc

Maybe Eloy knows more about this change @alloy

Maybe after dependency update I'll look to see some changes in these libraries

I think we need to bring this back, and reimplement @match and @module

@renanmav or @thicodes could help

https://github.com/leebyron/iterall/releases
--> Maybe something with this probably not
AsyncIterators now include throw() and return() methods (#48)

I'll go back in versions to see if it worked in previous versions

Can not go back further as I get issues with my current codebase and I don't know where query is wrong

Complex argument values (Lists or InputObjects with nested variables) are not supported.

I鈥檓 unsure how @match/@module are related to @raw_response, they were never implemented, as I didn鈥檛 use them and didn鈥檛 have a good code-base to base them off of. @raw_response definitely works, though, but you need to add the directive to your operation.

Ok thanks! I'll try that this evening and close this issue 馃憤

it is explicit in the docs https://relay.dev/docs/en/a-guided-tour-of-relay#optimistic-updates

that we need to use @raw_response_type when handling optimistic updates

thanks

Was this page helpful?
0 / 5 - 0 ratings