source.js.flow 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // @flow strict
  2. import devAssert from '../jsutils/devAssert';
  3. import defineToStringTag from '../jsutils/defineToStringTag';
  4. type Location = {|
  5. line: number,
  6. column: number,
  7. |};
  8. /**
  9. * A representation of source input to GraphQL.
  10. * `name` and `locationOffset` are optional. They are useful for clients who
  11. * store GraphQL documents in source files; for example, if the GraphQL input
  12. * starts at line 40 in a file named Foo.graphql, it might be useful for name to
  13. * be "Foo.graphql" and location to be `{ line: 40, column: 0 }`.
  14. * line and column in locationOffset are 1-indexed
  15. */
  16. export class Source {
  17. body: string;
  18. name: string;
  19. locationOffset: Location;
  20. constructor(body: string, name?: string, locationOffset?: Location): void {
  21. this.body = body;
  22. this.name = name || 'GraphQL request';
  23. this.locationOffset = locationOffset || { line: 1, column: 1 };
  24. devAssert(
  25. this.locationOffset.line > 0,
  26. 'line in locationOffset is 1-indexed and must be positive',
  27. );
  28. devAssert(
  29. this.locationOffset.column > 0,
  30. 'column in locationOffset is 1-indexed and must be positive',
  31. );
  32. }
  33. }
  34. // Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
  35. defineToStringTag(Source);