source.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.Source = void 0;
  6. exports.isSource = isSource;
  7. var _devAssert = require('../jsutils/devAssert.js');
  8. var _inspect = require('../jsutils/inspect.js');
  9. var _instanceOf = require('../jsutils/instanceOf.js');
  10. /**
  11. * A representation of source input to GraphQL. The `name` and `locationOffset` parameters are
  12. * optional, but they are useful for clients who store GraphQL documents in source files.
  13. * For example, if the GraphQL input starts at line 40 in a file named `Foo.graphql`, it might
  14. * be useful for `name` to be `"Foo.graphql"` and location to be `{ line: 40, column: 1 }`.
  15. * The `line` and `column` properties in `locationOffset` are 1-indexed.
  16. */
  17. class Source {
  18. constructor(
  19. body,
  20. name = 'GraphQL request',
  21. locationOffset = {
  22. line: 1,
  23. column: 1,
  24. },
  25. ) {
  26. typeof body === 'string' ||
  27. (0, _devAssert.devAssert)(
  28. false,
  29. `Body must be a string. Received: ${(0, _inspect.inspect)(body)}.`,
  30. );
  31. this.body = body;
  32. this.name = name;
  33. this.locationOffset = locationOffset;
  34. this.locationOffset.line > 0 ||
  35. (0, _devAssert.devAssert)(
  36. false,
  37. 'line in locationOffset is 1-indexed and must be positive.',
  38. );
  39. this.locationOffset.column > 0 ||
  40. (0, _devAssert.devAssert)(
  41. false,
  42. 'column in locationOffset is 1-indexed and must be positive.',
  43. );
  44. }
  45. get [Symbol.toStringTag]() {
  46. return 'Source';
  47. }
  48. }
  49. /**
  50. * Test if the given value is a Source object.
  51. *
  52. * @internal
  53. */
  54. exports.Source = Source;
  55. function isSource(source) {
  56. return (0, _instanceOf.instanceOf)(source, Source);
  57. }