Apollo-link-rest: Ability to pass data to a POST request

Created on 16 Jun 2018  ·  3Comments  ·  Source: apollographql/apollo-link-rest

I have an existing REST API that requires data to be posted as JSON. It does not accept url parameters. I have looked into the documentation, but doesn't seem to talk about this. Is this supported?

Here is an example of what I am trying to accomplish:

curl -X POST http://localhost:8000/rest-auth/login/ -d '{}'

Thanks in advance!

question❔

Most helpful comment

You’re right, bodyKey/input are what you need.

First: you’re passing input to the directive, so you probably wanted:

mutation login ($username: String!, $password: String!) {
  login(input: {username: $username, password: $password}) @rest(
    method: "POST",
    endpoint: "default",
    path: "rest-auth/login/", 
  ) {
    results
  }
}

But that might not work, I think we have a bug, where the “input” can’t be constructed dynamically like you are doing. (I’d love to get that bug fixed)

Try:

input LoginInput { # you can omit this whole declaration with ApolloLink Rest
  username: String!
  password: String!
}

mutation login(input: LoginInput!) {
  login(input: $input) @rest(
    method: "POST",
    endpoint: "default", # if you only have 1 endpoint, you can omit this too if you create your rest link the right way
    path: "rest-auth/login/", 
  ) {
    results
  }
}

This last example is basically what my app has.

All 3 comments

I think bodyKey/bodyBuilder is what I was looking for.

I have been trying to do something like this:

mutation login ($username: String!, $password: String!) {
  login @rest(
    method: "POST",
    endpoint: "default",
    path: "rest-auth/login/", 
    input: {username: $username, password: $password}
  ) {
    results
  }
}

But I continue to get this error:

[GraphQL mutation using a REST call without a body]. No `input` was detected. Pass bodyKey, or bodyBuilder to the @rest() directive to resolve this.

My Rest Link looks like this:

export const restLink = new RestLink({
  endpoints: {
    default: process.env.REACT_APP_HOST_URL,
  },
  headers: {
    "Content-Type": "application/json"
  },
  credentials: "same-origin",
})

My Client looks like this:

const client = new ApolloClient({
  link: ApolloLink.from([ stateLink, restLink ]),
  cache: cache,
  defaultOptions,
})

You’re right, bodyKey/input are what you need.

First: you’re passing input to the directive, so you probably wanted:

mutation login ($username: String!, $password: String!) {
  login(input: {username: $username, password: $password}) @rest(
    method: "POST",
    endpoint: "default",
    path: "rest-auth/login/", 
  ) {
    results
  }
}

But that might not work, I think we have a bug, where the “input” can’t be constructed dynamically like you are doing. (I’d love to get that bug fixed)

Try:

input LoginInput { # you can omit this whole declaration with ApolloLink Rest
  username: String!
  password: String!
}

mutation login(input: LoginInput!) {
  login(input: $input) @rest(
    method: "POST",
    endpoint: "default", # if you only have 1 endpoint, you can omit this too if you create your rest link the right way
    path: "rest-auth/login/", 
  ) {
    results
  }
}

This last example is basically what my app has.

@fbartho - thanks! This clarified a lot. You are right, I was accidentally putting the input into the directive.

Was this page helpful?
0 / 5 - 0 ratings