@acusti/appsync-fetch 中文文档教程
@acusti/appsync-fetch
appsync-fetch
是一个简单的节点和浏览器兼容包 导出包装的 appSyncFetch
函数的最小依赖项 fetch 向 AWS AppSync graphql API 发出请求。 它需要一个 用于传递 AWS 凭据的可选第三个参数,以及 地区。 如果未提供 AWS 凭证,它将使用 @aws-sdk/credential-providers
的 < code>fromEnv helper 获取 来自标准 AWS 环境变量的凭证在 拉姆达。 然后,它使用这些凭据来构建适当的 [AWS SigV4][] 用于基于 IAM 授权的授权标头。
[aws sigv4]: https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html
Usage
npm install @acusti/appsync-fetch
# or
yarn add @acusti/uniquify
该包导出 appSyncFetch
,它采用与 window.fetch
,但有一些便利内置。 开始 appSyncFetch
将设置所有必需的标头,包括 AWS 授权标头、日期标头和内容类型。 它还将设置 method: 'POST'
(所有 GraphQL 请求都需要)。
此外,第二个参数(在 window.fetch 文档中名为 init
) 可以采用 query: string
属性和 variables: object
属性, 它会将 JSON.stringify 转换为有效的 GraphQL 请求正文。 你可以 也可以直接将body
作为字符串传入,但是如果传入query
,则 body
将被覆盖(如果你 尝试通过两者)。
该函数还带有一个可选的第三个参数,您可以在其中手动 如果您不想依赖内置的,请传入 AWS 凭据 凭据处理,将从环境中提取凭据 通过 process.env
变量。 代码示例中说明了用法 以下。
最后,appSyncFetch
调用 await response.json()
并返回 结果,因为无论如何这就是你想要的。
import { appSyncFetch } from '@acusti/appsync-fetch';
const appSyncURL =
'https://abcdefghijklmnopqrstuvwxyz.appsync-api.us-west-2.amazonaws.com/graphql';
// In its simplest usage, environment variables are used for authorization
const itemsResult = await appSyncFetch(appSyncURL, {
query: `
query ListItems {
listItems {
items {
id
text
}
}
}`,
});
// itemsResult is the parsed JSON from the response, e.g.:
// const response = await fetch(...);
// const itemsResult = await response.json();
// You can also pass in variables
const createdItemResult = await appSyncFetch(appSyncURL, {
query: `
mutation CreateItem($input: CreateItemInput!) {
createItem(input: $input) {
id
}
}`,
variables: {
input: {
text: 'Here is the text of a new item',
},
},
});
// You can also provide the authentication variables manually
const manualAuthenticationResult = await appSyncFetch(
appSyncURL,
{ query: 'query {...}' },
{
accessKeyId,
secretAccessKey,
sessionToken,
},
);
// By default, appSyncFetch will extract the amazon region from the AppSync URL
// You can override that by passing in a region via the optional 3rd argument
// The full type for the
@acusti/appsync-fetch
appsync-fetch
is a simple node and browser-compatible package with minimal dependencies that exports an appSyncFetch
function that wraps fetch to make requests to an AWS AppSync graphql API. It takes an optional third argument for passing in AWS credentials, as well as the region. If AWS credentials aren’t provided, it uses @aws-sdk/credential-providers
’s fromEnv
helper to get credentials from the standard AWS environment variables made available in lambdas. It then uses those credentials to construct the appropriate [AWS SigV4][] authorization headers for IAM-based authorization.
[aws sigv4]: https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html
Usage
npm install @acusti/appsync-fetch
# or
yarn add @acusti/uniquify
The package exports appSyncFetch
, which takes the same arguments as window.fetch
, but with a few conveniences built-in. To start with, appSyncFetch
will set all required headers, including AWS authorization headers, a Date header, and Content-Type. It will also set method: 'POST'
(required for all GraphQL requests).
In addition, the second argument (named init
in the window.fetch docs) can take a query: string
property and a variables: object
property, which it will JSON.stringify into a valid GraphQL request body. You can also pass in body
as a string directly, but if you pass in a query
, the body
will be overwritten (you will get a type error in typescript if you try to pass both).
The function also takes an optional third argument where you can manually pass in AWS credentials if you don’t wish to rely on the built-in credentials handling, which will extract credentials from environment variables via process.env
. Usage is illustrated in the code example below.
And lastly, appSyncFetch
calls await response.json()
and returns the result, because that’s what you wanted anyways.
import { appSyncFetch } from '@acusti/appsync-fetch';
const appSyncURL =
'https://abcdefghijklmnopqrstuvwxyz.appsync-api.us-west-2.amazonaws.com/graphql';
// In its simplest usage, environment variables are used for authorization
const itemsResult = await appSyncFetch(appSyncURL, {
query: `
query ListItems {
listItems {
items {
id
text
}
}
}`,
});
// itemsResult is the parsed JSON from the response, e.g.:
// const response = await fetch(...);
// const itemsResult = await response.json();
// You can also pass in variables
const createdItemResult = await appSyncFetch(appSyncURL, {
query: `
mutation CreateItem($input: CreateItemInput!) {
createItem(input: $input) {
id
}
}`,
variables: {
input: {
text: 'Here is the text of a new item',
},
},
});
// You can also provide the authentication variables manually
const manualAuthenticationResult = await appSyncFetch(
appSyncURL,
{ query: 'query {...}' },
{
accessKeyId,
secretAccessKey,
sessionToken,
},
);
// By default, appSyncFetch will extract the amazon region from the AppSync URL
// You can override that by passing in a region via the optional 3rd argument
// The full type for the