@aaxis/lokka-transport-http 中文文档教程

发布于 5年前 浏览 18 项目主页 更新于 3年前

@aaxis/lokka-transport-http

具有 Expose 句柄请求和响应的同构 HTTP 传输层


这是 Lokka 的兼容传输层。

Basic Usage

安装包:

npm i --save @aaxis/lokka-transport-http
npm i --save lokka
import HttpTransport from '@aaxis/lokka-transport-http';
const transport = new HttpTransport('http://graphql-swapi.parseapp.com/');
transport.send(`
    {
      allFilms {
        films {
          title
        }
      }
    }
`).then(response => {
    console.log(JSON.stringify(response, null, 2));
});

Send Custom Request & Response

可以像这样发送自定义标头:

const headers = {
    'my-headers': 'some-value'
};
const transport = new HttpTransport('/graphql', {
    headers,
    buildOptions: () => {
      // build you custom header here
    },
    formatResponse: (response) => {
      // format you response here
    }
})

Authentication

此包不为您处理身份验证信息。 但它会让您与应用程序现有的身份验证机制进行交互。

  • If you already have an authorized cookie, it'll be sent with the HTTP request. (supports CORS)
  • You can also set a custom Authorization header to implement basic-auth support.

Error Handling

默认情况下,它将使用第一个 GraphQL 错误创建并抛出一个新的 Error 对象。 可以使用 handleErrors 选项自定义错误处理。 查看 lib/index.js 中的默认错误处理程序作为示例。

@aaxis/lokka-transport-http

Isomorphic HTTP Transport Layer with Expose handle Request and Response


This is a compatible transport layer for Lokka.

Basic Usage

Install the package:

npm i --save @aaxis/lokka-transport-http
npm i --save lokka
import HttpTransport from '@aaxis/lokka-transport-http';
const transport = new HttpTransport('http://graphql-swapi.parseapp.com/');
transport.send(`
    {
      allFilms {
        films {
          title
        }
      }
    }
`).then(response => {
    console.log(JSON.stringify(response, null, 2));
});

Send Custom Request & Response

It's possible to send custom headers like this:

const headers = {
    'my-headers': 'some-value'
};
const transport = new HttpTransport('/graphql', {
    headers,
    buildOptions: () => {
      // build you custom header here
    },
    formatResponse: (response) => {
      // format you response here
    }
})

Authentication

This package does not handle authentication information for you. But it'll let you interact with your app's existing authentication mechanism.

  • If you already have an authorized cookie, it'll be sent with the HTTP request. (supports CORS)
  • You can also set a custom Authorization header to implement basic-auth support.

Error Handling

By default it will create and throw a new Error object using the first GraphQL error. Error handling can be customized with the handleErrors option. Check the deafult error handler in lib/index.js for an example.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文