Can anyone suggest a clean way to call GraphQL API using golang?

Currently, we are using the HTTP library for calling the SlashQL endpoint, but I find it messy to use.

Any other solution?

I use MachineBox’s GraphQL client library.

Not the best but flexible enough for me to do toy projects on. If you want something more industrial, you might have to write your own

Thanks for the response.
We want something industrial so I think we need to write our own wrapper using native http library.

Oh you should definitely try GitHub - shurcooL/graphql: Package graphql provides a GraphQL client implementation. first tho.

There’s also Hasura’s fork, which I have not used.

1 Like

I got to write a post about this and open source it, but we’re using GitHub - Yamashou/gqlgenc: This is Go library for building GraphQL client with gqlgen along with a custom gqlgen plugin which fetches the generated schema from the SlashQL backend and generates a typed Go client, and you get to use it like this:

uidFilter := slashModel.StringHashFilter{
        In: sliceutils.MapStrSliceToStrPointerSlice(opts.Filters.Ids),
      }
      f := slashModel.DetectorFilter{
        UID: &uidFilter,
      }

      res, err := s.slashClient.GraphQL().QueryDetectors(ctx, &f, nil, nil, nil)
      if err != nil {
        s.logger.Error("msg", "unable to list detectors. Received error from SlashQL",
          "err", err)
        return nil, errors.ErrGQL
      }

      detectors, err := s.FromGQLModel(res)
      if err != nil {
        return nil, err
      }

      r.Detectors = detectors
1 Like

Now you really gotta write that post. I so want to read it

Can you please give me code for this call in golang with these libraries. I have tried both of them shrucool/graphql and machineBox but somehow I am not able to run these kind of queries.

mutation addAuthor($author: [AddAuthorInput!]!) {
  addAuthor(input: $author) {
    author {
      id
      name
    }
  }
}