https://gqlgen.com

Gqlgen Custom Data Validation — Part 1

In this article, we will create our custom data validation for the gqlgen library.

Why Gqlgen?

Gqlgen is a powerful library to generate a graphql API architecture.

  • Schema First, which will help us generate code from Graphql Schema that we write before
  • Type Safety; we will rarely see map[string]interface{}; most of the responses will use a model
  • Codegen, most of the repeating part will be generated so that we only focus on the important part of the code

Libraries

Get Started

Repo: https://github.com/david-yappeter/gqlgen-validation-example

# Initialize
go mod init myapp
go get github.com/99designs/gqlgen
go get github.com/go-playground/validator
# gqlgen init
go run github.com/99designs/gqlgen init
directory after gqlgen init
  • gqlgen.yml: config file for gqlgen generator
  • graph/*.graphqls: Pre-written GraphQL schema (user-defined)
  • graph/*.resolvers.go: output of generated GraphQL schema
  • graph/model/models_gen.go: All model will be generated inside this file
  • graph/generated/generated.go: repeating action that generated by gqlgen

Define and Generating Our Schema

graph/schema.graphqls

package graph//go:generate go run github.com/99designs/gqlgen// This file will not be regenerated automatically.//// It serves as dependency injection for your app, add any dependencies you require here.type Resolver struct{}
go generate ./...
after go generate ./…

Create User Service

service/user.go :

Testing Our Code

Until this point, we will test our code without a validation yet.

go run server.go# output:
# connect to http://localhost:8080/ for GraphQL playground
get all users (empty)
create first user
create second user
get all users ()

Custom Validation with Directives

Gqlgen supports Custom Directives which allow us to create our data validation for an input.

...
directive @goField(forceResolver: String, name: String) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION
directive @binding(constraint: String!) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
...
input NewUser {
name: String! @binding(constraint: "required,max=20")
email: String! @binding(constraint: "required,email")
age: Int! @binding(constraint: "gte=0,lte=128")
country_id: String! @binding(constraint: "required,iso3166_1_alpha2")
}
name ‘required’ failed
age greater than equal ‘0’ failed
age ‘gte’ failed
name ‘required’ failed
name ‘max’ failed
email ‘email’ failed (custom message)

--

--

Web dev 👨‍💻

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store