-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added documentation for adding extensions
- Loading branch information
Showing
4 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters