Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GraphQL Input Constraint Validation BBE #4656

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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