Popularity
2.4
Growing
Activity
0.0
Stable
36
1
6

Programming language: Go
License: MIT License
Tags: Query Language    
Latest version: v2.0.0

straf alternatives and similar packages

Based on the "Query Language" category.
Alternatively, view straf alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of straf or a related project?

Add another 'Query Language' Package

README

Go Report Card GoDoc GoCover

Straf

  1. Convert Golang Struct To GraphQL Object On The Fly
  2. Easily Create GraphQL Schemas

Example

Converting struct to GraphQL Object

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

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


func 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]