

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.

# AWS::Serverless::GraphQLApi
<a name="sam-resource-graphqlapi"></a>

Verwenden Sie den `AWS::Serverless::GraphQLApi` Ressourcentyp AWS Serverless Application Model (AWS SAM), um eine AWS AppSync GraphQL API für Ihre serverlose Anwendung zu erstellen und zu konfigurieren.

Weitere Informationen dazu finden Sie AWS AppSync unter [Was ist AWS AppSync?](https://docs.aws.amazon.com/appsync/latest/devguide/what-is-appsync.html) im *AWS AppSync Entwicklerhandbuch*.

## Syntax
<a name="sam-resource-graphqlapi-syntax"></a>

### YAML
<a name="sam-resource-graphqlapi-syntax-yaml"></a>

```
{{LogicalId}}:
  Type: AWS::Serverless::GraphQLApi
  Properties:
    ApiKeys: {{ApiKeys}}
    Auth: {{Auth}}
    Cache: {{[AWS::AppSync::ApiCache](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-apicache.html)}}
    DataSources: {{DataSource}}
    DomainName: {{[AWS::AppSync::DomainName](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-domainname.html)}}
    Functions: {{Function}}
    Logging: {{[LogConfig](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-appsync-graphqlapi-logconfig.html)}}
    Name: {{String}}
    Resolvers: {{Resolver}}
    SchemaInline: {{String}}
    SchemaUri: {{String}}
    Tags:
    {{- [Tag](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-resource-tags.html)}}
    XrayEnabled: {{Boolean}}
```

## Eigenschaften
<a name="sam-resource-graphqlapi-properties"></a>

`ApiKeys`  <a name="sam-graphqlapi-apikeys"></a>
Erstellen Sie einen eindeutigen Schlüssel, der verwendet werden kann, um GraphQL Operationen auszuführen, für die ein API-Schlüssel erforderlich ist.  
*Typ:* [ApiKeys](sam-property-graphqlapi-apikeys.md)  
*Required*: No  
*CloudFormation Kompatibilität*: Diese Eigenschaft ist einzigartig für AWS SAM und hat kein CloudFormation Äquivalent.

`Auth`  <a name="sam-graphqlapi-auth"></a>
Konfigurieren Sie die Authentifizierung für Ihre GraphQL API.  
*Typ*: [Auth](sam-property-graphqlapi-auth.md)  
*Erforderlich*: Ja  
*CloudFormation Kompatibilität*: Diese Eigenschaft ist einzigartig für AWS SAM und hat kein CloudFormation Äquivalent.

`Cache`  <a name="sam-graphqlapi-cache"></a>
Die Eingabe einer `CreateApiCache` Operation.  
*Typ:* [AWS::AppSync::ApiCache](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-apicache.html)  
*Required*: No  
*CloudFormation Kompatibilität*: Diese Eigenschaft wird direkt an die [AWS::AppSync::ApiCache](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-apicache.html)Ressource übergeben.

`DataSources`  <a name="sam-graphqlapi-datasources"></a>
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](sam-property-graphqlapi-datasource.md)  
*Erforderlich*: Ja  
*CloudFormation Kompatibilität*: Diese Eigenschaft ist einzigartig für AWS SAM und hat kein CloudFormation Äquivalent.

`DomainName`  <a name="sam-graphqlapi-domainname"></a>
Benutzerdefinierter Domainname für Ihre GraphQL API.  
*Typ:* [AWS::AppSync::DomainName](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-domainname.html)  
*Required*: No  
*CloudFormation Kompatibilität*: Diese Eigenschaft wird direkt an die [AWS::AppSync::DomainName](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-domainname.html)Ressource übergeben. AWS SAM generiert die [AWS::AppSync::DomainNameApiAssociation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-domainnameapiassociation.html)Ressource automatisch.

`Functions`  <a name="sam-graphqlapi-functions"></a>
Konfigurieren Sie Funktionen GraphQL APIs , um bestimmte Operationen auszuführen.  
*Typ*: [Funktion](sam-property-graphqlapi-function.md)  
*Erforderlich*: Ja  
*CloudFormation Kompatibilität*: Diese Eigenschaft ist einzigartig für AWS SAM und hat kein CloudFormation Äquivalent.

`Logging`  <a name="sam-graphqlapi-logging"></a>
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](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-appsync-graphqlapi-logconfig.html)  
*Required*: No  
*CloudFormation Kompatibilität*: Diese Eigenschaft wird direkt an die `[LogConfig](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-graphqlapi.html#cfn-appsync-graphqlapi-logconfig)` Eigenschaft einer `AWS::AppSync::GraphQLApi` Ressource übergeben.

