Skip to content

Commit 3d1ebcd

Browse files
committed
Require passing generated flow type for useMutation
1 parent 74631be commit 3d1ebcd

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/rule-generated-flow-types.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,24 @@ module.exports = {
564564
});
565565
},
566566

567+
/**
568+
* Find useMutation() calls without type arguments.
569+
*/
570+
'CallExpression[callee.name=useMutation]:not([typeArguments])'(node) {
571+
const queryName = getDefinitionName(node.arguments[0]);
572+
context.report({
573+
node,
574+
message: `The \`useMutation\` hook should be used with an explicit generated Flow type, e.g.: useMutation<{{queryName}}>(...)`,
575+
data: {
576+
queryName: queryName
577+
},
578+
fix:
579+
queryName != null && options.fix
580+
? createTypeImportFixer(node, queryName, queryName)
581+
: null
582+
});
583+
},
584+
567585
/**
568586
* Find usePaginationFragment() calls without type arguments.
569587
*/

test/test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,21 @@ import type {FooQuery} from './__generated__/FooQuery.graphql'
901901
],
902902
output: null
903903
},
904+
{
905+
code: `\nconst mutation = graphql\`mutation FooMutation { id }\`;\nconst [commit] = useMutation(mutation);`,
906+
options: DEFAULT_OPTIONS,
907+
errors: [
908+
{
909+
message:
910+
'The `useMutation` hook should be used with an explicit generated Flow type, e.g.: useMutation<FooMutation>(...)',
911+
line: 3
912+
}
913+
],
914+
output: `
915+
import type {FooMutation} from './__generated__/FooMutation.graphql'
916+
const mutation = graphql\`mutation FooMutation { id }\`;
917+
const [commit] = useMutation<FooMutation>(mutation);`
918+
},
904919
{
905920
code: `\ncommitMutation(environemnt, {mutation: graphql\`mutation FooMutation { id }\`})`,
906921
errors: [

0 commit comments

Comments
 (0)