-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4656 from DimuthuMadushan/gql-contraint-bbe
Add GraphQL Input Constraint Validation BBE
- Loading branch information
Showing
7 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
examples/graphql-input-constraint-validation/graphql_input_constraint_validation.bal
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,27 @@ | ||
import ballerina/constraint; | ||
import ballerina/graphql; | ||
|
||
public type Profile record {| | ||
// Define constraints for the fields | ||
@constraint:String { | ||
maxLength: 5 | ||
} | ||
string name; | ||
|
||
@constraint:Int { | ||
minValue: 0 | ||
} | ||
int age; | ||
|
||
@constraint:Float { | ||
maxValue: 3.0 | ||
} | ||
float height; | ||
|}; | ||
|
||
service /graphql on new graphql:Listener(9090) { | ||
|
||
resource function get name(Profile profile) returns string { | ||
return profile.name; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
examples/graphql-input-constraint-validation/graphql_input_constraint_validation.client.out
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,2 @@ | ||
curl -X POST -H "Content-type: application/json" -d '{ "query": "query{ name(profile:{name:\"Harry Potter\", age: -4, height:6})}" }' 'http://localhost:9090/graphql' | ||
{"errors":[{"message":"Input validation failed in the field \"name\": Validation failed for '$.age:minValue','$.height:maxValue','$.name:maxLength' constraint(s).", "locations":[{"line":1, "column":8}], "path":["name"]}], "data":null} |
3 changes: 3 additions & 0 deletions
3
examples/graphql-input-constraint-validation/graphql_input_constraint_validation.graphql
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,3 @@ | ||
{ | ||
name(profile: {name: "Harry Potter", age: -4, height: 6}) | ||
} |
25 changes: 25 additions & 0 deletions
25
...ples/graphql-input-constraint-validation/graphql_input_constraint_validation.md
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,25 @@ | ||
# GraphQL service - Input constraint validation | ||
|
||
GraphQL input constraint validation allows you to define and enforce constraints on input arguments. These constraints ensure that the provided input values meet certain criteria before they are processed. The input constraints can be configured using the `@constraint` annotation provided by the Ballerina constraint package. The constraint validation can be enabled or disabled using the `validation` field in the `graphql:ServiceConfig`. By default, the validation field is set to `true`. | ||
|
||
::: code graphql_input_constraint_validation.bal ::: | ||
|
||
Run the service by executing the following command. | ||
|
||
::: out graphql_input_constraint_validation.server.out ::: | ||
|
||
Send the following document to the GraphQL endpoint to test the service. | ||
|
||
::: code graphql_input_constraint_validation.graphql ::: | ||
|
||
To send the document, execute the following cURL command in a separate terminal. | ||
|
||
::: out graphql_input_constraint_validation.client.out ::: | ||
|
||
>**Tip:** You can invoke the above service via the [GraphQL client](/learn/by-example/graphql-client-query-endpoint/). | ||
## Related links | ||
- [Constraint annotation - API documentation](https://lib.ballerina.io/ballerina/constraint/latest#Annotations) | ||
- [GraphQL constraint config - API documentation](https://ballerina.io/spec/graphql/#1018-constraint-configurations) | ||
- [`constraint` module - API documentation](https://lib.ballerina.io/ballerina/constraint/latest) | ||
- [`graphql` module - API documentation](https://lib.ballerina.io/ballerina/graphql/latest) |
2 changes: 2 additions & 0 deletions
2
examples/graphql-input-constraint-validation/graphql_input_constraint_validation.metatags
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,2 @@ | ||
description: A GraphQL service endpoint written in Ballerina. | ||
keywords: ballerina, ballerina by example, bbe, graphql, graphql input constraint validation, graphql constraint validation |
1 change: 1 addition & 0 deletions
1
examples/graphql-input-constraint-validation/graphql_input_constraint_validation.server.out
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 @@ | ||
$ bal run graphql_input_constraint_validation.bal |
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