Apollo-link-rest: القدرة على تمرير البيانات إلى طلب POST

تم إنشاؤها على ١٦ يونيو ٢٠١٨  ·  3تعليقات  ·  مصدر: apollographql/apollo-link-rest

لديّ واجهة برمجة تطبيقات REST الحالية تتطلب نشر البيانات على هيئة JSON. لا يقبل معلمات URL. لقد بحثت في الوثائق ، لكن لا يبدو أنني أتحدث عن ذلك. هل هذا مدعوم؟

هذا مثال على ما أحاول تحقيقه:

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

شكرا لك مقدما!

question❔

التعليق الأكثر فائدة

أنت على حق ، bodyKey / المدخلات هي ما تحتاجه.

أولاً: تقوم بتمرير المدخلات إلى التوجيه ، لذلك ربما تريد:

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.

يبدو My Rest Link كما يلي:

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 / المدخلات هي ما تحتاجه.

أولاً: تقوم بتمرير المدخلات إلى التوجيه ، لذلك ربما تريد:

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 التقييمات