Apollo-link-rest: Error when running a mutation without any request body

Created on 28 Sep 2018  ·  5Comments  ·  Source: apollographql/apollo-link-rest

It seems that you can't make a PATCH or POST request without input?

Error: Error: [GraphQL mutation using a REST call without a body]. Noinputwas detected. Pass bodyKey, or bodyBuilder to the @rest() directive to resolve this.

Mutation:

mutation extendTrial($orgSlug: String!) {
  extendTrialResult(orgSlug: $orgSlug) @rest(
    type: "Org",
    path: "/orgs/{args.orgSlug}/extend_trial/",
    method: "PATCH",
    bodySerializer: "text"
  )
}

Apollo options:

bodySerializers: {
  text: (data, headers) => {
    headers.set('Content-Type', 'text/plain');
    return { body: data, headers };
  }
}

Most helpful comment

Actually for me input: null did not work, but this worked:

mutation extendTrial($orgSlug: String!) {
  extendTrialResult(orgSlug: $orgSlug, input: {}) @rest(
    type: "Org",
    path: "/orgs/{args.orgSlug}/extend_trial/",
    method: "PATCH",
    bodySerializer: "text"
  )
}

All 5 comments

I think you can do this:

mutation extendTrial($orgSlug: String!) {
  extendTrialResult(orgSlug: $orgSlug, input: null) @rest(
    type: "Org",
    path: "/orgs/{args.orgSlug}/extend_trial/",
    method: "PATCH",
    bodySerializer: "text"
  )
}

Actually for me input: null did not work, but this worked:

mutation extendTrial($orgSlug: String!) {
  extendTrialResult(orgSlug: $orgSlug, input: {}) @rest(
    type: "Org",
    path: "/orgs/{args.orgSlug}/extend_trial/",
    method: "PATCH",
    bodySerializer: "text"
  )
}

Is this not objectively horrible? Is there a way around this now? The documentation says:

When making a POST or PUT HTTP request, you often need to provide a request body. By convention, GraphQL recommends you name your input-types as input, so by default that's where we'll look to find a JSON object for your body.

Doesn't this sound as though we're aware of the fact that you don't always need a body?

Speaking as the the writer of that doc line, yeah, I’m aware. This just hasn’t been that high priority for my use cases.

@d9001 If you look at @Laruxo‘s workaround, you can see that all you need to do is add a few letters to bypass this constraint. If you want to contribute code for this feature I’d be happy to help review it.

Yeah it's not that I couldn't get it to work. It just drove me nuts figuring out what the issue was, and the solution is kinda meh as called out in my pull requests where everyone told me to remove the unused object :|

I'll look at what it would take to remove that requirement.

Was this page helpful?
0 / 5 - 0 ratings