main.go package main import ( "context" "encoding/json" "fmt" resolvers "github.com/Besufikad17/graphqldemo/resolvers" "github.com/graphql-go/graphql" "net/http" ) var Schema, _ = graphql.NewSchema(graphql.SchemaConfig{ Query: resolvers.QueryType, Mutation: resolvers.MutationType, }) func executeQuery(query string, schema graphql.Schema, token string) *graphql.Result { result := graphql.Do(graphql.Params{ Schema: schema, RequestString: query, Context: context.WithValue(context.Background(), "token", token), }) if len(result.Errors) > 0 { fmt.Printf("errors: %v", result.Errors) } return result } func enableCORS(w *http.ResponseWriter) { (*w).Header().Set("Access-Control-Allow-Origin", "*") } func main() { http.HandleFunc("/graphql", func(w http.ResponseWriter, r *http.Request) { enableCORS(&w) tokenString := r.Header.Get("Authorization") result := executeQuery(r.URL.Query().Get("query"), Schema, tokenString) json.NewEncoder(w).Encode(result) }) http.ListenAndServe(":8000", nil) }
This is not the web server considering the ports are very different
This is the main entry for my graphql server
Обсуждают сегодня