source.js.flow 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // @flow strict
  2. import { SYMBOL_TO_STRING_TAG } from '../polyfills/symbols';
  3. import inspect from '../jsutils/inspect';
  4. import devAssert from '../jsutils/devAssert';
  5. import instanceOf from '../jsutils/instanceOf';
  6. type Location = {|
  7. line: number,
  8. column: number,
  9. |};
  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. export class Source {
  18. body: string;
  19. name: string;
  20. locationOffset: Location;
  21. constructor(
  22. body: string,
  23. name: string = 'GraphQL request',
  24. locationOffset: Location = { line: 1, column: 1 },
  25. ) {
  26. devAssert(
  27. typeof body === 'string',
  28. `Body must be a string. Received: ${inspect(body)}.`,
  29. );
  30. this.body = body;
  31. this.name = name;
  32. this.locationOffset = locationOffset;
  33. devAssert(
  34. this.locationOffset.line > 0,
  35. 'line in locationOffset is 1-indexed and must be positive.',
  36. );
  37. devAssert(
  38. this.locationOffset.column > 0,
  39. 'column in locationOffset is 1-indexed and must be positive.',
  40. );
  41. }
  42. // $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
  43. get [SYMBOL_TO_STRING_TAG]() {
  44. return 'Source';
  45. }
  46. }
  47. /**
  48. * Test if the given value is a Source object.
  49. *
  50. * @internal
  51. */
  52. declare function isSource(source: mixed): boolean %checks(source instanceof
  53. Source);
  54. // eslint-disable-next-line no-redeclare
  55. export function isSource(source) {
  56. return instanceOf(source, Source);
  57. }