-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into doc/update-request
- Loading branch information
Showing
3 changed files
with
52 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,48 +4,52 @@ title: "@const" | |
|
||
The `@const` directive in GraphQL is a powerful tool for embedding data directly into your schema, offering two primary functionalities: | ||
|
||
1. **Static Response**: This feature allows for the inclusion of a constant response within the schema definition itself. It is useful for scenarios where the response is static and unchanging. e.g: | ||
|
||
```graphql | ||
schema { | ||
query: Query | ||
} | ||
|
||
type Query { | ||
user: User @const(data: {name: "John", age: 12}) | ||
} | ||
|
||
type User { | ||
name: String | ||
age: Int | ||
} | ||
``` | ||
|
||
The const operator also checks the provided value at compile time to ensure it matches the field's schema. If not, the console displays a descriptive error message. | ||
|
||
2. **Dynamic Template**: Beyond static data embedding, the `@const` directive extends its utility to support dynamic data injection through Mustache template syntax. This feature enables the use of placeholders within the constant data, which are then dynamically replaced with actual values at runtime. It supports both scalar values and complex objects, including lists and nested objects, offering flexibility in tailoring responses to specific needs. e.g: | ||
|
||
```graphql | ||
schema { | ||
query: Query | ||
} | ||
|
||
type Query { | ||
user: User @const(data: {name: "John", workEmail: "[email protected]", personalEmail: "[email protected]"}) | ||
} | ||
|
||
type User { | ||
name: String | ||
age: Int | ||
personalEmail: String | ||
workEmail: String | ||
emails: Emails @const(data: {emails: {workEmail: "{{value.workEmail}}", personalEmail: "{{value.personalEmail}}"}}) | ||
} | ||
|
||
type Emails { | ||
workEmail: String | ||
personalEmail: String | ||
} | ||
``` | ||
## Static | ||
|
||
This feature allows for the inclusion of a constant response within the schema definition itself. It is useful for scenarios where the response is unchanging. e.g: | ||
|
||
```graphql | ||
schema { | ||
query: Query | ||
} | ||
|
||
type Query { | ||
user: User @const(data: {name: "John", age: 12}) | ||
} | ||
|
||
type User { | ||
name: String | ||
age: Int | ||
} | ||
``` | ||
|
||
The const operator also checks the provided value at compile time to ensure it matches the field's schema. If not, the console displays a descriptive error message. | ||
|
||
## Dynamic | ||
|
||
Beyond static data embedding, the `@const` directive extends its utility to support dynamic data injection through Mustache template syntax. This feature enables the use of placeholders within the constant data, which are then dynamically replaced with actual values at runtime. It supports both scalar values and complex objects, including lists and nested objects, offering flexibility in tailoring responses to specific needs. e.g: | ||
|
||
```graphql | ||
schema { | ||
query: Query | ||
} | ||
|
||
type Query { | ||
user: User @const(data: {name: "John", workEmail: "[email protected]", personalEmail: "[email protected]"}) | ||
} | ||
|
||
type User { | ||
name: String | ||
age: Int | ||
personalEmail: String | ||
workEmail: String | ||
emails: Emails @const(data: {emails: {workEmail: "{{value.workEmail}}", personalEmail: "{{value.personalEmail}}"}}) | ||
} | ||
|
||
type Emails { | ||
workEmail: String | ||
personalEmail: String | ||
} | ||
``` | ||
|
||
In this example, the `@const` directive dynamically generate an `Emails` object based on the provided template data. The placeholders within the template (`{{value.workEmail}}` and `{{value.personalEmail}}`) gets replaced with the actual values specified in the `User` type, allowing for dynamic content generation while still adhering to the schema's structure. |
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