@apollo/client#MutationHookOptions TypeScript Examples

The following examples show how to use @apollo/client#MutationHookOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: channel.ts    From atlas with GNU General Public License v3.0 6 votes vote down vote up
useUnfollowChannel = (opts?: MutationHookOptions<UnfollowChannelMutation>) => {
  const [unfollowChannel, rest] = useUnfollowChannelMutation()
  return {
    unfollowChannel: (id: string) =>
      unfollowChannel({
        ...opts,
        variables: {
          channelId: id,
        },
        update: (cache, mutationResult) => {
          cache.modify({
            id: cache.identify({
              __typename: 'Channel',
              id,
            }),
            fields: {
              follows: () => mutationResult.data?.unfollowChannel.follows,
            },
          })
        },
      }),
    ...rest,
  }
}
Example #2
Source File: form.create.mutation.ts    From ui with GNU Affero General Public License v3.0 6 votes vote down vote up
useFormCreateMutation = (
  options: MutationHookOptions<Data, Variables> = {}
): MutationTuple<Data, Variables> => {
  const oldUpdate = options.update

  options.update = (cache, result, options) => {
    cache.evict({
      fieldName: 'listForms',
    })
    cache.gc()

    if (oldUpdate) {
      oldUpdate(cache, result, options)
    }
  }

  return useMutation<Data, Variables>(MUTATION, options)
}
Example #3
Source File: useMutation.ts    From lexicon with MIT License 6 votes vote down vote up
export function useMutation<TData, TVariables = OperationVariables>(
  query: DocumentNode,
  options?: MutationHookOptions<TData, TVariables>,
  errorAlert: ErrorAlertOptionType = 'SHOW_ALERT',
): MutationTuple<TData, TVariables> {
  const { navigate } = useNavigation<TabNavProp<'Home'>>();

  const onErrorDefault = (error: ApolloError) => {
    errorHandlerAlert(error, navigate);
  };

  const { ...others } = options ?? {};

  const {
    onError = errorAlert === 'SHOW_ALERT' ? onErrorDefault : undefined,
    ...otherOptions
  } = others;

  let [mutationFunction, mutationResult] = useMutationBase<TData, TVariables>(
    query,
    {
      onError,
      ...otherOptions,
    },
  );

  return [mutationFunction, mutationResult];
}
Example #4
Source File: useUpload.ts    From lexicon with MIT License 6 votes vote down vote up
export function useStatefulUpload(
  imagesArray: Array<Image>,
  currentToken: number,
  options?: MutationHookOptions<UploadType, UploadVariables>,
) {
  const [completedToken, setCompletedToken] = useState(1);
  const [tempArray, setTempArray] = useState<Array<Image>>(imagesArray);
  let newArray = imagesArray;

  let [upload] = useMutation<UploadType, UploadVariables>(UPLOAD, {
    ...options,
    onCompleted: ({ upload: result }) => {
      const {
        originalFilename: name,
        width,
        height,
        shortUrl: url,
        token,
      } = result;
      if (token) {
        const imageUrl = formatImageLink(name, width, height, url);
        newArray[token - 1] = { link: imageUrl, done: true };
        setTempArray(newArray);
        setCompletedToken(token);
      }
    },
    onError: (error) => {
      newArray[currentToken - 2] = { link: '', done: true };
      setTempArray(newArray);
      errorHandlerAlert(error);
    },
  });

  return { upload, tempArray, completedToken };
}
Example #5
Source File: useUpload.ts    From lexicon with MIT License 6 votes vote down vote up
export function useStatelessUpload(
  options?: MutationHookOptions<UploadType, UploadVariables>,
) {
  const [upload, { loading }] = useMutation<UploadType, UploadVariables>(
    UPLOAD,
    {
      ...options,
    },
  );
  return { upload, loading };
}
Example #6
Source File: useProfile.ts    From lexicon with MIT License 6 votes vote down vote up
export function useEditProfile(
  options?: MutationHookOptions<EditProfileType, EditProfileVariables>,
) {
  const [editProfile, { loading }] = useMutation<
    EditProfileType,
    EditProfileVariables
  >(EDIT_PROFILE, {
    ...options,
  });

  return { editProfile, loading };
}
Example #7
Source File: useNotification.ts    From lexicon with MIT License 6 votes vote down vote up
export function useMarkRead(
  options?: MutationHookOptions<MarkReadType, MarkReadVariables>,
) {
  const [markAsRead, { loading }] = useMutation<
    MarkReadType,
    MarkReadVariables
  >(MARK_READ, {
    ...options,
  });

  return { markAsRead, loading };
}
Example #8
Source File: useEmail.ts    From lexicon with MIT License 6 votes vote down vote up
export function useSetPrimaryEmail(
  options?: MutationHookOptions<SetPrimaryEmailType, SetPrimaryEmailVariables>,
) {
  const [setPrimaryEmail, { loading }] = useMutation<
    SetPrimaryEmailType,
    SetPrimaryEmailVariables
  >(SET_PRIMARY_EMAIL, {
    ...options,
  });

  return { setPrimaryEmail, loading };
}
Example #9
Source File: useEmail.ts    From lexicon with MIT License 6 votes vote down vote up
export function useDeleteEmail(
  options?: MutationHookOptions<DeleteEmailType, DeleteEmailVariables>,
) {
  const [deleteEmail, { loading }] = useMutation<
    DeleteEmailType,
    DeleteEmailVariables
  >(DELETE_EMAIL, {
    ...options,
  });

  return { deleteEmail, loading };
}
Example #10
Source File: useEmail.ts    From lexicon with MIT License 6 votes vote down vote up
export function useAddEmail(
  options?: MutationHookOptions<AddEmailAddressType, AddEmailAddressVariables>,
) {
  const [addEmailAddress, { loading, error }] = useMutation<
    AddEmailAddressType,
    AddEmailAddressVariables
  >(ADD_EMAIL_ADDRESS, {
    ...options,
  });

  return { addEmailAddress, loading, error };
}
Example #11
Source File: useChangePassword.ts    From lexicon with MIT License 6 votes vote down vote up
export function useChangePassword(
  options?: MutationHookOptions<
    ChangeNewPasswordType,
    ChangeNewPasswordVariables
  >,
) {
  const [changeNewPassword, { loading, error }] = useMutation<
    ChangeNewPasswordType,
    ChangeNewPasswordVariables
  >(CHANGE_PASSWORD, {
    ...options,
  });

  return { changeNewPassword, loading, error };
}
Example #12
Source File: useReplyTopic.ts    From lexicon with MIT License 6 votes vote down vote up
export function useReplyTopic(
  options?: MutationHookOptions<ReplyTopicType, ReplyTopicVariables>,
) {
  const [reply, { loading }] = useMutation<ReplyTopicType, ReplyTopicVariables>(
    REPLY_TOPIC,
    { ...options },
  );

  return { reply, loading };
}
Example #13
Source File: useReplyPost.ts    From lexicon with MIT License 6 votes vote down vote up
export function useReplyPost(
  options?: MutationHookOptions<ReplyPostType, ReplyPostVariables>,
) {
  const [reply, { loading }] = useMutation<ReplyPostType, ReplyPostVariables>(
    REPLY,
    { ...options },
  );

  return { reply, loading };
}
Example #14
Source File: useNewTopic.ts    From lexicon with MIT License 6 votes vote down vote up
export function useNewTopic(
  options?: MutationHookOptions<NewTopicType, NewTopicVariables>,
) {
  const [newTopic, { loading }] = useMutation<NewTopicType, NewTopicVariables>(
    NEW_TOPIC,
    { ...options },
  );

  return { newTopic, loading };
}
Example #15
Source File: channel.ts    From atlas with GNU General Public License v3.0 6 votes vote down vote up
useFollowChannel = (opts?: MutationHookOptions<FollowChannelMutation>) => {
  const [followChannel, rest] = useFollowChannelMutation()
  return {
    followChannel: (id: string) =>
      followChannel({
        ...opts,
        variables: {
          channelId: id,
        },
        update: (cache, mutationResult) => {
          cache.modify({
            id: cache.identify({
              __typename: 'Channel',
              id,
            }),
            fields: {
              follows: () => mutationResult.data?.followChannel.follows,
            },
          })
        },
      }),
    ...rest,
  }
}
Example #16
Source File: video.ts    From atlas with GNU General Public License v3.0 6 votes vote down vote up
useAddVideoView = (opts?: Omit<MutationHookOptions<AddVideoViewMutation>, 'variables'>) => {
  const [addVideoView, rest] = useAddVideoViewMutation({
    update: (cache, mutationResult) => {
      cache.modify({
        id: cache.identify({
          __typename: 'Video',
          id: mutationResult.data?.addVideoView.id,
        }),
        fields: {
          views: () => mutationResult.data?.addVideoView.views,
        },
      })
    },
    ...opts,
  })
  return {
    addVideoView,
    ...rest,
  }
}
Example #17
Source File: Apollo.ts    From graphql-ts-client with MIT License 6 votes vote down vote up
export function useTypedMutation<
    TData extends object,
    TVariables extends object,
    TContext = DefaultContext, 
	TCache extends ApolloCache<any> = ApolloCache<any>
