

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# GraphQL スキーマのディレクティブの操作
<a name="tools-graphql-schema-with-directives"></a>

次のようなコマンドを使用して、既にディレクティブが設定されている GraphQL スキーマから開始できます。

```
neptune-for-graphql \
  --input-schema-file (your GraphQL schema file with directives) \
  --create-update-aws-pipeline \
  --create-update-aws-pipeline-name (name for your new GraphQL API) \
  --create-update-aws-pipeline-neptune-endpoint  (empty Neptune database endpoint):(port number) \
  --output-resolver-query-https
```

ユーティリティが作成したディレクティブを変更したり、独自のディレクティブを GraphQL スキーマに追加したりできます。ディレクティブを操作するには次のような方法があります。

## ミューテーションが発生しないようにユーティリティを実行する
<a name="tools-graphql-no-mutations"></a>

ユーティリティが GraphQL API にミューテーションを生成しないようにするには、`neptune-for-graphql` コマンドの `--output-schema-no-mutations` オプションを使用します。

## `@alias` ディレクティブ
<a name="tools-graphql-alias-directive"></a>

`@alias` ディレクティブは、GraphQL スキーマタイプまたはフィールドに適用できます。グラフデータベースと GraphQL スキーマの間で異なる名前をマッピングします。構文は次のとおりです。

```
@alias(property: (property name))
```

以下の例では、`airport` は `Airport` GraphQL タイプにマッピングされたグラフデータベースノードラベルであり、`desc` は `description` フィールドにマッピングされたグラフノードプロパティです (「[空路の例](tools-graphql.md)」を参照)。

```
type Airport @alias(property: "airport") {
  city: String
  description: String @alias(property: "desc")
}
```

標準の GraphQL フォーマットでは、パスカルケーシングの型名とキャメルケーシングのフィールド名が必要であることに注意してください。

## `@relationship` ディレクティブ
<a name="tools-graphql-relationship-directive"></a>

`@relationship` ディレクティブは、ネストされた GraphQL タイプをグラフデータベースのエッジにマッピングします。構文は次のとおりです。

```
@relationship(edgeType: (edge name), direction: (IN or OUT))
```

コマンドの例を次に示します。

```
type Airport @alias(property: "airport") {
  ...
  continentContainsIn: Continent @relationship(edgeType: "contains", direction: IN)
  countryContainsIn: Country @relationship(edgeType: "contains", direction: IN)
  airportRoutesOut(filter: AirportInput, options: Options): [Airport] @relationship(edgeType: "route", direction: OUT)
  airportRoutesIn(filter: AirportInput, options: Options): [Airport] @relationship(edgeType: "route", direction: IN)
}
```

