|
| 1 | +# Arguments and input types |
| 2 | + |
| 3 | +Resolver methods take GraphQL arguments as ordinary parameters, in SDL order. graphql-java-tools converts scalars, enums, |
| 4 | +lists and input objects into the parameter's declared class, applies SDL default values, and passes |
| 5 | +`DataFetchingEnvironment` when it is the last parameter. An input object can refer to itself, or to another input object |
| 6 | +that refers back. With `inputArgumentOptionalDetectOmission`, an `Optional` parameter tells an omitted argument (`null`) |
| 7 | +from an explicit `null` (`Optional.empty()`). |
| 8 | + |
| 9 | +## Sub-features |
| 10 | + |
| 11 | +- `args-defaults` applies an argument's SDL default when the operation omits it. |
| 12 | +- `args-enum-list` converts `[Genre!]` literals into a `List<Genre>` of a Kotlin enum. |
| 13 | +- `args-env` passes `DataFetchingEnvironment` as the trailing parameter. |
| 14 | +- `args-input-object` maps a nested input object into data classes, including input field defaults. |
| 15 | +- `args-optional-omission` passes `null` for an omitted argument, `Optional.empty()` for an explicit null, and |
| 16 | + `Optional.of` for a value. |
| 17 | +- `args-invalid-enum` rejects an unknown enum literal during validation, before any resolver runs. |
| 18 | +- `args-unmapped-warn` logs a WARN at build for an input field the argument's class has no property for, here a |
| 19 | + misspelled property under an `Optional`. |
| 20 | +- `args-unmapped-ignored` doesn't report a field the property's `@JsonIgnoreProperties` ignores, here on an `Optional` |
| 21 | + property. |
| 22 | +- `args-type-variable` converts an argument whose parameter is typed by the resolver superclass's type variable into the |
| 23 | + class the subclass binds it to. |
| 24 | +- `args-scalars` passes a custom scalar's coerced object through unconverted, also when it's a Kotlin class without visible |
| 25 | + properties inside an input object, converts `ID` into a `Long` parameter, and converts `[[String]]` into a |
| 26 | + `List<Set<String>>` parameter. |
| 27 | +- `args-recursive` converts an input object that refers to itself, and two that refer to each other, through lists, nested |
| 28 | + lists and non-null types into data classes at every depth. It also builds a directive whose arguments are an input |
| 29 | + object, an enum with a default and a list. |
| 30 | + |
| 31 | +## Expected results |
| 32 | + |
| 33 | +- **Run.** `run inputs`. Exit `0`. `summary.txt` shows `errors=0` for `01` to |
| 34 | + `03` and `errors=1` for `04-invalid-enum`. |
| 35 | +- **Defaults, enums, env.** `01-search.json` has |
| 36 | + `data.defaults = "term=dune limit=10 genres=null field=search"` and |
| 37 | + `data.explicit = "term=dune limit=2 genres=[SCIENCE, HISTORY] field=search"`. |
| 38 | +- **Omitted vs null.** `02-nickname.json` has `omitted = "omitted"`, `explicitNull = "explicit null"` and |
| 39 | + `given = "value Bilbo"`. |
| 40 | +- **Input objects.** `03-add-book.json` (variables in `03-add-book.variables.json`) has `data.addBook` equal to |
| 41 | + `{"title": "Cosmos", "genre": "SCIENCE", "tags": ["space", "classic"], "pages": 396, "language": "en"}`. `language` |
| 42 | + comes from the input field default. `data.defaults` is `{"genre": "FICTION", "tags": [], "pages": null}`. |
| 43 | +- **Invalid enum.** `04-invalid-enum.json` has no `data`, and one error whose message starts with |
| 44 | + `Validation error (WrongType@[search])` and names `POETRY`. |
| 45 | +- **Unmapped input fields.** `run unmapped-inputs`. Exit `0`. `probe.log` has |
| 46 | + exactly one `UnmappedInputFieldChecker` WARN, starting `Input field 'ItemInput.name' has no matching property in |
| 47 | + scenarios.ItemInput` and listing `Known properties: nmae`. There is no WARN for `InnerInput.legacy`. |
| 48 | + `01-ignored.json` has `data.ignored = "a"`. `02-typo.json` has one error containing |
| 49 | + `Unrecognized property "name" (class scenarios.ItemInput)` (`Unrecognized field` before Jackson 3, in 14.x), which shows |
| 50 | + the reported field really fails. |
| 51 | +- **Type variable parameters.** `run generic-input-resolver`. Exit `0`. |
| 52 | + `summary.txt` shows `errors=0` for both operations. `02-generic.json` has |
| 53 | + `data.generic = "generic got GreetingInput(name=a)"`, and `01-plain.json` has |
| 54 | + `data.plain = "plain got GreetingInput(name=a)"`. |
| 55 | +- **Scalar arguments.** `run scalar-args`. Exit `0`. `summary.txt` shows |
| 56 | + `errors=0` for the first five operations. `01-custom-scalar.json` has `data.tag = "tag kotlin"` and |
| 57 | + `data.tags = "tags [a, b]"` (`Tag` has no constructor Jackson can use, so this only works when the value is passed |
| 58 | + through). `02-id.json` has `data.id = "id 43 (long)"`. `03-nested-sets.json` has |
| 59 | + `data.nestedSets = "sets [[a, b], [c]] sizes [2, 1]"`. `04-nested-lists.json` has |
| 60 | + `data.nestedLists = "lists [[a, a, b], [c]]"`. `05-empty-kotlin-scalar.json` has `data.tokenInput = "token input"`. |
| 61 | + `06-marked-empty-kotlin-scalars.json` has one error and `07-optional-marked-empty-kotlin-scalars.json` two, all containing |
| 62 | + `missing type id property '@class'` or `Cannot deserialize value of type`. Those scalars fail rather than reach the resolver |
| 63 | + as a default instance. |
| 64 | +- **Recursive inputs.** `run recursive-inputs`. Exit `0`, `errors=0` for both operations and an empty `probe.log`. |
| 65 | + `01-describe.json` (variables in `01-describe.variables.json`) has `data.names = ["a", "b"]`. `data.describe` starts |
| 66 | + `TreeFilter(name=root, and=[TreeFilter(name=a,` and ends `siblings=[[ChildFilter(parent=null, siblings=null, depth=4)], |
| 67 | + []], depth=3))`. `data.literal` is `TreeFilter(name=x, and=null, not=null, child=ChildFilter(parent=null, |
| 68 | + siblings=[[ChildFilter(parent=null, siblings=null, depth=2)]], depth=1))`. `schema.graphqls` has `siblings: |
| 69 | + [[ChildFilter!]!]` and `directive @limit(labels: [String!], range: RangeInput!, unit: LimitUnit = ITEMS) on |
| 70 | + FIELD_DEFINITION | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION`. In `02-introspect.json`, `limit` has `range` of |
| 71 | + `RangeInput!`, `unit` of `LimitUnit` with default `ITEMS`, and `labels` of `[String!]`. |
| 72 | + |
| 73 | +## Gotchas |
| 74 | + |
| 75 | +- Arguments bind by position, not name. A resolver whose parameters are in a different order than the SDL still builds |
| 76 | + when the types line up, and returns swapped values. |
| 77 | +- Without `inputArgumentOptionalDetectOmission`, an omitted argument and an explicit null both arrive as |
| 78 | + `Optional.empty()`. This scenario turns the option on. Prove the default behavior in a separate scenario. |
| 79 | +- `04-invalid-enum` fails in graphql-java validation. It proves the generated enum type, not the resolver's conversion. |
| 80 | +- The unmapped input field check comes from PR #857. On a checkout without it, `unmapped-inputs` still exits `0` with the |
| 81 | + same JSON, but `probe.log` has no `UnmappedInputFieldChecker` lines. |
| 82 | +- `02-typo` errors because the scenario uses the default `ObjectMapper`, which fails on unknown properties. It doesn't |
| 83 | + cover a mapper that ignores them. |
| 84 | +- The type variable fix comes from PR #882. On a checkout without it, `generic-input-resolver` still builds and exits |
| 85 | + `0`, but `02-generic.json` has `data: null` and an error containing `TypeVariableImpl cannot be cast to class |
| 86 | + java.lang.Class`. |
| 87 | +- The nested list fix comes from PR #891. On a checkout without it, `scalar-args` still exits `0`, but |
| 88 | + `03-nested-sets.json` has an error containing `ArrayList cannot be cast to class java.util.Set`. |
| 89 | +- The fix for a Kotlin scalar class without visible properties comes from issue #926. On a checkout without it, |
| 90 | + `05-empty-kotlin-scalar.json` has `data.tokenInput = "token default"`, since Jackson wrote the value as `{}` and read it |
| 91 | + back as a new instance. The same scalar written with a type id or an object id comes from issue #932. On a checkout |
| 92 | + without it, `07-optional-marked-empty-kotlin-scalars.json` has no errors and `"typed default"` and `"id default"`. The probe doesn't cover an `ObjectNode` parameter, which needs Jackson 3 on the probe's compile |
| 93 | + path. `CustomScalarInputTest` does. |
| 94 | +- `recursive-inputs` registers no directive wiring. `wiring-shapes` in `directives-and-relay.md` covers wirings on a |
| 95 | + recursive input. |
0 commit comments