Apollo-link-rest: 能够将数据传递给 POST 请求

创建于 2018-06-16  ·  3评论  ·  资料来源: apollographql/apollo-link-rest

我有一个现有的 REST API,它需要将数据作为 JSON 发布。 它不接受 url 参数。 我查看了文档,但似乎没有谈论这个。 这是支持的吗?

这是我试图完成的一个例子:

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

提前致谢!

question❔

最有用的评论

你是对的,bodyKey/input 正是你所需要的。

首先:您将输入传递给指令,所以您可能想要:

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

但这可能不起作用,我认为我们有一个错误,其中“输入”不能像您正在做的那样动态构建。 (我很想修复那个错误)

尝试:

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
  }
}

最后一个例子基本上就是我的应用程序所拥有的。

所有3条评论

我认为bodyKey/bodyBuilder是我一直在寻找的。

我一直在尝试做这样的事情:

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

但我继续收到此错误:

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

我的休息链接看起来像这样:

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

我的客户看起来像这样:

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

你是对的,bodyKey/input 正是你所需要的。

首先:您将输入传递给指令,所以您可能想要:

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

但这可能不起作用,我认为我们有一个错误,其中“输入”不能像您正在做的那样动态构建。 (我很想修复那个错误)

尝试:

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
  }
}

最后一个例子基本上就是我的应用程序所拥有的。

@fbartho - 谢谢! 这澄清了很多。 你是对的,我不小心将输入放入指令中。

此页面是否有帮助?
0 / 5 - 0 等级