Calling a 8base graphql mutation from a standard HTTP post using Apex

Hi,

I’m trying to determine if we can use an external system (Salesforce) to send mutations into our 8base Custom Function resolvers and am having a bit of trouble.

The call is a basic post in which I’m sending graphql string in the body, like so:

'{"query": "mutation { anotherCustomResolver(foo:\"testing foo\") { result } }"}'

Unfortunately, all I’m getting back is the standard ValidationError:

{"message":"The request is invalid.","code":"ValidationError","details":{"body":"Syntax error, malformed JSON"}}

This query works in the API Explorer:

mutation {
  anotherCustomResolver(foo:"testing foo") {
    result
  }
}

I tried a simple query (not a mutation) using the same method from Salesforce and it worked as expected:

'{ "query": "{ applicantsList { items { firstName emailAddress } } }" }'

Am I missing something about how mutations needed to be formatted to work with 8base Custom Function resolvers?

Thanks.

Hey @evan.mcdaniel - I’m curious as to whether the error you’re getting is being caused by the response JSON object rather than the API call that you are making.

Have you been able to make the call using a GraphQL client like Postman or GraphiQL?

Hey @sebastian.scholl, thanks for getting back to me on this. I have been able to make the call using GraphiQL and other clients.

However, looking into the custom function logs in 8base I realized that the function was never getting called on your end, which made me realize that the parsing error is with the request itself.

With that in mind I rewrote the apex logic using the built in JSONGenerator class (a real PIA trust me) and it’s now working fine.

Happy to post the Apex code if your interested.

Would love to see that! Please do share here.

Sure. Here you go:

HttpRequest req = new HttpRequest();
req.setEndPoint('https://api.8base.com/<myEndpointId>');
req.setMethod('POST');
req.setHeader('Accept', 'application/json');
req.setHeader('Content-Type', 'application/json');
req.setHeader('Authorization', 'Bearer <myAPIToken>');

JSONGenerator jsonBody = JSON.createGenerator(true);
jsonBody.writeStartObject();
jsonBody.writeStringField('query', 'mutation { anotherCustomResolver(foo:"testing foobar") { result } }');
jsonBody.writeEndObject();

req.setBody(jsonBody.getAsString());

Http http = new Http();
HttpResponse res = http.send(req);
System.debug('Log: res.getBody(): ' + res.getBody());
1 Like