>(
    fetcher: Fetcher<"Mutation", TData, TVariables>,
    options?: MutationHookOptions<TData, TVariables, TContext> & {
        readonly operationName?: string
	}
): MutationTuple<
    TData, 
    TVariables, 
    TContext, 
    TCache
> {
    const body = requestBody(fetcher);

    const request = useMemo<DocumentNode>(() => {
		const operationName = options?.operationName ?? `mutation_${util.toMd5(body)}`;
		return gql`mutation ${operationName}${body}`;
	}, [body, options?.operationName]);

	const response = useMutation<
		TData, 
		TVariables, 
		TContext, 
		TCache
	>(request, options);
	const responseData = response[1].data;
	const newResponseData = useMemo(() => util.exceptNullValues(responseData), [responseData]);
	return newResponseData === responseData ? response : [
        response[0],
        { ...response[1], data: newResponseData }
    ];
}
Example #18
Source File: useLogin.ts    From lexicon with MIT License 6 votes vote down vote up
export function useLogin(
  options?: MutationHookOptions<LoginType, LoginVariables>,
) {
  const [login, { loading, error }] = useMutation<LoginType, LoginVariables>(
    LOGIN,
    {
      ...options,
    },
  );

  return { login, loading, error };
}
Example #19
Source File: useLogout.ts    From lexicon with MIT License 6 votes vote down vote up
export function useLogout(
  options?: MutationHookOptions<LogoutType, LogoutVariables>,
) {
  const [logout, { loading }] = useMutation<LogoutType, LogoutVariables>(
    LOGOUT,
    { ...options },
  );

  return { logout, loading };
}
Example #20
Source File: useRegister.ts    From lexicon with MIT License 6 votes vote down vote up
export function useRegister(
  options?: MutationHookOptions<RegisterType, RegisterVariables>,
) {
  const [register, { loading }] = useMutation<RegisterType, RegisterVariables>(
    REGISTER,
    { ...options },
  );

  return { register, loading };
}
Example #21
Source File: useEditPost.ts    From lexicon with MIT License 6 votes vote down vote up
export function useEditPost(
  options?: MutationHookOptions<EditPostType, EditPostVariables>,
) {
  const [editPost, { loading }] = useMutation<EditPostType, EditPostVariables>(
    EDIT_POST,
    { ...options },
  );

  return { editPost, loading };
}
Example #22
Source File: useEditTopic.ts    From lexicon with MIT License 6 votes vote down vote up
export function useEditTopic(
  options?: MutationHookOptions<EditTopicType, EditTopicVariables>,
) {
  const [editTopic, { loading }] = useMutation<
    EditTopicType,
    EditTopicVariables
  >(EDIT_TOPIC, {
    ...options,
  });

  return { editTopic, loading };
}
Example #23
Source File: useFlagPost.ts    From lexicon with MIT License 6 votes vote down vote up
export function useFlagPost(
  options?: MutationHookOptions<FlagPostType, FlagPostVariables>,
) {
  const [flag, { loading }] = useMutation<FlagPostType, FlagPostVariables>(
    FLAG_POST,
    {
      ...options,
    },
  );

  return { flag, loading };
}
Example #24
Source File: useNewMessage.ts    From lexicon with MIT License 6 votes vote down vote up
export function useNewMessage(
  options?: MutationHookOptions<NewMessageType, NewMessageVariables>,
) {
  const [newMessage, { loading }] = useMutation<
    NewMessageType,
    NewMessageVariables
  >(NEW_PRIVATE_MESSAGE, { ...options });

  return { newMessage, loading };
}
Example #25
Source File: usePostRaw.ts    From lexicon with MIT License 6 votes vote down vote up
export function usePostRaw(
  options?: MutationHookOptions<PostRawType, PostRawVariables>,
) {
  const [postRaw, { loading }] = useLazyQuery<PostRawType, PostRawVariables>(
    POST_RAW,
    { ...options },
  );

  return { postRaw, loading };
}
Example #26
Source File: form.delete.mutation.ts    From ui with GNU Affero General Public License v3.0 5 votes vote down vote up
useFormDeleteMutation = (
  data?: MutationHookOptions<Data, Variables>
): MutationTuple<Data, Variables> => useMutation<Data, Variables>(MUTATION, data)
Example #27
Source File: form.update.mutation.ts    From ui with GNU Affero General Public License v3.0 5 votes vote down vote up
useFormUpdateMutation = (
  data?: MutationHookOptions<Data, Variables>
): MutationTuple<Data, Variables> => useMutation<Data, Variables>(MUTATION, data)
Example #28
Source File: profile.update.mutation.ts    From ui with GNU Affero General Public License v3.0 5 votes vote down vote up
useProfileUpdateMutation = (
  data?: MutationHookOptions<Data, Variables>
): MutationTuple<Data, Variables> => useMutation<Data, Variables>(MUTATION, data)
Example #29
Source File: register.mutation.ts    From ui with GNU Affero General Public License v3.0 5 votes vote down vote up
useRegisterMutation = (
  data?: MutationHookOptions<Data, Variables>
): MutationTuple<Data, Variables> => useMutation<Data, Variables>(MUTATION, data)