source.mjs 1.4 KB

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