// Graphql Execute a graphql request
Graphql Execute a graphql request
// Graphql Execute a graphql request
func (pql *GraphQLClient) Do(greq GraphQLRequest) (result map[string]interface{}, err error) {
	var b bytes.Buffer
	w := multipart.NewWriter(&b)
	if greq.Files != nil {
		for _, file := range greq.Files {
			var fw io.Writer
			fw, err = w.CreateFormFile(file.FieldName, file.Name)
			if err != nil {
				return
			}
			if _, err = io.Copy(fw, file.Source); err != nil {
				return
			}
		}
	}
	err = w.WriteField("query", greq.Query)
	if err != nil {
		return
	}
	if greq.Variables != nil {
		var data []byte
		data, err = json.Marshal(greq.Variables)
		if err != nil {
			return
		}
		err = w.WriteField("variables", string(data))
		if err != nil {
			return
		}
	}
	err = w.WriteField("operation", greq.Operation)
	if err != nil {
		return
	}
	w.Close()
	v := url.Values{}
	token := ""
	token, err = pql.getToken()
	v.Set("access_token", token)
	url := pql.EndPoint + "?" + v.Encode()
	req, err := http.NewRequest("POST", url, &b)
	if err != nil {
		return
	}
	req.Header.Set("Content-Type", w.FormDataContentType())
	res, err := pql.HTTPClient.Do(req)
	if err != nil {
		return
	}
	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return
	}
	err = json.Unmarshal(body, &result)
	if err != nil {
		return
	}
	return
}
func ( pql * GraphQLClient ) Do ( greq GraphQLRequest ) ( result map [ string ] interface { } , err error ) { var b bytes . Buffer w := multipart . NewWriter ( & b ) if greq . Files != nil { for _ , file := range greq . Files { var fw io . Writer fw , err = w . CreateFormFile ( file . FieldName , file . Name ) if err != nil { return } if _ , err = io . Copy ( fw , file . Source ) ; err != nil { return } } } err = w . WriteField ( " " , greq . Query ) if err != nil { return } if greq . Variables != nil { var data [ ] byte data , err = json . Marshal ( greq . Variables ) if err != nil { return } err = w . WriteField ( " " , string ( data ) ) if err != nil { return } } err = w . WriteField ( " " , greq . Operation ) if err != nil { return } w . Close ( ) v := url . Values { } token := " " token , err = pql . getToken ( ) v . Set ( " " , token ) url := pql . EndPoint + " " + v . Encode ( ) req , err := http . NewRequest ( " " , url , & b ) if err != nil { return } req . Header . Set ( " " , w . FormDataContentType ( ) ) res , err := pql . HTTPClient . Do ( req ) if err != nil { return } body , err := ioutil . ReadAll ( res . Body ) if err != nil { return } err = json . Unmarshal ( body , & result ) if err != nil { return } return }
Graphql Execute a graphql request
Graphql Execute a graphql request
// Prepare a form that you will submit to that URL. // Add your image file // Add the query field // Add the variables field // Add the operation field // Don't forget to close the multipart writer. // If you don't close it, your request will be missing the terminating boundary. // Now that you have a form, you can submit it to your handler. // Don't forget to set the content type, this will contain the boundary. // Submit the request