All Projects → ThundR67 → straf

ThundR67 / straf

Licence: MIT license
Convert Golang Struct To GraphQL Object On The Fly

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to straf

graphql-api
GraphQL-Api is an opinionated Graphql framework for Rails that supports auto generating queries based on Active Record models and plain Ruby objects
Stars: ✭ 58 (+75.76%)
Mutual labels:  graphql-query, graphql-schema
go-httpheader
A Go library for encoding structs into Header fields.
Stars: ✭ 38 (+15.15%)
Mutual labels:  structs, struct
gqlc
GraphQL IDL Compiler
Stars: ✭ 23 (-30.3%)
Mutual labels:  graphql-schema
react-apollo-client-testing-example
A React with Apollo Client application with a mocked GraphQL server and tested Query and Mutation components.
Stars: ✭ 17 (-48.48%)
Mutual labels:  graphql-schema
python-cstruct
C-style structs for Python
Stars: ✭ 38 (+15.15%)
Mutual labels:  struct
schemaglue
Naturally breaks down your monolithic graphql schema into bits and pieces and then glue them back together.
Stars: ✭ 117 (+254.55%)
Mutual labels:  graphql-schema
govalidators
struct 验证器,内置大部分常用验证,可自定义
Stars: ✭ 56 (+69.7%)
Mutual labels:  struct
graphql-metadata
Annotate your graphql schema with lightweight directives
Stars: ✭ 28 (-15.15%)
Mutual labels:  graphql-schema
graphql-binding-yelp
Embed Yelp's GraphQL API into your server application
Stars: ✭ 11 (-66.67%)
Mutual labels:  graphql-schema
table
Produces a string that represents slice data in a text table, inspired by gajus/table.
Stars: ✭ 130 (+293.94%)
Mutual labels:  struct
graphql-markdown
Flexible GraphQL Documentation Generator (Markdown)
Stars: ✭ 74 (+124.24%)
Mutual labels:  graphql-schema
grpc-graphql-schema
Convert gRPC proto definition into GraphQL Schema
Stars: ✭ 32 (-3.03%)
Mutual labels:  graphql-schema
genstruct
Golang struct generator similar to mysql terminal
Stars: ✭ 18 (-45.45%)
Mutual labels:  struct
graphql-spotify
GraphQL Schema And Resolvers For Spotify Web API
Stars: ✭ 55 (+66.67%)
Mutual labels:  graphql-schema
girin
Define GraphQL types linked to classes.
Stars: ✭ 26 (-21.21%)
Mutual labels:  graphql-schema
faker
Random fake data and struct generator for Go.
Stars: ✭ 67 (+103.03%)
Mutual labels:  struct
graphql-compose-elasticsearch
Graphql App using Node with typescript, KOA framework and Elasticsearch
Stars: ✭ 40 (+21.21%)
Mutual labels:  graphql-schema
cstruct-go
a fast c-style struct packer & unpacker for golang
Stars: ✭ 28 (-15.15%)
Mutual labels:  struct
valify
Validates data in JavaScript in a very simple way
Stars: ✭ 13 (-60.61%)
Mutual labels:  struct
fauna-gql-upload
A tool for managing your FaunaDB database using files. Create resources such as functions by simply creating a new file.
Stars: ✭ 45 (+36.36%)
Mutual labels:  graphql-schema

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]

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].