shurcooL / graphql

Package graphql provides a GraphQL client implementation.
MIT License
709 stars 282 forks source link

How to insert an object using mutation #37

Open cedricve opened 5 years ago

cedricve commented 5 years ago

Hello,

Thank you for this amazing library, I'm trying to insert an object by executing an insert mutation.

So what I have is a simple table called events, with two properties id, event. So what I try to do is create an object with these two properties and insert it. However I'm not sure how to do it? Can someone have a quick look?

        listEvents := []*Events{}
        listEvents = append(listEvents, &Events{
            Event:     graphql.String("test event"),
        })

        var mutation struct {
            CreateEvents struct {
                Event string
            } `graphql:"CreateEvents(events:$events)"`
        }
        variables := map[string]interface{}{
            "event": listEvents,
        }

        if err := client.Mutate(context.Background(),&mutation, variables); err != nil {
            fmt.Println(err)
        }

The error I get is this..

non-200 OK status code: 400 Bad Request body: "{\"errors\":[{\"extensions\":{\"path\":\"$\",\"code\":\"validation-failed\"},\"message\":\"variables can only be defined on input types(enums, scC02XL1E5JHD3:graphC02XL1E5JHD3:grC02XL1C02XL1C02XL1E5JHD3C02XL1E5JC02C02XL1C02C02XL1E5JC02XL1E5JHD3:grC02XL1E5JHD3:grC02XL1C02C02XL1E5JHD3:grC02XL1C02XL1C02XL1E5JHD3C02XL1E5JHD3C02C02XL1E5JHD3C02
dmitshur commented 5 years ago

What GraphQL server are you using? Can we reproduce this error somehow?

It looks like the GraphQL server is returning an error "variables can only be defined on input types". Googling that error message gets me here: https://github.com/hasura/graphql-engine/blob/master/server/src-lib/Hasura/GraphQL/Validate.hs#L95. Is that the GraphQL server you're using? Perhaps you can try to ask there under which conditions that error is expected and to clarify the error message.

From what I see so far, the GraphQL client is working as intended. At least I'm not seeing a problem.