-
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.
feat: add support for metadata and persisted operations in client pre…
…set (#8757) * feat: add support for persisted operations * always do cleanup * crypto ftw * sanitize document * stable print and config options * feat: only include executable documents in persisted operations map * feat: stable print * fix shit * feat: new __meta__ approach * test: embed metadata in document node * refactor: use Map and remove hacks * docs: update react-query instructions * add changesets * feat: update to stable package version * chore(dependencies): updated changesets for modified dependencies Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
5bc753d
commit 4f290aa
Showing
10 changed files
with
955 additions
and
134 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
.changeset/@graphql-codegen_client-preset-8757-dependencies.md
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,5 @@ | ||
--- | ||
"@graphql-codegen/client-preset": patch | ||
--- | ||
dependencies updates: | ||
- Added dependency [`@graphql-tools/documents@^0.1.0` ↗︎](https://www.npmjs.com/package/@graphql-tools/documents/v/0.1.0) (to `dependencies`) |
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,52 @@ | ||
--- | ||
'@graphql-codegen/client-preset': minor | ||
--- | ||
|
||
Add support for persisted documents. | ||
|
||
You can now generate and embed a persisted documents hash for the executable documents. | ||
|
||
```ts | ||
/** codegen.ts */ | ||
import { CodegenConfig } from '@graphql-codegen/cli' | ||
|
||
const config: CodegenConfig = { | ||
schema: 'https://swapi-graphql.netlify.app/.netlify/functions/index', | ||
documents: ['src/**/*.tsx'], | ||
ignoreNoDocuments: true, // for better experience with the watcher | ||
generates: { | ||
'./src/gql/': { | ||
preset: 'client', | ||
plugins: [], | ||
presetConfig: { | ||
persistedOperations: true, | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default config | ||
``` | ||
|
||
This will generate `./src/gql/persisted-documents.json` (dictionary of hashes with their operation string). | ||
|
||
In addition to that each generated document node will have a `__meta__.hash` property. | ||
|
||
```ts | ||
import { gql } from './gql.js' | ||
|
||
const allFilmsWithVariablesQueryDocument = graphql(/* GraphQL */ ` | ||
query allFilmsWithVariablesQuery($first: Int!) { | ||
allFilms(first: $first) { | ||
edges { | ||
node { | ||
...FilmItem | ||
} | ||
} | ||
} | ||
} | ||
`) | ||
|
||
console.log((allFilmsWithVariablesQueryDocument as any)["__meta__"]["hash"]) | ||
``` | ||
|
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,54 @@ | ||
--- | ||
'@graphql-codegen/client-preset': minor | ||
--- | ||
|
||
Add support for embedding metadata in the document AST. | ||
|
||
It is now possible to embed metadata (e.g. for your GraphQL client within the emitted code). | ||
|
||
```ts | ||
/** codegen.ts */ | ||
import { CodegenConfig } from '@graphql-codegen/cli' | ||
|
||
const config: CodegenConfig = { | ||
schema: 'https://swapi-graphql.netlify.app/.netlify/functions/index', | ||
documents: ['src/**/*.tsx'], | ||
ignoreNoDocuments: true, // for better experience with the watcher | ||
generates: { | ||
'./src/gql/': { | ||
preset: 'client', | ||
plugins: [], | ||
presetConfig: { | ||
onExecutableDocumentNode(documentNode) { | ||
return { | ||
operation: documentNode.definitions[0].operation, | ||
name: documentNode.definitions[0].name.value | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default config | ||
``` | ||
|
||
You can then access the metadata via the `__meta__` property on the document node. | ||
|
||
```ts | ||
import { gql } from './gql.js' | ||
|
||
const allFilmsWithVariablesQueryDocument = graphql(/* GraphQL */ ` | ||
query allFilmsWithVariablesQuery($first: Int!) { | ||
allFilms(first: $first) { | ||
edges { | ||
node { | ||
...FilmItem | ||
} | ||
} | ||
} | ||
} | ||
`) | ||
|
||
console.log((allFilmsWithVariablesQueryDocument as any)["__meta__"]) | ||
``` |
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
Oops, something went wrong.