Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.
Verwenden Sie den AWS::Serverless::GraphQLApi
Ressourcentyp AWS Serverless Application Model (AWS SAM), um eine zu erstellen und zu konfigurieren AWS AppSync GraphQL API für Ihre serverlose Anwendung.
Weitere Informationen dazu finden Sie AWS AppSync unter Was ist AWS AppSync? im AWS AppSync Entwicklerhandbuch.
Syntax
YAML
LogicalId
:
Type: AWS::Serverless::GraphQLApi
Properties:
ApiKeys: ApiKeys
Auth: Auth
Cache: AWS::AppSync::ApiCache
DataSources: DataSource
DomainName: AWS::AppSync::DomainName
Functions: Function
Logging: LogConfig
Name: String
Resolvers: Resolver
SchemaInline: String
SchemaUri: String
Tags:
- Tag
XrayEnabled: Boolean
Eigenschaften
ApiKeys
-
Erstellen Sie einen eindeutigen Schlüssel, mit dem Sie Folgendes ausführen können GraphQL Operationen, für die ein API-Schlüssel erforderlich ist.
Typ: ApiKeys
Required: No
AWS CloudFormation Kompatibilität: Diese Eigenschaft ist einzigartig für AWS SAM und hat kein AWS CloudFormation Äquivalent.
Auth
-
Konfigurieren Sie die Authentifizierung für Ihr GraphQL API.
Typ: Auth
Erforderlich: Ja
AWS CloudFormation Kompatibilität: Diese Eigenschaft ist einzigartig für AWS SAM und hat kein AWS CloudFormation Äquivalent.
Cache
-
Die Eingabe einer
CreateApiCache
Operation.Required: No
AWS CloudFormation Kompatibilität: Diese Eigenschaft wird direkt an die AWS::AppSync::ApiCacheRessource übergeben.
DataSources
-
Erstellen Sie Datenquellen für Funktionen, mit denen AWS AppSync Sie eine Verbindung herstellen möchten. AWS SAM unterstützt Amazon DynamoDB und AWS Lambda Datenquellen.
Typ: DataSource
Erforderlich: Ja
AWS CloudFormation Kompatibilität: Diese Eigenschaft ist einzigartig für AWS SAM und hat kein AWS CloudFormation Äquivalent.
DomainName
-
Benutzerdefinierter Domainname für Ihren GraphQL API.
Required: No
AWS CloudFormation Kompatibilität: Diese Eigenschaft wird direkt an die AWS::AppSync::DomainNameRessource übergeben. AWS SAM generiert die AWS::AppSync::DomainNameApiAssociationRessource automatisch.
Functions
-
Funktionen konfigurieren in GraphQL APIs um bestimmte Operationen auszuführen.
Typ: Funktion
Erforderlich: Ja
AWS CloudFormation Kompatibilität: Diese Eigenschaft ist einzigartig AWS SAM und hat kein AWS CloudFormation Äquivalent.
Logging
-
Konfiguriert die CloudWatch Amazon-Protokollierung für Ihre GraphQL API.
Wenn Sie diese Eigenschaft nicht angeben, AWS SAM werden die folgenden Werte generiert
CloudWatchLogsRoleArn
und festgelegt:-
ExcludeVerboseContent: true
-
FieldLogLevel: ALL
Um die Protokollierung zu deaktivieren, geben Sie Folgendes an:
Logging: false
Typ: LogConfig
Required: No
AWS CloudFormation Kompatibilität: Diese Eigenschaft wird direkt an die
LogConfig
Eigenschaft einerAWS::AppSync::GraphQLApi
Ressource übergeben. -
LogicalId
-
Der eindeutige Name Ihres GraphQL API.
Typ: Zeichenfolge
Erforderlich: Ja
AWS CloudFormation Kompatibilität: Diese Eigenschaft wird direkt an die
Name
Eigenschaft einerAWS::AppSync::GraphQLApi
Ressource übergeben. Name
-
Der Name Ihres GraphQL API. Geben Sie diese Eigenschaft an, um den
LogicalId
Wert zu überschreiben.Typ: Zeichenfolge
Required: No
AWS CloudFormation Kompatibilität: Diese Eigenschaft wird direkt an die
Name
Eigenschaft einerAWS::AppSync::GraphQLApi
Ressource übergeben. Resolvers
-
Konfigurieren Sie Resolver für die Felder Ihres GraphQL API. AWS SAM unterstützt JavaScript Pipeline-Resolver.
Erforderlich: Ja
AWS CloudFormation Kompatibilität: Diese Eigenschaft ist einzigartig AWS SAM und hat kein AWS CloudFormation Äquivalent.
SchemaInline
-
Die Textdarstellung eines GraphQL Schema in SDL .
Typ: Zeichenfolge
Erforderlich: Bedingt. Sie müssen
SchemaInline
oder angebenSchemaUri
.AWS CloudFormation Kompatibilität: Diese Eigenschaft wird direkt an die
Definition
Eigenschaft einerAWS::AppSync::GraphQLSchema
Ressource übergeben. SchemaUri
-
Die Amazon Simple Storage Service (Amazon S3) -Bucket-URI oder der Pfad zu einem lokalen Ordner des Schemas.
Wenn Sie einen Pfad zu einem lokalen Ordner angeben, AWS CloudFormation muss die Datei vor der Bereitstellung zuerst auf Amazon S3 hochgeladen werden. Sie können das verwenden AWS SAM CLI um diesen Prozess zu erleichtern. Weitere Informationen finden Sie unter So werden lokale Dateien bei der Bereitstellung AWS SAM hochgeladen.
Typ: Zeichenfolge
Erforderlich: Bedingt. Sie müssen
SchemaInline
oder angebenSchemaUri
.AWS CloudFormation Kompatibilität: Diese Eigenschaft wird direkt an die
DefinitionS3Location
Eigenschaft einerAWS::AppSync::GraphQLSchema
Ressource übergeben. -
Tags (Schlüssel-Wert-Paare) dafür GraphQL API. Verwenden Sie Tags, um Ressourcen zu identifizieren und zu kategorisieren.
Typ: Liste von Tag
Required: No
AWS CloudFormation Kompatibilität: Diese Eigenschaft wird direkt an die
Tag
Eigenschaft einerAWS::AppSync::GraphQLApi
Ressource übergeben. XrayEnabled
-
Geben Sie an, ob AWS X-Ray Tracing für diese Ressource verwendet werden soll.
Typ: Boolesch
Required: No
AWS CloudFormation Kompatibilität: Diese Eigenschaft wird direkt an die
XrayEnabled
Eigenschaft einerAWS::AppSync::GraphQLApi
Ressource übergeben.
Rückgabewerte
Eine Liste der Rückgabewerte finden Sie unter AWS::Serverless::GraphQLApi im AWS CloudFormation -Benutzerhandbuch.
Beispiele
GraphQL API mit DynamoDB-Datenquelle
In diesem Beispiel erstellen wir eine GraphQL API, die eine DynamoDB-Tabelle als Datenquelle verwendet.
schema.graphql
schema { query: Query mutation: Mutation } type Query { getPost(id: String!): Post } type Mutation { addPost(author: String!, title: String!, content: String!): Post! } type Post { id: String! author: String title: String content: String ups: Int! downs: Int! version: Int! }
Vorlage.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
...
Resources:
DynamoDBPostsTable:
Type: AWS::Serverless::SimpleTable
MyGraphQLAPI:
Type: AWS::Serverless::GraphQLApi
Properties:
SchemaUri: ./sam_graphql_api/schema.graphql
Auth:
Type: AWS_IAM
DataSources:
DynamoDb:
PostsDataSource:
TableName: !Ref DynamoDBPostsTable
TableArn: !GetAtt DynamoDBPostsTable.Arn
Functions:
preprocessPostItem:
Runtime:
Name: APPSYNC_JS
Version: 1.0.0
DataSource: NONE
CodeUri: ./sam_graphql_api/preprocessPostItem.js
createPostItem:
Runtime:
Name: APPSYNC_JS
Version: "1.0.0"
DataSource: PostsDataSource
CodeUri: ./sam_graphql_api/createPostItem.js
getPostFromTable:
Runtime:
Name: APPSYNC_JS
Version: "1.0.0"
DataSource: PostsDataSource
CodeUri: ./sam_graphql_api/getPostFromTable.js
Resolvers:
Mutation:
addPost:
Runtime:
Name: APPSYNC_JS
Version: "1.0.0"
Pipeline:
- preprocessPostItem
- createPostItem
Query:
getPost:
CodeUri: ./sam_graphql_api/getPost.js
Runtime:
Name: APPSYNC_JS
Version: "1.0.0"
Pipeline:
- getPostFromTable
createPostItem.js
import { util } from "@aws-appsync/utils";
export function request(ctx) {
const { key, values } = ctx.prev.result;
return {
operation: "PutItem",
key: util.dynamodb.toMapValues(key),
attributeValues: util.dynamodb.toMapValues(values),
};
}
export function response(ctx) {
return ctx.result;
}
getPostFromTable.js
import { util } from "@aws-appsync/utils";
export function request(ctx) {
return dynamoDBGetItemRequest({ id: ctx.args.id });
}
export function response(ctx) {
return ctx.result;
}
/**
* A helper function to get a DynamoDB item
*/
function dynamoDBGetItemRequest(key) {
return {
operation: "GetItem",
key: util.dynamodb.toMapValues(key),
};
}
preprocessPostItem.js
import { util } from "@aws-appsync/utils";
export function request(ctx) {
const id = util.autoId();
const { ...values } = ctx.args;
values.ups = 1;
values.downs = 0;
values.version = 1;
return { payload: { key: { id }, values: values } };
}
export function response(ctx) {
return ctx.result;
}
Hier ist unser Resolver-Code:
getPost.js
export function request(ctx) {
return {};
}
export function response(ctx) {
return ctx.prev.result;
}
GraphQL API mit einer Lambda-Funktion als Datenquelle
In diesem Beispiel erstellen wir eine GraphQL API, die eine Lambda-Funktion als Datenquelle verwendet.
template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
...
Resources:
MyLambdaFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs20.x
CodeUri: ./lambda
MyGraphQLAPI:
Type: AWS::Serverless::GraphQLApi
Properties:
Name: MyApi
SchemaUri: ./gql/schema.gql
Auth:
Type: API_KEY
ApiKeys:
MyApiKey:
Description: my api key
DataSources:
Lambda:
MyLambdaDataSource:
FunctionArn: !GetAtt MyLambdaFunction.Arn
Functions:
lambdaInvoker:
Runtime:
Name: APPSYNC_JS
Version: 1.0.0
DataSource: MyLambdaDataSource
CodeUri: ./gql/invoker.js
Resolvers:
Mutation:
addPost:
Runtime:
Name: APPSYNC_JS
Version: 1.0.0
Pipeline:
- lambdaInvoker
Query:
getPost:
Runtime:
Name: APPSYNC_JS
Version: 1.0.0
Pipeline:
- lambdaInvoker
Outputs:
MyGraphQLAPI:
Description: AppSync API
Value: !GetAtt MyGraphQLAPI.GraphQLUrl
MyGraphQLAPIMyApiKey:
Description: API Key for authentication
Value: !GetAtt MyGraphQLAPIMyApiKey.ApiKey
schema.graphql
schema { query: Query mutation: Mutation } type Query { getPost(id: ID!): Post } type Mutation { addPost(id: ID!, author: String!, title: String, content: String): Post! } type Post { id: ID! author: String! title: String content: String ups: Int downs: Int }
Hier sind unsere Funktionen:
lambda/index.js
exports.handler = async (event) => {
console.log("Received event {}", JSON.stringify(event, 3));
const posts = {
1: {
id: "1",
title: "First book",
author: "Author1",
content: "Book 1 has this content",
ups: "100",
downs: "10",
},
};
console.log("Got an Invoke Request.");
let result;
switch (event.field) {
case "getPost":
return posts[event.arguments.id];
case "addPost":
// return the arguments back
return event.arguments;
default:
throw new Error("Unknown field, unable to resolve " + event.field);
}
};
invoker.js
import { util } from "@aws-appsync/utils";
export function request(ctx) {
const { source, args } = ctx;
return {
operation: "Invoke",
payload: { field: ctx.info.fieldName, arguments: args, source },
};
}
export function response(ctx) {
return ctx.result;
}