Skip to content

Commit

Permalink
Added documentation for adding extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
mskacelik committed Feb 22, 2023
1 parent c5bfc57 commit 4a3bb19
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Documentation is built using the `mkdocs` tool.
See link:https://www.mkdocs.org/user-guide/installation/[Installation guide] on how to install it.
On top of that install these 3 plugins:
```
pip install mkdocs-material
pip install mkdocs-build-plantuml-plugin
pip install mkdocs-macros-plugin
```
Then, building the documentation locally will be as simple as running `mkdocs build` in the root
of this repository (NOT in the `docs` directory) - the output should appear in the `site` directory.
To fix a few things, like making links work correctly, you should serve the site
Expand Down
44 changes: 44 additions & 0 deletions docs/extensions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Adding extensions to GraphQL response
**SmallRye GraphQL** allows the user to add their own extensions inside the GraphQL response
which is received from the server. The `extensions` are stored next to the `data` field in the GraphQL response.

``` json
{
"data": {
"shirt": {
"size": "L"
}
},
"extensions": {
"country": "Germany",
"pi": 3.1415926535
}
}
```

To add extensions, you need to `@Inject` an instance of `SmallRyeContext` in your `@GraphQLApi` annotated class.
After that, you can add your own extensions via the method `addExtensions`
with its input parameters: `key : String` as an identification of the added extension.
And `value: Object` as a value of the extension. Value can be any given object that can be converted into JsonValue.

As an example, this class below contains the query `getShirt` and during the http request, the query adds these extensions:
`{"country": "Germany", "pi": 3.1415926535}`.
These extensions will be sent back via response in the `extensions` field.
```java
import jakarta.inject.Inject;

@GraphQLApi
public class ShirtResources {
@Inject
SmallRyeContext smallRyeContext;

@Query
public Shirt getShirt() {
smallRyeContext.addExtensions("country", "Germany");
smallRyeContext.addExtensions("pi", 3.1415926535);
//...
}
}
```
> [NOTE]
> You can also use the method `setAddedExtensions(Map<String, Object> addedExtensions)` to set all the extensions with map instance.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ nav:
- Directives: 'directives.md'
- Federation: 'federation.md'
- Custom error extensions: 'custom-error-extensions.md'
- Response extensions: 'extensions.md'
- Typesafe client:
- Basic usage: 'typesafe-client-usage.md'
- Reactive: 'typesafe-client-reactive-types.md'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,21 @@ public Map<String, Object> getAddedExtensions() {
return addedExtensions;
}

/**
* Sets the entire map of extension(s) into the context.
*
* @param addedExtensions The Map object containing extension(s).
*/
public void setAddedExtensions(Map<String, Object> addedExtensions) {
this.addedExtensions = addedExtensions;
}

/**
* Adds single instance of user created extension into the context.
*
* @param key The key (identification) of the extension.
* @param value The value of extension.
*/
public void addExtension(String key, Object value) {
addedExtensions.put(key, value);
}
Expand Down

0 comments on commit 4a3bb19

Please sign in to comment.