Custom Mutation returning 502 Error

“Evaluation of custom field failed because external request returned an error: unexpected status code: 502 for field: MyMutation within type: Mutation.”

My types here are pretty basic:

type MyResponse @remote {
  myVar1: String
  myVar2: DateTime
}

type Mutation {
  MyMutation(field1: String!, field2: String!): MyResponse @custom(http:{
    url: "http://mydomain/my/secret/script"
    method: "POST"
    body: "{ field1: $field1, field2: $field2}"
  })
}

I have tested my script with Insomnia and can get a response in the form of MyResponse above by doing a post request with a JSON body and a Content-Type:application/json header.

I assume that Dgraph makes the request taking my body and POSTing it with the application/json header. I am not sur ewhat else I am missing from here.

This would help bring our authentication script into the same domain with our API.

Running v2.0.0-rc1-518-g3c710183a which was the latest master branch from 7/15 I believe

Can’t say anything from 502.

No, that header is not set by dgraph. I think we should add it because we only support JSON payloads in custom HTTP requests anyways.

But, just to check, Can you once try the request from Insomnia/Postman without the Content-type header, and see if it works. If that fails, you may need to modify your schema like this for now:

type Mutation {
  MyMutation(field1: String!, field2: String!): MyResponse @custom(http:{
    url: "http://mydomain/my/secret/script"
    method: "POST"
    body: "{ field1: $field1, field2: $field2}"
    forwardHeaders: ["X-Forward-Content:Content-type"]
  })
}

And then send a mutation request to Dgraph with X-forward-Content header set to "application/json". And it would be forwarded to your script as Content-type header.

I can’t figure out how this could be related to 502 though. Are there any network limitations on the machine you are running dgraph? Also, are you running Insomnia on the same machine as dgraph? If not, can you try that?

1 Like

yeah, without that header from my local machine I get 502 Bad Gateway.

I cannot run insomnia on my dgraph machine because it is actually a docker machine running in an ec2 instance on AWS. I can probably get around this by the time being by doing the additional headers with the original request and forwarding headers.

Feature Request: Can we add a headers parameter in the custom directive so that we can send custom headers without them needing to be set by the original request.

Overview: 1) Client makes standard graphql request with no special headers. 2) Dgraph receives the request, maps it to the custom mutation/query/field. 3) Dgraph adds headers and sends it to the url.

type Mutation {
  MyMutation(field1: String!, field2: String!): MyResponse @custom(http:{
    url: "http://mydomain/my/secret/script"
    method: "POST"
    body: "{ field1: $field1, field2: $field2}"
    headers: ["Content-type:application/json"]
  })
}

We already have something like this. We call it secretHeaders. Here it is:

type Mutation {
  MyMutation(field1: String!, field2: String!): MyResponse @custom(http:{
    url: "http://mydomain/my/secret/script"
    method: "POST"
    body: "{ field1: $field1, field2: $field2}"
    secretHeaders: ["Content-type"]
  })
}

# Dgraph.Secret Content-type "application/json"

It was initially designed to store secrets in the schema. But, it could be used here as well :smiley:. This should work for you until we don’t send this header automatically.

Sorry, we don’t have any docs up for it yet.

2 Likes

Sweet, the hidden features, lol.

This was the solution that was needed! My custom mutation now works and I can use my Authentication script from within my graphql endpoint! Thanks @abhimanyusinghgaur

2 Likes