λ°μ΄ν°λ₯Ό JSONμΌλ‘ κ²μν΄μΌ νλ κΈ°μ‘΄ REST APIκ° μμ΅λλ€. url 맀κ°λ³μλ₯Ό νμ©νμ§ μμ΅λλ€. λλ λ¬Έμλ₯Ό μ‘°μ¬νμ§λ§ μ΄κ²μ λν΄ μ΄μΌκΈ°νμ§ μλ κ² κ°μ΅λλ€. μ§μλλμ?
λ€μμ λ΄κ° λ¬μ±νλ €λ κ²μ μμ λλ€.
curl -X POST http://localhost:8000/rest-auth/login/ -d '{}'
미리 κ°μ¬λ립λλ€!
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 - κ°μ¬ν©λλ€! μ΄κ²μ λ§μ κ²μ λͺ νν νμ΅λλ€. λ§μ΅λλ€. μ€μλ‘ μ§μλ¬Έμ μ λ ₯μ μ λ ₯νμ΅λλ€.
κ°μ₯ μ μ©ν λκΈ
λΉμ μ΄ μ³μ΅λλ€. bodyKey/inputμ λΉμ μ΄ νμλ‘ νλ κ²μ λλ€.
첫째: μ§μλ¬Έμ μ λ ₯μ μ λ¬νλ―λ‘ μλ§λ λ€μμ μνμ κ²μ λλ€.
νμ§λ§ μλνμ§ μμ μλ μμ΅λλ€. μ μκ°μλ "μ λ ₯"μ΄ μ¬λ¬λΆμ²λΌ λμ μΌλ‘ ꡬμ±λ μ μλ λ²κ·Έκ° μλ€κ³ μκ°ν©λλ€. (κ·Έ λ²κ·Έλ₯Ό μμ νκ³ μΆμ΅λλ€)
λ Έλ ₯νλ€:
μ΄ λ§μ§λ§ μλ κΈ°λ³Έμ μΌλ‘ λ΄ μ±μ μλ κ²μ λλ€.