connection.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.connectionDefinitions = connectionDefinitions;
  6. exports.connectionArgs =
  7. exports.backwardConnectionArgs =
  8. exports.forwardConnectionArgs =
  9. void 0;
  10. var _graphql = require('graphql');
  11. /**
  12. * Returns a GraphQLFieldConfigArgumentMap appropriate to include on a field
  13. * whose return type is a connection type with forward pagination.
  14. */
  15. const forwardConnectionArgs = Object.freeze({
  16. after: {
  17. type: _graphql.GraphQLString,
  18. description:
  19. 'Returns the items in the list that come after the specified cursor.',
  20. },
  21. first: {
  22. type: _graphql.GraphQLInt,
  23. description: 'Returns the first n items from the list.',
  24. },
  25. });
  26. /**
  27. * Returns a GraphQLFieldConfigArgumentMap appropriate to include on a field
  28. * whose return type is a connection type with backward pagination.
  29. */
  30. exports.forwardConnectionArgs = forwardConnectionArgs;
  31. const backwardConnectionArgs = Object.freeze({
  32. before: {
  33. type: _graphql.GraphQLString,
  34. description:
  35. 'Returns the items in the list that come before the specified cursor.',
  36. },
  37. last: {
  38. type: _graphql.GraphQLInt,
  39. description: 'Returns the last n items from the list.',
  40. },
  41. });
  42. /**
  43. * Returns a GraphQLFieldConfigArgumentMap appropriate to include on a field
  44. * whose return type is a connection type with bidirectional pagination.
  45. */
  46. exports.backwardConnectionArgs = backwardConnectionArgs;
  47. const connectionArgs = {
  48. ...forwardConnectionArgs,
  49. ...backwardConnectionArgs,
  50. };
  51. /**
  52. * A type alias for cursors in this implementation.
  53. */
  54. /**
  55. * A type describing the arguments a connection field receives in GraphQL.
  56. */
  57. exports.connectionArgs = connectionArgs;
  58. /**
  59. * Returns a GraphQLObjectType for a connection with the given name,
  60. * and whose nodes are of the specified type.
  61. */
  62. function connectionDefinitions(config) {
  63. var _config$name;
  64. const { nodeType } = config;
  65. const name =
  66. (_config$name = config.name) !== null && _config$name !== void 0
  67. ? _config$name
  68. : (0, _graphql.getNamedType)(nodeType).name;
  69. const edgeType = new _graphql.GraphQLObjectType({
  70. name: name + 'Edge',
  71. description: 'An edge in a connection.',
  72. fields: () => {
  73. var _config$edgeFields;
  74. return {
  75. node: {
  76. type: nodeType,
  77. resolve: config.resolveNode,
  78. description: 'The item at the end of the edge',
  79. },
  80. cursor: {
  81. type: new _graphql.GraphQLNonNull(_graphql.GraphQLString),
  82. resolve: config.resolveCursor,
  83. description: 'A cursor for use in pagination',
  84. },
  85. ...(0, _graphql.resolveObjMapThunk)(
  86. (_config$edgeFields = config.edgeFields) !== null &&
  87. _config$edgeFields !== void 0
  88. ? _config$edgeFields
  89. : {},
  90. ),
  91. };
  92. },
  93. });
  94. const connectionType = new _graphql.GraphQLObjectType({
  95. name: name + 'Connection',
  96. description: 'A connection to a list of items.',
  97. fields: () => {
  98. var _config$connectionFie;
  99. return {
  100. pageInfo: {
  101. type: new _graphql.GraphQLNonNull(pageInfoType),
  102. description: 'Information to aid in pagination.',
  103. },
  104. edges: {
  105. type: new _graphql.GraphQLList(edgeType),
  106. description: 'A list of edges.',
  107. },
  108. ...(0, _graphql.resolveObjMapThunk)(
  109. (_config$connectionFie = config.connectionFields) !== null &&
  110. _config$connectionFie !== void 0
  111. ? _config$connectionFie
  112. : {},
  113. ),
  114. };
  115. },
  116. });
  117. return {
  118. edgeType,
  119. connectionType,
  120. };
  121. }
  122. /**
  123. * A type designed to be exposed as a `Connection` over GraphQL.
  124. */
  125. /**
  126. * A type designed to be exposed as a `Edge` over GraphQL.
  127. */
  128. /**
  129. * The common page info type used by all connections.
  130. */
  131. const pageInfoType = new _graphql.GraphQLObjectType({
  132. name: 'PageInfo',
  133. description: 'Information about pagination in a connection.',
  134. fields: () => ({
  135. hasNextPage: {
  136. type: new _graphql.GraphQLNonNull(_graphql.GraphQLBoolean),
  137. description: 'When paginating forwards, are there more items?',
  138. },
  139. hasPreviousPage: {
  140. type: new _graphql.GraphQLNonNull(_graphql.GraphQLBoolean),
  141. description: 'When paginating backwards, are there more items?',
  142. },
  143. startCursor: {
  144. type: _graphql.GraphQLString,
  145. description: 'When paginating backwards, the cursor to continue.',
  146. },
  147. endCursor: {
  148. type: _graphql.GraphQLString,
  149. description: 'When paginating forwards, the cursor to continue.',
  150. },
  151. }),
  152. });
  153. /**
  154. * A type designed to be exposed as `PageInfo` over GraphQL.
  155. */