signature.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // In Apollo Studio, we want to group requests making the same query together,
  2. // and treat different queries distinctly. But what does it mean for two queries
  3. // to be "the same"? And what if you don't want to send the full text of the
  4. // query to Apollo's servers, either because it contains sensitive data or
  5. // because it contains extraneous operations or fragments?
  6. //
  7. // To solve these problems, Apollo Studio and related components have the
  8. // concept of "signatures". We don't (by default) send the full query string of
  9. // queries to Apollo's servers. Instead, each trace has its query string's
  10. // "signature".
  11. //
  12. // This module combines several AST transformations to create its signature:
  13. //
  14. // - dropUnusedDefinitions, which removes operations and fragments that aren't
  15. // going to be used in execution
  16. // - stripSensitiveLiterals, which replaces all numeric and string literals as
  17. // well as list and object input values with "empty" values
  18. // - removeAliases, which removes field aliasing from the query
  19. // - sortAST, which sorts the children of most multi-child nodes consistently
  20. // - printWithReducedWhitespace, a variant on graphql-js's 'print' which gets
  21. // rid of unneeded whitespace
  22. import { dropUnusedDefinitions } from "@apollo/utils.dropunuseddefinitions";
  23. import { stripSensitiveLiterals } from "@apollo/utils.stripsensitiveliterals";
  24. import { printWithReducedWhitespace } from "@apollo/utils.printwithreducedwhitespace";
  25. import { removeAliases } from "@apollo/utils.removealiases";
  26. import { sortAST } from "@apollo/utils.sortast";
  27. import type { DocumentNode } from "graphql";
  28. export function usageReportingSignature(
  29. ast: DocumentNode,
  30. operationName: string,
  31. ): string {
  32. return printWithReducedWhitespace(
  33. sortAST(
  34. removeAliases(
  35. stripSensitiveLiterals(dropUnusedDefinitions(ast, operationName), {
  36. hideListAndObjectLiterals: true,
  37. }),
  38. ),
  39. ),
  40. );
  41. }