`@relationship` ディレクティブは「[Todo の例](tools-graphql-start-from-schema.md#tools-graphql-todo-example)」と「[空路の例](tools-graphql.md)」の両方にあります。

## `@graphQuery` および `@cypher` ディレクティブ
<a name="tools-graphql-graphquery-cypher-directives"></a>

openCypher クエリを定義して、フィールド値を解決したり、クエリを追加したり、ミューテーションを追加したりできます。例えば、これは、アウトバウンドルートをカウントする新しい `outboundRoutesCount` フィールドを `Airport` タイプに追加します。

```
type Airport @alias(property: "airport") {
  ...
  outboundRoutesCount: Int @graphQuery(statement: "MATCH (this)-[r:route]->(a) RETURN count(r)")
}
```

以下は新しいクエリとミューテーションの例です。

```
type Query {
  getAirportConnection(fromCode: String!, toCode: String!): Airport \
    @cypher(statement: \
      "MATCH (:airport{code: '$fromCode'})-[:route]->(this:airport)-[:route]->(:airport{code:'$toCode'})")
}

type Mutation {
  createAirport(input: AirportInput!): Airport @graphQuery(statement: "CREATE (this:airport {$input}) RETURN this")
  addRoute(fromAirportCode:String, toAirportCode:String, dist:Int): Route \
    @graphQuery(statement: \
     "MATCH (from:airport{code:'$fromAirportCode'}), (to:airport{code:'$toAirportCode'}) \
      CREATE (from)-[this:route{dist:$dist}]->(to) \
      RETURN this")
}
```

`RETURN` を省略すると、リゾルバーはキーワード `this` を戻り値のスコープと見なすことに注意してください。

Gremlin クエリを使用してクエリやミューチュテーションを追加することもできます。

```
type Query {
  getAirportWithGremlin(code:String): Airport \
    @graphQuery(statement: "g.V().has('airport', 'code', '$code').elementMap()")  # single node
  getAirportsWithGremlin: [Airport] \
    @graphQuery(statement: "g.V().hasLabel('airport').elementMap().fold()")       # list of nodes
  getCountriesCount: Int \
    @graphQuery(statement: "g.V().hasLabel('country').count()")                   # scalar
}
```

現時点では、Gremlin クエリはスカラー値を返すもの、または単一ノードについて `elementMap()`、またはノードのリストについて `elementMap().fold()` を返すものに限られています。

## `@id` ディレクティブ
<a name="tools-graphql-id-directive"></a>

`@id` ディレクティブは、`id` グラフデータベースエンティティにマッピングされたフィールドを識別します。Amazon Neptune のようなグラフデータベースには、一括インポート時に割り当てられたか、または自動生成された、ノードとエッジの一意の `id` が常にあります。例えば、次のようになります。

```
type Airport {
  _id: ID! @id
  city: String
  code: String
}
```

## 予約されているタイプ、クエリ、ミューテーション名
<a name="tools-graphql-reserved-names"></a>

このユーティリティは、クエリとミューテーションを自動生成して、動作する GraphQL API を作成します。これらの名前のパターンはリゾルバーによって認識され、予約されています。タイプ `Airport` と接続タイプ `Route` の例を以下に示します。

`Options` タイプは予約されています。

```
input Options {
  limit: Int
}
```

`filter` および `options` 関数パラメータは予約されています。

```
type Query {
  getNodeAirports(filter: AirportInput, options: Options): [Airport]
}
```

クエリ名の getNode プレフィックスは予約されており、`createNode`、`updateNode`、`deleteNode`、`connectNode`、`deleteNode`、`updateEdge`、`deleteEdge` などのミューテーション名のプレフィックスは予約されています。

```
type Query {
  getNodeAirport(id: ID, filter: AirportInput): Airport
  getNodeAirports(filter: AirportInput): [Airport]
}

type Mutation {
  createNodeAirport(input: AirportInput!): Airport
  updateNodeAirport(id: ID!, input: AirportInput!): Airport
  deleteNodeAirport(id: ID!): Boolean
  connectNodeAirportToNodeAirportEdgeRout(from: ID!, to: ID!, edge: RouteInput!): Route
  updateEdgeRouteFromAirportToAirport(from: ID!, to: ID!, edge: RouteInput!): Route
  deleteEdgeRouteFromAirportToAirport(from: ID!, to: ID!): Boolean
}
```

## GraphQL スキーマに変更を適用する
<a name="tools-graphql-apply-schema-changes"></a>

GraphQL ソーススキーマを変更して、ユーティリティを再実行すると、Neptune データベースから最新のスキーマを取得できます。ユーティリティは、データベース内の新しいスキーマを検出するたびに、新しい GraphQL スキーマを生成します。

GraphQL ソーススキーマを手動で編集し、Neptune データベースエンドポイントの代わりにソーススキーマを入力として使用してユーティリティを再実行することもできます。

最後に、次の JSON 形式を使用して、変更内容をファイルに入れることができます。

```
[
  {
    "type": "(GraphQL type name)",
    "field": "(GraphQL field name)",
    "action": "(remove or add)",
    "value": "(value)"
  }
]
```

例えば、次のようになります。

```
[
  {
    "type": "Airport",
    "field": "outboundRoutesCountAdd",
    "action": "add",
    "value":"outboundRoutesCountAdd: Int @graphQuery(statement: \"MATCH (this)-[r:route]->(a) RETURN count(r)\")"
  },
  {
    "type": "Mutation",
    "field": "deleteNodeVersion",
    "action": "remove",
    "value": ""
  },
  {
    "type": "Mutation",
    "field": "createNodeVersion",
    "action": "remove",
    "value": ""
  }
]
```

次に、コマンドの `--input-schema-changes-file` パラメータを使用して、このファイルに対してユーティリティを実行すると、ユーティリティは変更を一度に適用します。