Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ThundR67/straf
Convert Golang Struct To GraphQL Object On The Fly
https://github.com/ThundR67/straf
go golang graphql graphql-query graphql-schema struct structs structtographql
Last synced: about 2 months ago
JSON representation
Convert Golang Struct To GraphQL Object On The Fly
- Host: GitHub
- URL: https://github.com/ThundR67/straf
- Owner: ThundR67
- License: mit
- Created: 2019-08-16T13:31:39.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-05-16T13:22:22.000Z (over 4 years ago)
- Last Synced: 2024-04-13T20:15:49.484Z (8 months ago)
- Topics: go, golang, graphql, graphql-query, graphql-schema, struct, structs, structtographql
- Language: Go
- Size: 27.3 KB
- Stars: 36
- Watchers: 1
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - straf - Easily Convert Golang structs to GraphQL objects. (Query Language / HTTP Clients)
- awesome-go-extra - straf - 08-16T13:31:39Z|2020-05-16T13:22:22Z| (Query Language / HTTP Clients)
README
[![Go Report Card](https://goreportcard.com/badge/github.com/SonicRoshan/straf)](https://goreportcard.com/report/github.com/SonicRoshan/straf) [![GoDoc](https://godoc.org/github.com/SonicRoshan/straf?status.svg)](https://godoc.org/github.com/SonicRoshan/straf) [![GoCover](https://gocover.io/_badge/github.com/SonicRoshan/straf)](https://gocover.io/github.com/SonicRoshan/straf)
# Straf
1. Convert Golang Struct To GraphQL Object On The Fly
2. Easily Create GraphQL Schemas## Example
### Converting struct to GraphQL Object
```go
type UserExtra struct {
Age int `description:"Age of the user"` // You can use description struct tag to add description
Gender string `deprecationReason:"Some Reason"` // You can use deprecationReason tag to add a deprecation reason
}type User struct {
UserID int
Username string `unique:"true"` // You can use unique tag to define if a field would be unique
Extra UserExtra
Password string `exclude:"true"` // You can use exclude tag to exclude a field
}func main() {
// GetGraphQLObject will convert golang struct to a graphQL object
userType, err := straf.GetGraphQLObject(User{})// You can then use userType in your graphQL schema
}
```### Using The Schema Builder
```go
type User struct {
UserID int `isArg:"true"` // You can use isArg tag to define a field as a graphql argument
Username string `isArg:"true"`
}var database []User = []User{}
func main() {
// GetGraphQLObject will convert golang struct to a graphQL object
userType, err := straf.GetGraphQLObject(User{})builder := straf.NewSchemaBuilder(userType, User{})
builder.AddArgumentsFromStruct(object2{}) // You can use this function to add more arguments from a struct
builder.AddFunction("CreateUser",
"Adds a user to database",
func(params graphql.ResolveParams) (interface{}, error)) {
id := params.Args["UserID"]
username := params.Args["Username"]
database = append(database, User{UserID: id, Username: Username})
})
schema := builder.Schema
// You can then use this schema
}
```### Using Middleware In Schema Builder
```gofunc middleware(function func(graphql.ResolveParams) (interface{}, error), params graphql.ResolveParams) (interface{}, error) {
fmt.Println("This function will run as a middleware")
return function(params)
}func main() {
builder := straf.NewSchemaBuilder(userType, User{}, middleware)builder.AddFunction("SomeFunction",
"Does Something",
someFunction)// Here the middleware function would run everytime before someFunction. middleware function would act as a middleware to all functions added to schema builder.
}```
## Author
Roshan Jignesh Mehta - [email protected]