source.js.flow 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // @flow strict
  2. import { SYMBOL_TO_STRING_TAG } from '../polyfills/symbols';
  3. import devAssert from '../jsutils/devAssert';
  4. type Location = {|
  5. line: number,
  6. column: number,
  7. |};
  8. /**
  9. * A representation of source input to GraphQL. The `name` and `locationOffset` parameters are
  10. * optional, but they are useful for clients who store GraphQL documents in source files.
  11. * For example, if the GraphQL input starts at line 40 in a file named `Foo.graphql`, it might
  12. * be useful for `name` to be `"Foo.graphql"` and location to be `{ line: 40, column: 1 }`.
  13. * The `line` and `column` properties in `locationOffset` are 1-indexed.
  14. */
  15. export class Source {
  16. body: string;
  17. name: string;
  18. locationOffset: Location;
  19. constructor(
  20. body: string,
  21. name: string = 'GraphQL request',
  22. locationOffset: Location = { line: 1, column: 1 },
  23. ): void {
  24. this.body = body;
  25. this.name = name;
  26. this.locationOffset = locationOffset;
  27. devAssert(
  28. this.locationOffset.line > 0,
  29. 'line in locationOffset is 1-indexed and must be positive.',
  30. );
  31. devAssert(
  32. this.locationOffset.column > 0,
  33. 'column in locationOffset is 1-indexed and must be positive.',
  34. );
  35. }
  36. // $FlowFixMe Flow doesn't support computed properties yet
  37. get [SYMBOL_TO_STRING_TAG]() {
  38. return 'Source';
  39. }
  40. }