predicates.js.flow 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // @flow strict
  2. import { Kind } from './kinds';
  3. import { type ASTNode } from './ast';
  4. export function isDefinitionNode(node: ASTNode): boolean %checks {
  5. return (
  6. isExecutableDefinitionNode(node) ||
  7. isTypeSystemDefinitionNode(node) ||
  8. isTypeSystemExtensionNode(node)
  9. );
  10. }
  11. export function isExecutableDefinitionNode(node: ASTNode): boolean %checks {
  12. return (
  13. node.kind === Kind.OPERATION_DEFINITION ||
  14. node.kind === Kind.FRAGMENT_DEFINITION
  15. );
  16. }
  17. export function isSelectionNode(node: ASTNode): boolean %checks {
  18. return (
  19. node.kind === Kind.FIELD ||
  20. node.kind === Kind.FRAGMENT_SPREAD ||
  21. node.kind === Kind.INLINE_FRAGMENT
  22. );
  23. }
  24. export function isValueNode(node: ASTNode): boolean %checks {
  25. return (
  26. node.kind === Kind.VARIABLE ||
  27. node.kind === Kind.INT ||
  28. node.kind === Kind.FLOAT ||
  29. node.kind === Kind.STRING ||
  30. node.kind === Kind.BOOLEAN ||
  31. node.kind === Kind.NULL ||
  32. node.kind === Kind.ENUM ||
  33. node.kind === Kind.LIST ||
  34. node.kind === Kind.OBJECT
  35. );
  36. }
  37. export function isTypeNode(node: ASTNode): boolean %checks {
  38. return (
  39. node.kind === Kind.NAMED_TYPE ||
  40. node.kind === Kind.LIST_TYPE ||
  41. node.kind === Kind.NON_NULL_TYPE
  42. );
  43. }
  44. export function isTypeSystemDefinitionNode(node: ASTNode): boolean %checks {
  45. return (
  46. node.kind === Kind.SCHEMA_DEFINITION ||
  47. isTypeDefinitionNode(node) ||
  48. node.kind === Kind.DIRECTIVE_DEFINITION
  49. );
  50. }
  51. export function isTypeDefinitionNode(node: ASTNode): boolean %checks {
  52. return (
  53. node.kind === Kind.SCALAR_TYPE_DEFINITION ||
  54. node.kind === Kind.OBJECT_TYPE_DEFINITION ||
  55. node.kind === Kind.INTERFACE_TYPE_DEFINITION ||
  56. node.kind === Kind.UNION_TYPE_DEFINITION ||
  57. node.kind === Kind.ENUM_TYPE_DEFINITION ||
  58. node.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION
  59. );
  60. }
  61. export function isTypeSystemExtensionNode(node: ASTNode): boolean %checks {
  62. return node.kind === Kind.SCHEMA_EXTENSION || isTypeExtensionNode(node);
  63. }
  64. export function isTypeExtensionNode(node: ASTNode): boolean %checks {
  65. return (
  66. node.kind === Kind.SCALAR_TYPE_EXTENSION ||
  67. node.kind === Kind.OBJECT_TYPE_EXTENSION ||
  68. node.kind === Kind.INTERFACE_TYPE_EXTENSION ||
  69. node.kind === Kind.UNION_TYPE_EXTENSION ||
  70. node.kind === Kind.ENUM_TYPE_EXTENSION ||
  71. node.kind === Kind.INPUT_OBJECT_TYPE_EXTENSION
  72. );
  73. }