-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
typename selection set transform in client preset (#9562)
- Loading branch information
Showing
4 changed files
with
129 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
'@graphql-codegen/client-preset': minor | ||
--- | ||
|
||
Add the `addTypenameSelectionDocumentTransform` for automatically adding `__typename` selections to all objct type selection sets. | ||
|
||
This is useful for GraphQL Clients such as Apollo Client or urql that need typename information for their cache to function. | ||
|
||
**Example Usage** | ||
|
||
``` | ||
import { addTypenameSelectionDocumentTransform } from '@graphql-codegen/client-preset'; | ||
import { CodegenConfig } from "@graphql-codegen/cli"; | ||
const config: CodegenConfig = { | ||
schema: "YOUR_GRAPHQL_ENDPOINT", | ||
documents: ["./**/*.{ts,tsx}"], | ||
ignoreNoDocuments: true, | ||
generates: { | ||
"./gql/": { | ||
preset: "client", | ||
plugins: [], | ||
presetConfig: { | ||
persistedDocuments: true, | ||
}, | ||
documentTransforms: [addTypenameSelectionDocumentTransform], | ||
}, | ||
}, | ||
}; | ||
export default config; | ||
``` |
38 changes: 38 additions & 0 deletions
38
packages/presets/client/src/add-typename-selection-document-transform.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Kind, visit } from 'graphql'; | ||
import { Types } from '@graphql-codegen/plugin-helpers'; | ||
|
||
/** | ||
* Automatically adds `__typename` selections to every object type in your GraphQL document. | ||
* This is useful for GraphQL Clients such as Apollo Client or urql that need typename information for their cache to function. | ||
*/ | ||
export const addTypenameSelectionDocumentTransform: Types.DocumentTransformObject = { | ||
transform({ documents }) { | ||
return documents.map(document => ({ | ||
...document, | ||
document: document.document | ||
? visit(document.document, { | ||
SelectionSet(node) { | ||
if ( | ||
!node.selections.find(selection => selection.kind === 'Field' && selection.name.value === '__typename') | ||
) { | ||
return { | ||
...node, | ||
selections: [ | ||
{ | ||
kind: Kind.FIELD, | ||
name: { | ||
kind: Kind.NAME, | ||
value: '__typename', | ||
}, | ||
}, | ||
...node.selections, | ||
], | ||
}; | ||
} | ||
return undefined; | ||
}, | ||
}) | ||
: undefined, | ||
})); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters