https://github.com/dimikot/gql-dedup
A graphql-tag improvement library which de-duplicates fragments
https://github.com/dimikot/gql-dedup
Last synced: 10 months ago
JSON representation
A graphql-tag improvement library which de-duplicates fragments
- Host: GitHub
- URL: https://github.com/dimikot/gql-dedup
- Owner: dimikot
- License: mit
- Created: 2020-06-26T03:01:16.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-10-11T04:24:11.000Z (over 2 years ago)
- Last Synced: 2024-08-08T05:06:05.305Z (over 1 year ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/gql-dedup
- Size: 3.91 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-list - gql-dedup - tag improvement library which de-duplicates fragments | DmitryKoterov | 1 | (TypeScript)
README
# gql-dedup: a graphql-tag improvement library which de-duplicates fragments
Well-known Apollo's [graphql-tag](https://github.com/apollographql/graphql-tag)
library hasn't been updated for a long time, and there has been several PRs got
stuck there.
The current library solves one common weakness of graphql-tag: it allows to
refer fragments multiple times and have them de-duplicated in the query.
## Example
Imagine we have a common FragDate fragment which two other fragments,
FragCreated and FragUpdated, depend on. Then, with the default graphql-tag
library, using of **both** FragCreated and FragUpdated in the same query won't
be possible since FragDate would be included twice, and we'd get a server-side
error.
Here comes graphql-tag: it de-duplicates fragments each time they're used.
```ts
import gql from "gql-dedup";
const FragDate = gql`
fragment FragDate on User {
day
month
year
}
`;
const FragCreated = gql`
fragment FragCreated on User {
created {
...FragDate
}
}
${FragDate}
`;
const FragUpdated = gql`
fragment FragUpdated on User {
updated {
...FragDate
}
}
${FragDate}
`;
const Query = gql`
query User {
viewer {
...FragCreated
...FragUpdated
}
}
${FragCreated}
${FragUpdated}
`;
```
Result (notice that FragDate is included only once):
```graphql
query User {
viewer {
...FragCreated
...FragUpdated
}
}
fragment FragCreated on User {
created {
...FragDate
}
}
fragment FragDate on User {
day
month
year
}
fragment FragUpdated on User {
updated {
...FragDate
}
}
```