`LogicalId`  <a name="sam-graphqlapi-logicalid"></a>
Der eindeutige Name Ihrer GraphQL API.  
*Typ:* Zeichenfolge  
*Erforderlich*: Ja  
*CloudFormation Kompatibilität*: Diese Eigenschaft wird direkt an die `[Name](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-graphqlapi.html#cfn-appsync-graphqlapi-name)` Eigenschaft einer `AWS::AppSync::GraphQLApi` Ressource übergeben.

`Name`  <a name="sam-graphqlapi-name"></a>
Der Name Ihrer GraphQL API. Geben Sie diese Eigenschaft an, um den `LogicalId` Wert zu überschreiben.  
*Typ:* Zeichenfolge  
*Required*: No  
*CloudFormation Kompatibilität*: Diese Eigenschaft wird direkt an die `[Name](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-graphqlapi.html#cfn-appsync-graphqlapi-name)` Eigenschaft einer `AWS::AppSync::GraphQLApi` Ressource übergeben.

`Resolvers`  <a name="sam-graphqlapi-resolvers"></a>
Konfigurieren Sie Resolver für die Felder Ihrer GraphQL API. AWS SAM unterstützt [JavaScriptPipeline-Resolver](https://docs.aws.amazon.com/appsync/latest/devguide/resolver-reference-overview-js.html#anatomy-of-a-pipeline-resolver-js).  
*[Typ: Resolver](sam-property-graphqlapi-resolver.md)*  
*Erforderlich*: Ja  
*CloudFormation Kompatibilität*: Diese Eigenschaft ist einzigartig AWS SAM und hat kein CloudFormation Äquivalent.

`SchemaInline`  <a name="sam-graphqlapi-schemainline"></a>
Die Textdarstellung eines GraphQL Schemas im SDL Format.  
*Typ:* Zeichenfolge  
*Erforderlich:* Bedingt. Sie müssen `SchemaInline` oder angeben`SchemaUri`.  
*CloudFormation Kompatibilität*: Diese Eigenschaft wird direkt an die `[Definition](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-graphqlschema.html#cfn-appsync-graphqlschema-definition)` Eigenschaft einer `AWS::AppSync::GraphQLSchema` Ressource übergeben.

`SchemaUri`  <a name="sam-graphqlapi-schemauri"></a>
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, CloudFormation muss die Datei vor der Bereitstellung zuerst auf Amazon S3 hochgeladen werden. Sie können den verwenden AWS SAMCLI, um diesen Vorgang zu vereinfachen. Weitere Informationen finden Sie unter [So werden lokale Dateien bei der Bereitstellung AWS SAM hochgeladen](deploy-upload-local-files.md).  
*Typ:* Zeichenfolge  
*Erforderlich:* Bedingt. Sie müssen `SchemaInline` oder angeben`SchemaUri`.  
*CloudFormation Kompatibilität*: Diese Eigenschaft wird direkt an die `[DefinitionS3Location](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-graphqlschema.html#cfn-appsync-graphqlschema-definitions3location)` Eigenschaft einer `AWS::AppSync::GraphQLSchema` Ressource übergeben.

`Tags`  <a name="sam-graphqlapi-tags"></a>
Tags (Schlüssel-Wert-Paare) für diese GraphQL API. Verwenden Sie Tags, um Ressourcen zu identifizieren und zu kategorisieren.  
*Typ:* Liste von [Tag](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-resource-tags.html)  
*Required*: No  
*CloudFormation Kompatibilität*: Diese Eigenschaft wird direkt an die `[Tag](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-graphqlapi.html#cfn-appsync-graphqlapi-tags)` Eigenschaft einer `AWS::AppSync::GraphQLApi` Ressource übergeben.

`XrayEnabled`  <a name="sam-graphqlapi-xrayenabled"></a>
Geben Sie an, ob [AWS X-Ray Tracing](https://docs.aws.amazon.com/xray/latest/devguide/aws-xray.html) für diese Ressource verwendet werden soll.  
*Typ*: Boolesch  
*Required*: No  
*CloudFormation Kompatibilität*: Diese Eigenschaft wird direkt an die `[XrayEnabled](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-graphqlapi.html#cfn-appsync-graphqlapi-xrayenabled)` Eigenschaft einer `AWS::AppSync::GraphQLApi` Ressource übergeben.

## Rückgabewerte
<a name="sam-resource-graphqlapi-return-values"></a>

Eine Liste der Rückgabewerte finden Sie [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-graphqlapi.html#aws-resource-appsync-graphqlapi-return-values.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appsync-graphqlapi.html#aws-resource-appsync-graphqlapi-return-values.html)im [CloudFormation Benutzerhandbuch](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html).

## Beispiele
<a name="sam-resource-graphqlapi-examples"></a>

### GraphQL APImit DynamoDB-Datenquelle
<a name="sam-resource-graphqlapi-examples-example1"></a>

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;
}
```

### GraphQLAPI mit einer Lambda-Funktion als Datenquelle
<a name="sam-resource-graphqlapi-examples-example2"></a>

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;
}
```