Isigar

GraphQL GO

Jan 10th, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.21 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "github.com/graphql-go/graphql"
  6.     "github.com/graphql-go/handler"
  7.     Mocks "graphql/mocks"
  8.     "graphql/models"
  9.     "graphql/resolvers"
  10.     "log"
  11.     "net/http"
  12.     "strings"
  13. )
  14.  
  15. var rootQuery = graphql.NewObject(graphql.ObjectConfig{
  16.     Name: "query",
  17.     Fields: graphql.Fields{
  18.         "users": &graphql.Field{
  19.             Type: graphql.NewList(models.UserSchema),
  20.             Args: graphql.FieldConfigArgument{
  21.                 "name": &graphql.ArgumentConfig{
  22.                     Type: graphql.NewNonNull(graphql.String),
  23.                     Description: "Some desc",
  24.                 },
  25.             },
  26.             Resolve: func(params graphql.ResolveParams) (interface{}, error){
  27.                 name := params.Args["name"].(string);
  28.                 filtered := resolvers.UserFilter(Mocks.Users(),func(v models.User){
  29.                     return strings.Contains(v.Name, name).(bool);
  30.                 });
  31.                 return filtered,nil
  32.             },
  33.         },
  34.     },
  35. })
  36.  
  37. func main() {
  38.     schema, err := graphql.NewSchema(graphql.SchemaConfig{
  39.         Query:rootQuery,
  40.     })
  41.  
  42.     if err != nil {
  43.         log.Fatalf("failed to create new schema! Error: %v",err)
  44.     }
  45.  
  46.     h := handler.New(&handler.Config{
  47.         Schema: &schema,
  48.         Pretty: true,
  49.         GraphiQL: true,
  50.     })
  51.     http.Handle("/graphql", h);
  52.     http.ListenAndServe(":9090",nil)
  53.     fmt.Println("Server is running at port 9090")
  54. }
Advertisement
Add Comment
Please, Sign In to add comment