Skip to content

Commit 6578cbc

Browse files
committed
feat(relay-optimizer-plugin): also transform fragments (in order to have valid documents during runtime)
BREAKING CHANGE: graphql-codegen >= 1.6.1 requires now the `skipDocumentsValidation` config option being set to `true`. Documents will still be validted by relay-compiler.
1 parent d1400f5 commit 6578cbc

File tree

8 files changed

+212
-129
lines changed

8 files changed

+212
-129
lines changed

packages/graphql-codegen-relay-optimizer-plugin/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ schema: schema.graphql
2828
generates:
2929
src/generated-types.tsx:
3030
documents: "src/documents/**/*.graphql"
31+
config:
32+
skipDocumentsValidation: true
3133
plugins:
32-
- "typescript"
3334
- "@n1ru4l/graphql-codegen-relay-optimizer-plugin"
35+
- "typescript"
3436
- "typescript-operations"
3537
- "typescript-react-apollo"
3638
```

packages/graphql-codegen-relay-optimizer-plugin/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ export const plugin: PluginFunction<RelayOptimizerPluginConfig> = (
4747
).addAll(relayDocuments);
4848

4949
const fragmentDocuments = fragmentCompilerContext
50+
.applyTransforms([
51+
RelayApplyFragmentArgumentTransform.transform,
52+
FlattenTransform.transformWithOptions({ flattenAbstractTypes: false }),
53+
SkipRedundantNodesTransform.transform
54+
])
5055
.documents()
5156
.filter(doc => doc.kind === "Fragment");
5257

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "react-app"
3+
}

packages/todo-app-example/codegen.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ generates:
44
src/generated-types.tsx:
55
documents: "src/**/*.graphql"
66
config:
7+
skipDocumentsValidation: true
78
withHOC: false
89
withComponent: false
910
withHooks: true
1011
plugins:
11-
- "typescript"
1212
- "@n1ru4l/graphql-codegen-relay-optimizer-plugin"
13+
- "typescript"
1314
- "typescript-operations"
1415
- "typescript-react-apollo"

packages/todo-app-example/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@
4747
"devDependencies": {
4848
"@babel/node": "7.6.1",
4949
"@babel/preset-flow": "7.0.0",
50-
"@graphql-codegen/cli": "1.7.0",
51-
"@graphql-codegen/plugin-helpers": "1.7.0",
52-
"@graphql-codegen/typescript": "1.7.0",
53-
"@graphql-codegen/typescript-operations": "1.7.0",
54-
"@graphql-codegen/typescript-react-apollo": "1.7.0",
55-
"@n1ru4l/graphql-codegen-relay-optimizer-plugin": "3.0.0",
50+
"@graphql-codegen/cli": "1.7.1-alpha-b651f2ce.56",
51+
"@graphql-codegen/plugin-helpers": "1.7.1-alpha-b651f2ce.56",
52+
"@graphql-codegen/typescript": "1.7.1-alpha-b651f2ce.56",
53+
"@graphql-codegen/typescript-operations": "1.7.1-alpha-b651f2ce.56",
54+
"@graphql-codegen/typescript-react-apollo": "1.7.1-alpha-b651f2ce.56",
55+
"@n1ru4l/graphql-codegen-relay-optimizer-plugin": "3.1.0",
5656
"@types/classnames": "2.2.9",
5757
"@types/jest": "24.0.18",
5858
"@types/node": "12.7.5",

packages/todo-app-example/src/components/TodoListFooter_user.fragment.graphql

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
fragment TodoListFooter_user on User {
1+
fragment TodoListFooter_user on User
2+
@argumentDefinitions(first: { type: "Int", defaultValue: 2147483647 }) {
23
id
34
userId
45
completedCount
5-
todos(
6-
first: 2147483647 # max GraphQLInt
7-
) @connection(key: "TodoList_todos") {
6+
todos(first: $first) @connection(key: "TodoList_todos") {
87
edges {
98
node {
109
id

packages/todo-app-example/src/generated-types.tsx

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,22 @@ export const AppQueryDocument = gql`
468468
}
469469
`;
470470

471+
/**
472+
* __useAppQueryQuery__
473+
*
474+
* To run a query within a React component, call `useAppQueryQuery` and pass it any options that fit your needs.
475+
* When your component renders, `useAppQueryQuery` returns an object from Apollo Client that contains loading, error, and data properties
476+
* you can use to render your UI.
477+
*
478+
* @param baseOptions options that will be passed into the query, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options;
479+
*
480+
* @example
481+
* const { data, loading, error } = useAppQueryQuery({
482+
* variables: {
483+
* userId: // value for 'userId'
484+
* },
485+
* });
486+
*/
471487
export function useAppQueryQuery(
472488
baseOptions?: ApolloReactHooks.QueryHookOptions<
473489
AppQueryQuery,
@@ -490,8 +506,10 @@ export function useAppQueryLazyQuery(
490506
baseOptions
491507
);
492508
}
493-
494509
export type AppQueryQueryHookResult = ReturnType<typeof useAppQueryQuery>;
510+
export type AppQueryLazyQueryHookResult = ReturnType<
511+
typeof useAppQueryLazyQuery
512+
>;
495513
export type AppQueryQueryResult = ApolloReactCommon.QueryResult<
496514
AppQueryQuery,
497515
AppQueryQueryVariables
@@ -520,6 +538,23 @@ export type AddTodoMutationMutationFn = ApolloReactCommon.MutationFunction<
520538
AddTodoMutationMutationVariables
521539
>;
522540

541+
/**
542+
* __useAddTodoMutationMutation__
543+
*
544+
* To run a mutation, you first call `useAddTodoMutationMutation` within a React component and pass it any options that fit your needs.
545+
* When your component renders, `useAddTodoMutationMutation` returns a tuple that includes:
546+
* - A mutate function that you can call at any time to execute the mutation
547+
* - An object with fields that represent the current status of the mutation's execution
548+
*
549+
* @param baseOptions options that will be passed into the mutation, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options-2;
550+
*
551+
* @example
552+
* const [addTodoMutationMutation, { data, loading, error }] = useAddTodoMutationMutation({
553+
* variables: {
554+
* input: // value for 'input'
555+
* },
556+
* });
557+
*/
523558
export function useAddTodoMutationMutation(
524559
baseOptions?: ApolloReactHooks.MutationHookOptions<
525560
AddTodoMutationMutation,
@@ -560,6 +595,23 @@ export type ChangeTodoStatusMutationMutationFn = ApolloReactCommon.MutationFunct
560595
ChangeTodoStatusMutationMutationVariables
561596
>;
562597

598+
/**
599+
* __useChangeTodoStatusMutationMutation__
600+
*
601+
* To run a mutation, you first call `useChangeTodoStatusMutationMutation` within a React component and pass it any options that fit your needs.
602+
* When your component renders, `useChangeTodoStatusMutationMutation` returns a tuple that includes:
603+
* - A mutate function that you can call at any time to execute the mutation
604+
* - An object with fields that represent the current status of the mutation's execution
605+
*
606+
* @param baseOptions options that will be passed into the mutation, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options-2;
607+
*
608+
* @example
609+
* const [changeTodoStatusMutationMutation, { data, loading, error }] = useChangeTodoStatusMutationMutation({
610+
* variables: {
611+
* input: // value for 'input'
612+
* },
613+
* });
614+
*/
563615
export function useChangeTodoStatusMutationMutation(
564616
baseOptions?: ApolloReactHooks.MutationHookOptions<
565617
ChangeTodoStatusMutationMutation,
@@ -600,6 +652,23 @@ export type MarkAllTodosMutationMutationFn = ApolloReactCommon.MutationFunction<
600652
MarkAllTodosMutationMutationVariables
601653
>;
602654

655+
/**
656+
* __useMarkAllTodosMutationMutation__
657+
*
658+
* To run a mutation, you first call `useMarkAllTodosMutationMutation` within a React component and pass it any options that fit your needs.
659+
* When your component renders, `useMarkAllTodosMutationMutation` returns a tuple that includes:
660+
* - A mutate function that you can call at any time to execute the mutation
661+
* - An object with fields that represent the current status of the mutation's execution
662+
*
663+
* @param baseOptions options that will be passed into the mutation, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options-2;
664+
*
665+
* @example
666+
* const [markAllTodosMutationMutation, { data, loading, error }] = useMarkAllTodosMutationMutation({
667+
* variables: {
668+
* input: // value for 'input'
669+
* },
670+
* });
671+
*/
603672
export function useMarkAllTodosMutationMutation(
604673
baseOptions?: ApolloReactHooks.MutationHookOptions<
605674
MarkAllTodosMutationMutation,
@@ -638,6 +707,23 @@ export type RemoveCompletedTodosMutationMutationFn = ApolloReactCommon.MutationF
638707
RemoveCompletedTodosMutationMutationVariables
639708
>;
640709

710+
/**
711+
* __useRemoveCompletedTodosMutationMutation__
712+
*
713+
* To run a mutation, you first call `useRemoveCompletedTodosMutationMutation` within a React component and pass it any options that fit your needs.
714+
* When your component renders, `useRemoveCompletedTodosMutationMutation` returns a tuple that includes:
715+
* - A mutate function that you can call at any time to execute the mutation
716+
* - An object with fields that represent the current status of the mutation's execution
717+
*
718+
* @param baseOptions options that will be passed into the mutation, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options-2;
719+
*
720+
* @example
721+
* const [removeCompletedTodosMutationMutation, { data, loading, error }] = useRemoveCompletedTodosMutationMutation({
722+
* variables: {
723+
* input: // value for 'input'
724+
* },
725+
* });
726+
*/
641727
export function useRemoveCompletedTodosMutationMutation(
642728
baseOptions?: ApolloReactHooks.MutationHookOptions<
643729
RemoveCompletedTodosMutationMutation,
@@ -676,6 +762,23 @@ export type RemoveTodoMutationMutationFn = ApolloReactCommon.MutationFunction<
676762
RemoveTodoMutationMutationVariables
677763
>;
678764

765+
/**
766+
* __useRemoveTodoMutationMutation__
767+
*
768+
* To run a mutation, you first call `useRemoveTodoMutationMutation` within a React component and pass it any options that fit your needs.
769+
* When your component renders, `useRemoveTodoMutationMutation` returns a tuple that includes:
770+
* - A mutate function that you can call at any time to execute the mutation
771+
* - An object with fields that represent the current status of the mutation's execution
772+
*
773+
* @param baseOptions options that will be passed into the mutation, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options-2;
774+
*
775+
* @example
776+
* const [removeTodoMutationMutation, { data, loading, error }] = useRemoveTodoMutationMutation({
777+
* variables: {
778+
* input: // value for 'input'
779+
* },
780+
* });
781+
*/
679782
export function useRemoveTodoMutationMutation(
680783
baseOptions?: ApolloReactHooks.MutationHookOptions<
681784
RemoveTodoMutationMutation,
@@ -712,6 +815,23 @@ export type RenameTodoMutationMutationFn = ApolloReactCommon.MutationFunction<
712815
RenameTodoMutationMutationVariables
713816
>;
714817

818+
/**
819+
* __useRenameTodoMutationMutation__
820+
*
821+
* To run a mutation, you first call `useRenameTodoMutationMutation` within a React component and pass it any options that fit your needs.
822+
* When your component renders, `useRenameTodoMutationMutation` returns a tuple that includes:
823+
* - A mutate function that you can call at any time to execute the mutation
824+
* - An object with fields that represent the current status of the mutation's execution
825+
*
826+
* @param baseOptions options that will be passed into the mutation, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options-2;
827+
*
828+
* @example
829+
* const [renameTodoMutationMutation, { data, loading, error }] = useRenameTodoMutationMutation({
830+
* variables: {
831+
* input: // value for 'input'
832+
* },
833+
* });
834+
*/
715835
export function useRenameTodoMutationMutation(
716836
baseOptions?: ApolloReactHooks.MutationHookOptions<
717837
RenameTodoMutationMutation,

0 commit comments

Comments
 (0)