@@ -560,24 +560,24 @@ to send to the client.
560
560
== Pagination
561
561
562
562
The GraphQL https://relay.dev/graphql/connections.htm[Cursor Connection specification]
563
- defines a way to navigate large result sets by returning a subset of items at a time where
564
- each item is paired with a cursor that clients can use to request more items before or
565
- after the referenced item.
563
+ defines a way to navigate large result sets by returning a subset of items at a time in
564
+ which each item is paired with a cursor that clients can use to request more items before
565
+ or after the referenced item.
566
566
567
- The specification calls the pattern _"Connections"_. A schema type with a name that ends
568
- on Connection is a _Connection Type_ that represents a paginated result set. All `~Connection`
569
- types contain an "edges" field where `~Edge` type pairs the actual item with a cursor, as
570
- well as a "pageInfo" field with boolean flags to indicate if there are more items forward
571
- and backward.
567
+ The specification calls this pattern _"Connections"_, and schema types whose name end
568
+ with `~ Connection` are a connection type that represents a paginated result set.
569
+ All connection types contain a field called "edges" where an `~Edge` type contains
570
+ the actual item, a cursor, and a field called "pageInfo" that indicates if more
571
+ items exist forward and backward.
572
572
573
573
574
574
[[execution.pagination.types]]
575
575
=== Connection Types
576
576
577
- ` Connection` type definitions must be created for every type that needs pagination, adding
578
- boilerplate and noise to the schema. Spring for GraphQL provides
579
- `ConnectionTypeDefinitionConfigurer` to add these types on startup, if not already
580
- present in the parsed schema files. That means in the schema you only need this :
577
+ Connection types require boilerplate definitions that Spring for GraphQL's
578
+ `ConnectionTypeDefinitionConfigurer` can add transparently on startup, if not explicitly
579
+ declared. That means you only need the below, and the connection and edge types will
580
+ be added for you:
581
581
582
582
[source,graphql,indent=0,subs="verbatim,quotes"]
583
583
----
@@ -591,11 +591,12 @@ present in the parsed schema files. That means in the schema you only need this:
591
591
}
592
592
----
593
593
594
- Note the spec-defined forward pagination arguments `first` and `after` that clients can use
595
- to request the first N items after the given cursor, while `last` and `before` are backward
596
- pagination arguments to request the last N items before the given cursor.
594
+ The spec defined `first` and `after` arguments for forward pagination allow clients to
595
+ request the "first" N items "after" a given cursor. Similarly, the `last` and `before`
596
+ arguments for backward pagination arguments allow requesting the "last" N items "before"
597
+ a given cursor.
597
598
598
- Next , configure `ConnectionTypeDefinitionConfigurer` as follows:
599
+ To have connection types generated , configure `ConnectionTypeDefinitionConfigurer` as follows:
599
600
600
601
[source,java,indent=0,subs="verbatim,quotes"]
601
602
----
@@ -604,7 +605,8 @@ GraphQlSource.schemaResourceBuilder()
604
605
.typeDefinitionConfigurer(new ConnectionTypeDefinitionConfigurer)
605
606
----
606
607
607
- and the following type definitions will be transparently added to the schema:
608
+ The above will add the following type definitions:
609
+
608
610
[source,graphql,indent=0,subs="verbatim,quotes"]
609
611
----
610
612
type BookConnection {
@@ -631,19 +633,18 @@ The xref:boot-starter.adoc[Boot Starter] registers `ConnectionTypeDefinitionConf
631
633
[[execution.pagination.adapters]]
632
634
=== `ConnectionAdapter`
633
635
634
- Once xref:request-execution.adoc#execution.pagination.types[Connection Types] are available in the schema, you also need
635
- equivalent Java types. GraphQL Java provides those, including generic `Connection` and
636
- `Edge`, as well as a `PageInfo`.
636
+ In addition to
637
+ xref:request-execution.adoc#execution.pagination.types[Connection Types] in the schema,
638
+ you will also need equivalent Java types. GraphQL Java provides those, including generic
639
+ `Connection` and `Edge` types, and `PageInfo`.
637
640
638
- One option is to populate a `Connection` and return it from your controller method or
639
- `DataFetcher`. However, this requires boilerplate code to create the `Connection`,
640
- creating cursors, wrapping each item as an `Edge`, and creating the `PageInfo`.
641
- Moreover, you may already have an underlying pagination mechanism such as when using
642
- Spring Data repositories.
641
+ You can return `Connection` from a controller method, but it requires boilerplate code
642
+ to adapt your underlying data pagination mechanism to `Connection`, to create cursors,
643
+ add `~Edge` wrappers, and create a `PageInfo`.
643
644
644
645
Spring for GraphQL defines the `ConnectionAdapter` contract to adapt a container of items
645
- to `Connection`. Adapters are applied through a `DataFetcher` decorator that is in turn
646
- installed through a `ConnectionFieldTypeVisitor`. You can configure it as follows:
646
+ to `Connection`. Adapters are invoked from a `DataFetcher` decorator that is in turn
647
+ added by a `ConnectionFieldTypeVisitor`. You can configure it as follows:
647
648
648
649
[source,java,indent=0,subs="verbatim,quotes"]
649
650
----
@@ -659,12 +660,13 @@ GraphQlSource.schemaResourceBuilder()
659
660
<1> Create type visitor with one or more ``ConnectionAdapter``s.
660
661
<2> Resister the type visitor.
661
662
662
- There are xref:data.adoc#data.pagination.scroll[built-in] ``ConnectionAdapter``s for Spring Data's
663
- `Window` and `Slice`. You can also create your own custom adapter. `ConnectionAdapter`
664
- implementations rely on a xref:request-execution.adoc#execution.pagination.cursor.strategy[`CursorStrategy`] to
663
+ There are built-in xref:data.adoc#data.pagination.scroll[built-in] ``ConnectionAdapter``s
664
+ for Spring Data's `Window` and `Slice`. You can also create your own custom adapter.
665
+ `ConnectionAdapter` implementations rely on a
666
+ xref:request-execution.adoc#execution.pagination.cursor.strategy[`CursorStrategy`] to
665
667
create cursors for returned items. The same strategy is also used to support the
666
- xref:controllers.adoc#controllers.schema-mapping.subrange[`Subrange`] controller method argument that contains
667
- pagination input.
668
+ xref:controllers.adoc#controllers.schema-mapping.subrange[`Subrange`] controller method
669
+ argument that contains pagination input.
668
670
669
671
670
672
[[execution.pagination.cursor.strategy]]
0 commit comments