Skip to content

Commit 71b7092

Browse files
committed
Polishing documentation on pagination
See gh-1055
1 parent 763877b commit 71b7092

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

spring-graphql-docs/modules/ROOT/pages/request-execution.adoc

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -560,24 +560,24 @@ to send to the client.
560560
== Pagination
561561

562562
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.
566566

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.
572572

573573

574574
[[execution.pagination.types]]
575575
=== Connection Types
576576

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:
581581

582582
[source,graphql,indent=0,subs="verbatim,quotes"]
583583
----
@@ -591,11 +591,12 @@ present in the parsed schema files. That means in the schema you only need this:
591591
}
592592
----
593593

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.
597598

598-
Next, configure `ConnectionTypeDefinitionConfigurer` as follows:
599+
To have connection types generated, configure `ConnectionTypeDefinitionConfigurer` as follows:
599600

600601
[source,java,indent=0,subs="verbatim,quotes"]
601602
----
@@ -604,7 +605,8 @@ GraphQlSource.schemaResourceBuilder()
604605
.typeDefinitionConfigurer(new ConnectionTypeDefinitionConfigurer)
605606
----
606607

607-
and the following type definitions will be transparently added to the schema:
608+
The above will add the following type definitions:
609+
608610
[source,graphql,indent=0,subs="verbatim,quotes"]
609611
----
610612
type BookConnection {
@@ -631,19 +633,18 @@ The xref:boot-starter.adoc[Boot Starter] registers `ConnectionTypeDefinitionConf
631633
[[execution.pagination.adapters]]
632634
=== `ConnectionAdapter`
633635

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`.
637640

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`.
643644

644645
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:
647648

648649
[source,java,indent=0,subs="verbatim,quotes"]
649650
----
@@ -659,12 +660,13 @@ GraphQlSource.schemaResourceBuilder()
659660
<1> Create type visitor with one or more ``ConnectionAdapter``s.
660661
<2> Resister the type visitor.
661662

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
665667
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.
668670

669671

670672
[[execution.pagination.cursor.strategy]]

0 commit comments

Comments
 (0)