mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-18 23:46:21 +02:00
c1cda2b113
* DOCS: Add new graphql 4 docs * Reorganise docs * Docs done * Basic graphql index page * TOC for getting started * show folders on graphql index page * Add middleware note * Docs update * Update docs to reflect flushless schema * Docs updates * Docs for getByLink * Query caching docs * Docs on nested operations * update docs for new graphql dev admin * Docs for configurable operations * Replace readSiteTrees with readPages * Schema defaults docs * Docs for inherited plugins * Docs for customising * * Docs for field whitelisting * Change whitelist word * New docs on modelConfig * Document dev/build extension * Document default/global plugins * Document new input type fields config * Apply suggestions from code review Co-authored-by: Andre Kiste <bergice@users.noreply.github.com> * Note about when procedural schema gets built * Fix link * Apply suggestions from code review Co-authored-by: Andre Kiste <bergice@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Andre Kiste <bergice@users.noreply.github.com> * DOCS Note about plugins in custom queries * DOCS Note about filter and custom resolvers * DOCS Note about canview paging * DOCS Updated guidance on _extend See https://github.com/silverstripe/silverstripe-graphql/issues/296 * Apply suggestions from code review Co-authored-by: Andre Kiste <bergice@users.noreply.github.com> * DOCS Pre-release warning Co-authored-by: Ingo Schommer <ingo@silverstripe.com> Co-authored-by: Andre Kiste <bergice@users.noreply.github.com> Co-authored-by: Ingo Schommer <me@chillu.com>
78 lines
2.2 KiB
Markdown
78 lines
2.2 KiB
Markdown
---
|
|
title: Adding arguments
|
|
summary: Add arguments to your fields, queries, and mutations
|
|
---
|
|
|
|
# Working with generic types
|
|
|
|
[CHILDREN asList]
|
|
|
|
[alert]
|
|
You are viewing docs for a pre-release version of silverstripe/graphql (4.x).
|
|
Help us improve it by joining #graphql on the [Community Slack](https://www.silverstripe.org/blog/community-slack-channel/),
|
|
and report any issues at [github.com/silverstripe/silverstripe-graphql](https://github.com/silverstripe/silverstripe-graphql).
|
|
Docs for the current stable version (3.x) can be found
|
|
[here](https://github.com/silverstripe/silverstripe-graphql/tree/3)
|
|
[/alert]
|
|
|
|
## Adding arguments
|
|
|
|
Fields can have arguments, and queries are just fields, so let's add a simple
|
|
way of influencing our query response:
|
|
|
|
**app/_graphql/schema.yml**
|
|
```yaml
|
|
queries:
|
|
'readCountries(limit: Int!)': '[Country]'
|
|
```
|
|
|
|
We've provided the required argument `limit` to the query, which will allow us to truncate the results.
|
|
Let's update the resolver accordingly.
|
|
|
|
```php
|
|
public static function resolveReadCountries($obj, array $args = [])
|
|
{
|
|
$limit = $args['limit'];
|
|
$results = [];
|
|
$countries = Injector::inst()->get(Locales::class)->getCountries();
|
|
$countries = array_slice($countries, 0, $limit);
|
|
foreach ($countries as $code => $name) {
|
|
$results[] = [
|
|
'code' => $code,
|
|
'name' => $name
|
|
];
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
```
|
|
|
|
Now let's try our query again. This time, notice that the IDE is telling us we're missing a required argument.
|
|
|
|
```graphql
|
|
query {
|
|
readCountries(limit: 5) {
|
|
name
|
|
code
|
|
}
|
|
}
|
|
```
|
|
|
|
This works pretty well, but maybe it's a bit over the top to *require* the `limit` argument. We want to optimise
|
|
performance, but we also don't want to burden the developer with tedium like this. Let's give it a default value.
|
|
|
|
**app/_graphql/schema.yml**
|
|
```yaml
|
|
queries:
|
|
'readCountries(limit: Int = 20)': '[Country]'
|
|
```
|
|
|
|
Rebuild the schema, and notice that the IDE is no longer yelling at you for a `limit` argument.
|
|
|
|
Let's take this a step further by turning this in to a proper [paginated result](adding_pagination).
|
|
|
|
### Further reading
|
|
|
|
[CHILDREN]
|