Graphiql is an interactive client working in GraphQL browser.
To use in a go project, you need to add an html page with several dependencies.
If you need to have the latest versions, it will help install the node and packages. The project will grow greatly. There is an option to collect HTML into one go file and pull up dependencies with cdn at startup.
My version is go-graphiql .
To work you need to add only:
http.HandleFunc("/cli", graphiql.ServeGraphiQL)
The result is:
In the browser, open the developer console and add the js code:
let q=`{getMessage}` let v={} let options = (query, variables={}) =>{ return { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ query, variables }), } }; let o = options(q, v) console.log(o) fetch("http://localhost:9000/graphql",o) .then(response=>response.json()) .then(console.log)
Result of performance:
q=`mutation { setMessage(msg: "Hello Habr") } ` v={} o = options(q, v) console.log(o) fetch("http://localhost:9000/graphql",o) .then(response=>response.json()) .then(console.log)
Result of performance:
The scheme of work describes the object:
schema, err := graphql.NewSchema(graphql.SchemaConfig{ Query: types.RootQuery, Mutation: types.RootMutation, })
Data retrieval
var RootQuery = graphql.NewObject(graphql.ObjectConfig{ Name: "RootQuery", Fields: graphql.Fields{ "getMessage": &graphql.Field{ Type: graphql.String, Resolve: func(p graphql.ResolveParams) (interface{}, error) { msg := logic.GetMessage() return msg, nil }, }, }, })
The Resolve function gets data for us. Data can be from any source from the Database to the micro controller
Data mutation
var RootMutation = graphql.NewObject(graphql.ObjectConfig{ Name: "RootMutation", Fields: graphql.Fields{ "setMessage": &graphql.Field{ Type: graphql.String, Args: graphql.FieldConfigArgument{ "msg": &graphql.ArgumentConfig{Type: graphql.NewNonNull(graphql.String)}, }, Resolve: func(p graphql.ResolveParams) (interface{}, error) { msg := p.Args["msg"].(string) logic.SetMessage(msg) return msg, nil }, }, }, })
Edit data is also using the Resolve function.
Resolve can also trigger light events or regulate temperature.
An interesting point is that the type of the returned data can also have graphql.Fields, which will also have their own Resolve functions.
There are some limitations (features) of data exchange.
Variables have default values in go
Int is 0, String = ""
In graphql, there may be a situation when a variable is not set.
Therefore, for such parameters, we use the variable reference
I will write how to make graphql api to the registry of open data.
Because programmers are the laziest people on the planet. We will do the API so as to do something smaller.
There is an interesting project that creates a scheme for the text.
func main() { s := ` schema { query: Query } type Query { hello: String! } ` schema := graphql.MustParseSchema(s, &query{}) http.Handle("/query", &relay.Handler{Schema: schema}) log.Fatal(http.ListenAndServe(":8080", nil)) }
github.com/graph-gophers/graphql-go
I will make the generator for fields on structures
did not think up how to construct requests the generator
Source: https://habr.com/ru/post/449286/
All Articles