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

[DO NOT REVIEW] Align useMutation with Relay #434

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "major",
"comment": "Align nova useMutation with Relay",
"packageName": "@graphitation/apollo-react-relay-duct-tape",
"email": "[email protected]",
"dependentChangeType": "patch"
}
4 changes: 2 additions & 2 deletions examples/apollo-watch-fragments/src/useAddTodoMutation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback } from "react";
import { useMutation } from "@graphitation/apollo-react-relay-duct-tape";
import { useMutation_depricated } from "@graphitation/apollo-react-relay-duct-tape";
import { graphql } from "@graphitation/graphql-js-tag";

import { useAddTodoMutation } from "./__generated__/useAddTodoMutation.graphql";
Expand Down Expand Up @@ -47,7 +47,7 @@ const mutation = graphql`
// let tempID = 0;

export function useAddTodoMutation() {
const [commit] = useMutation<useAddTodoMutation>(mutation);
const [commit] = useMutation_depricated<useAddTodoMutation>(mutation);
return useCallback(
(description: string) => {
return commit({
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-react-relay-duct-tape/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"graphql": "^15.0.0",
"monorepo-scripts": "*",
"react": "^17.0.2",
"relay-hooks": "9.0.0",
"@graphitation/apollo-react-relay-duct-tape-compiler": "^1.5.1",
"ts-expect": "^1.3.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import {
useFragment,
useLazyLoadQuery,
useMutation,
useMutation_deprecated,
useSubscription,
} from "../hooks";

Expand All @@ -30,7 +30,7 @@ import {
hooksTestFragment,
hooksTestFragment$key,
} from "./__generated__/hooksTestFragment.graphql";
import { hooksTestMutation as hooksTestMutation$key } from "./__generated__/hooksTestMutation.graphql";
import { hooksTestMutation, hooksTestMutation as hooksTestMutation$key } from "./__generated__/hooksTestMutation.graphql";
import { getOperationName } from "@apollo/client/utilities";

const schema = buildSchema(
Expand Down Expand Up @@ -134,13 +134,13 @@ const SubscriptionComponent: React.FC<SubjectProps> = ({
return <>{children}</>;
};

const MutationComponent: React.FC<{
const MutationComponentDeprecated: React.FC<{
variables: any;
optimisticResponse: any;
context?: any;
}> = (props) => {
const { variables, optimisticResponse, context } = props;
const [commit, isInFlight] = useMutation<hooksTestMutation$key>(mutation);
const [commit, isInFlight] = useMutation_deprecated<hooksTestMutation$key>(mutation);
const [result, setResult] = React.useState<any>(null);
React.useEffect(() => {
(async function () {
Expand Down Expand Up @@ -357,11 +357,11 @@ describe(useSubscription, () => {
});
});

describe("useMutation", () => {
it("uses Apollo's useMutation hook", async () => {
describe("useMutation_deprecated", () => {
it("uses Apollo's useMutation_deprecated hook", async () => {
const tree = createTestRenderer(
<ApolloProvider client={client}>
<MutationComponent
<MutationComponentDeprecated
variables={{ name: "foo", id: "1" }}
optimisticResponse={null}
context={{ callerInfo: "mutation-component" }}
Expand All @@ -376,7 +376,7 @@ describe("useMutation", () => {
act(() => {
tree.update(
<ApolloProvider client={client}>
<MutationComponent
<MutationComponentDeprecated
variables={{ name: "foo", id: "1" }}
optimisticResponse={{
__typename: "Mutation",
Expand Down
8 changes: 5 additions & 3 deletions packages/apollo-react-relay-duct-tape/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import { convertFetchPolicy } from "./convertFetchPolicy";
import { useOverridenOrDefaultApolloClient } from "./useOverridenOrDefaultApolloClient";
import type { CompiledArtefactModule } from "@graphitation/apollo-react-relay-duct-tape-compiler";
import { FragmentReference } from "./storeObservation/compiledHooks/types";
import { useMutation } from 'relay-hooks';
export {useMutation};

/**
* Executes a GraphQL query.
Expand Down Expand Up @@ -291,7 +293,7 @@ interface IMutationCommitterOptions<TMutationPayload extends OperationType> {
context?: TMutationPayload["context"];
}

type MutationCommiter<TMutationPayload extends OperationType> = (
type MutationCommiter_deprecated<TMutationPayload extends OperationType> = (
options: IMutationCommitterOptions<TMutationPayload>,
) => Promise<{ errors?: Error[]; data?: TMutationPayload["response"] }>;

Expand All @@ -301,9 +303,9 @@ type MutationCommiter<TMutationPayload extends OperationType> = (
* @param mutation
* @returns
*/
export function useMutation<TMutationPayload extends OperationType>(
export function useMutation_deprecated<TMutationPayload extends OperationType>(
mutation: GraphQLTaggedNode,
): [MutationCommiter<TMutationPayload>, boolean] {
): [MutationCommiter_deprecated<TMutationPayload>, boolean] {
invariant(
mutation?.__brand === undefined,
"useMutation: Document must be a valid runtime type.",
Expand Down
38 changes: 38 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3054,6 +3054,13 @@
prop-types "^15.7.2"
tslib "^2.3.0"

"@restart/hooks@^0.4.9":
version "0.4.16"
resolved "https://registry.yarnpkg.com/@restart/hooks/-/hooks-0.4.16.tgz#95ae8ac1cc7e2bd4fed5e39800ff85604c6d59fb"
integrity sha512-f7aCv7c+nU/3mF7NWLtVVr0Ra80RqsO89hO72r+Y/nvQr5+q0UFGkocElTH6MJApvReVh6JHUFYn2cw1WdHF3w==
dependencies:
dequal "^2.0.3"

"@rushstack/[email protected]":
version "3.35.2"
resolved "https://registry.yarnpkg.com/@rushstack/node-core-library/-/node-core-library-3.35.2.tgz#21ca879b5051a5ebafa952fafcd648a07a142bcb"
Expand Down Expand Up @@ -5766,6 +5773,11 @@ dependency-graph@^0.11.0:
resolved "https://registry.yarnpkg.com/dependency-graph/-/dependency-graph-0.11.0.tgz#ac0ce7ed68a54da22165a85e97a01d53f5eb2e27"
integrity sha512-JeMq7fEshyepOWDfcfHK06N3MhyPhz++vtqWhMT5O9A3K42rdsEDpfdVqjaqaAhsw6a+ZqeDvQVtD0hFHQWrzg==

dequal@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/dequal/-/dequal-2.0.3.tgz#2644214f1997d39ed0ee0ece72335490a7ac67be"
integrity sha512-0je+qPKHEMohvfRTCEo3CrPG6cAzAYgmzKyxRiYSSDkS6eGJdyVJm7WaYA5ECaAD9wLB2T4EEeymA5aFVcYXCA==

[email protected]:
version "1.2.0"
resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.2.0.tgz#4803735509ad8be552934c67df614f94e66fa015"
Expand Down Expand Up @@ -6641,6 +6653,19 @@ fbjs@^3.0.0:
setimmediate "^1.0.5"
ua-parser-js "^0.7.18"

fbjs@^3.0.2:
version "3.0.5"
resolved "https://registry.yarnpkg.com/fbjs/-/fbjs-3.0.5.tgz#aa0edb7d5caa6340011790bd9249dbef8a81128d"
integrity sha512-ztsSx77JBtkuMrEypfhgc3cI0+0h+svqeie7xHbh1k/IKdcydnvadp/mUaGgjAOXQmQSxsqgaRhS3q9fy+1kxg==
dependencies:
cross-fetch "^3.1.5"
fbjs-css-vars "^1.0.0"
loose-envify "^1.0.0"
object-assign "^4.1.0"
promise "^7.1.1"
setimmediate "^1.0.5"
ua-parser-js "^1.0.35"

figures@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/figures/-/figures-1.7.0.tgz#cbe1e3affcf1cd44b80cadfed28dc793a9701d2e"
Expand Down Expand Up @@ -10809,6 +10834,14 @@ [email protected], relay-compiler@^12.0.0:
signedsource "^1.0.0"
yargs "^15.3.1"

[email protected]:
version "9.0.0"
resolved "https://registry.yarnpkg.com/relay-hooks/-/relay-hooks-9.0.0.tgz#3ee7f9e9b6de7c3440e2ca7d1c3ef3ddfcb0fb68"
integrity sha512-c0Pjnt8l+RFWFXBtRbQlp5FEo7jUEF9ESSJ0B3uz9s0EkW7n3+R+4OXusprmvZPaOPACISmd/yZlL88/wzQGmQ==
dependencies:
"@restart/hooks" "^0.4.9"
fbjs "^3.0.2"

[email protected]:
version "12.0.0"
resolved "https://registry.yarnpkg.com/relay-runtime/-/relay-runtime-12.0.0.tgz#1e039282bdb5e0c1b9a7dc7f6b9a09d4f4ff8237"
Expand Down Expand Up @@ -12224,6 +12257,11 @@ ua-parser-js@^0.7.18:
resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.34.tgz#afb439e2e3e394bdc90080acb661a39c685b67d7"
integrity sha512-cJMeh/eOILyGu0ejgTKB95yKT3zOenSe9UGE3vj6WfiOwgGYnmATUsnDixMFvdU+rNMvWih83hrUP8VwhF9yXQ==

ua-parser-js@^1.0.35:
version "1.0.39"
resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-1.0.39.tgz#bfc07f361549bf249bd8f4589a4cccec18fd2018"
integrity sha512-k24RCVWlEcjkdOxYmVJgeD/0a1TiSpqLg+ZalVGV9lsnr4yqu0w7tX/x2xX6G4zpkgQnRf89lxuZ1wsbjXM8lw==

uc.micro@^1.0.1, uc.micro@^1.0.5:
version "1.0.6"
resolved "https://registry.yarnpkg.com/uc.micro/-/uc.micro-1.0.6.tgz#9c411a802a409a91fc6cf74081baba34b24499ac"
Expand Down