- Notifications
You must be signed in to change notification settings - Fork6
Convert Golang Struct To GraphQL Object On The Fly
License
NotificationsYou must be signed in to change notification settings
ThundR67/straf
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
- Convert Golang Struct To GraphQL Object On The Fly
- Easily Create GraphQL Schemas
typeUserExtrastruct {Ageint`description:"Age of the user"`// You can use description struct tag to add descriptionGenderstring`deprecationReason:"Some Reason"`// You can use deprecationReason tag to add a deprecation reason}typeUserstruct {UserIDintUsernamestring`unique:"true"`// You can use unique tag to define if a field would be uniqueExtraUserExtraPasswordstring`exclude:"true"`// You can use exclude tag to exclude a field}funcmain() {// GetGraphQLObject will convert golang struct to a graphQL objectuserType,err:=straf.GetGraphQLObject(User{})// You can then use userType in your graphQL schema}
typeUserstruct {UserIDint`isArg:"true"`// You can use isArg tag to define a field as a graphql argumentUsernamestring`isArg:"true"`}vardatabase []User= []User{}funcmain() {// GetGraphQLObject will convert golang struct to a graphQL objectuserType,err:=straf.GetGraphQLObject(User{})builder:=straf.NewSchemaBuilder(userType,User{})builder.AddArgumentsFromStruct(object2{})// You can use this function to add more arguments from a structbuilder.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}
funcmiddleware(functionfunc(graphql.ResolveParams) (interface{},error),params graphql.ResolveParams) (interface{},error) {fmt.Println("This function will run as a middleware")returnfunction(params)}funcmain() {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.}
Roshan Jignesh Mehta -sonicroshan122@gmail.com
About
Convert Golang Struct To GraphQL Object On The Fly
Topics
Resources
License
Stars
Watchers
Forks
Packages0
No packages published