Skip to content

Commit

Permalink
Merge pull request #4656 from DimuthuMadushan/gql-contraint-bbe
Browse files Browse the repository at this point in the history
Add GraphQL Input Constraint Validation BBE
  • Loading branch information
praneesha authored Jul 6, 2023
2 parents 07d037d + be27ec1 commit 1b9a5b3
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 0 deletions.
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;
}
}
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}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
name(profile: {name: "Harry Potter", age: -4, height: 6})
}
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)
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ bal run graphql_input_constraint_validation.bal
8 changes: 8 additions & 0 deletions examples/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -2459,6 +2459,14 @@
"disablePlayground": true,
"isLearnByExample": false
},
{
"name": "Input constraint validation",
"url": "graphql-input-constraint-validation",
"verifyBuild": true,
"verifyOutput": false,
"disablePlayground": true,
"isLearnByExample": false
},
{
"name": "File upload",
"url": "graphql-file-upload",
Expand Down

0 comments on commit 1b9a5b3

Please sign in to comment.