gql-dedup
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

gql-dedup: a graphql-tag improvement library which de-duplicates fragments

Well-known Apollo's 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.

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):

query User {
  viewer {
    ...FragCreated
    ...FragUpdated
  }
}
fragment FragCreated on User {
  created {
    ...FragDate
  }
}
fragment FragDate on User {
  day
  month
  year
}
fragment FragUpdated on User {
  updated {
    ...FragDate
  }
}

Package Sidebar

Install

npm i gql-dedup

Weekly Downloads

876

Version

1.0.3

License

MIT

Unpacked Size

7.11 kB

Total Files

7

Last publish

Collaborators

  • dimikot