Skip to content

Commit f5e6e74

Browse files
authored
add more documentation for data builders (#6088)
1 parent 3cdd60c commit f5e6e74

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

docs/source/migration/4.0.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,9 @@ The [Android Studio plugin](../testing/android-studio-plugin#migration-helpers)
549549

550550
### Data builders
551551

552-
With Apollo Kotlin 4, the syntax for using data builders for fragments is the same as for operations and [doesn't need the top level `buildFoo {}` wrapper](https://github.com/apollographql/apollo-kotlin/pull/4782):
552+
In Apollo Kotlin 3, using data builders with fragments required nesting the fields in a `buildFoo {}` block to determine the specific type returned.
553+
554+
In Apollo Kotlin 4, this is specified by using the first paramter of the `Data()` constructor function. This allows the syntax for using data builders with fragments to be the same as for operations:
553555

554556
```kotlin
555557
// Replace
@@ -567,6 +569,8 @@ val data = AnimalDetailsImpl.Data(Cat) {
567569
}
568570
```
569571

572+
See [data builders documentation](../testing/data-builders) for more details.
573+
570574
### Test builders are removed
571575

572576
Test builders were an experimental feature that have been superseded by [data builders](../testing/data-builders), a simpler version that also plays nicer with custom scalars.

docs/source/testing/data-builders.mdx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,26 @@ fun test() {
195195
assertEquals(-1, data.hero.age)
196196
}
197197
```
198+
199+
## Using fragments
200+
201+
Because fragments may be defined on interfaces and unions, you need to explicit the concrete type you want to model. For an example, if you have an `Animal` interface, create a `Lion` fragment data using the following:
202+
203+
```kotlin
204+
val data = AnimalDetailsImpl.Data(Lion) {
205+
// you can access roar here
206+
roar = "Grrrrr"
207+
}
208+
```
209+
210+
Sometimes, you may want to test new types defined in your server that are not known to the client yet. To do so, use the abstract type (`Animal` here) as first parameter for the `Data()` constructor function.
211+
212+
In those cases, you need to specify the `__typename` of the returned object type explicitly:
213+
214+
```kotlin
215+
val data = AnimalDetailsImpl.Data(Animal) {
216+
// the client doesn't know about this type yet, and you need to specify it explicitly
217+
__typename = "Brontaroc"
218+
species = "alien"
219+
}
220+
```

0 commit comments

Comments
 (0)