machinebox / graphql

Simple low-level GraphQL HTTP client for Go
https://blog.machinebox.io/a-graphql-client-library-for-go-5bffd0455878
Apache License 2.0
933 stars 217 forks source link

Question on mocking graphql.Newclient #36

Closed jasonmccallister closed 4 years ago

jasonmccallister commented 5 years ago

Thank you for this excellent package! Made writing some scripts with Go and our GraphQL API (specifically this GraphQL API which has been amazing https://github.com/hasura/graphql-engine).

We are using Hasura with Lambda event functions, which takes a events.APIGatewayProxyRequest. Since we are using the "serverless" functionality we decided to use Go for Lambda and started writing unit tests against our Hasura API.

My question is to make sure I am on the right track, I have used some of your test code to get myself pretty far, so thank you, but I am stuck on one specific problem. Here are the steps I have taken so far:

  1. Create a new test server
  2. Write the example JSON response
  3. Pass srv.URL to graphql.NewClient(srv.URL)

Here is a sample of the test:

srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    w.WriteHeader(http.StatusOK)
    io.WriteString(w, `{
    "data": {
        "insert_bookings": {
        "returning": [
            {
            "id": "TX123456789",
            "created_at": "2019-06-19T00:08:16.547491+00:00",
            "updated_at": "2019-06-19T00:08:16.547491+00:00"
            }
        ]
        },
        "insert_booking_events": {
        "returning": [
            {
            "id": "bdeee2fe-7318-46bd-9b16-6c47ba12129a",
            "vehicle_bookings_id": "TX123456789",
            "status": "completed",
            "created_at": "2019-06-19T00:08:16.547491+00:00",
            "updated_at": "2019-06-19T00:08:16.547491+00:00"
            }
        ]
        }
    }
    }`)
}))
defer srv.Close()

However, when running the tests, I am getting an error with unsupported protocol scheme "" that is coming from:

if err := client.Run(context.Background(), req, &resp); err != nil {
    log.Fatal(err)
}

Any help or pointers would be greatly appreciated!

aerostitch commented 5 years ago

Hi @jasonmccallister ,

This error means that the call has been done with a url missing the trailing http:// or https://. Are you sure you have the https:// in srv.URL? Did you print it to be sure? Did you try to enable the logs in the client? client.Log = func(s string) { log.Println(s) } (with client being your graphql client).

Note: I have no affiliation with this project, just a user.

Thanks Joseph

jasonmccallister commented 5 years ago

@aerostitch that makes sense, however, I double checked the srv.URL contains the protocol in the test as it comes from httptest.NewTSLServer(). I took the same approach on the tests provided in the package but could not figure it out.

aerostitch commented 5 years ago

Did you try with httptest.NewServer() instead of the TLS version? I use that in my tests and it works pretty well.

jasonmccallister commented 5 years ago

I did try that first, then I switched to TLS after. Also, I did not mention this in the original response but thank you for taking the time to respond!