Skip to content

Commit dc5c3dd

Browse files
committed
Refactoring and documentation
Signed-off-by: Alan Cha <Alan.cha1@ibm.com>
1 parent bbae892 commit dc5c3dd

20 files changed

+848
-495
lines changed

packages/openapi-to-graphql/README.md

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ For example, let's say we have an API that has an operation called `GET /users/{
8383

8484
If you define a link object, then OpenAPI-to-GraphQL will add a new field to your object type. In this case, the `User` object type will have not only an `currentEmployerId` field, but also an `employer` field. Then, you will be able to create nested GraphQL queries like the following:
8585

86-
```
86+
```graphql
8787
query {
8888
user(userId: "Alan") {
8989
currentEmployerId
@@ -100,7 +100,7 @@ To create nested object types for arrays, you will need to keep the following in
100100

101101
Continuing from the previous example, let's say that there is a third operation called `GET /friends/{userId}` which would return an array of users, specifically the friends of a particular user. Furthermore, let's say you wanted to run the following query, which would allow you to get all the employers of Alan's friends:
102102

103-
```
103+
```graphql
104104
query {
105105
friends(userId: "Alan") {
106106
currentEmployerId
@@ -188,7 +188,7 @@ Resolver options:
188188

189189
Authentication options:
190190

191-
- `viewer` (type: `boolean`, default: `true`): The viewer object types (i.e. `QueryViewer` and `MutationViewer`) are artificial constructs that allow users to pass authentication credentials to OpenAPI-to-GraphQL. They are created when the OAS defines [security scheme objects](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#securitySchemeObject) and when operations adopt them through a [security requirement object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#securityRequirementObject). A viewer is created for each security scheme and each viewer contains authenticated operations that uses its respective security scheme. In addition, a special `AnyAuth` viewer, which can authenticate requests utilizing different security schemes, is created. Unfortunately, viewers are bulky so, depending on the API, it may be possible to send credentials through the `header`, `qs`, or `requestOptions` options. _Note: OAuth authentication is handled using the `tokenJSONpath` and `sendOAuthTokenInQuery` options._
191+
- `viewer` (type: `boolean`, default: `true`): The viewer object types (i.e. `QueryViewer` and `MutationViewer`) are artificial constructs that allow users to pass authentication credentials to OpenAPI-to-GraphQL. They are created when the OAS defines [security scheme objects](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#securitySchemeObject) and when operations adopt them through a [security requirement object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#securityRequirementObject). A viewer is created for each security scheme and each viewer contains authenticated operations that uses its respective security scheme. In addition, a special `anyAuth` viewer, which can authenticate requests utilizing different security schemes, is created. Unfortunately, viewers are bulky so, depending on the API, it may be possible to send credentials through the `header`, `qs`, or `requestOptions` options. _Note: OAuth authentication is handled using the `tokenJSONpath` and `sendOAuthTokenInQuery` options._
192192

193193
- `tokenJSONpath` (type: `string`): Used to pass the [JSONPath](http://goessner.net/articles/JsonPath/) of the OAuth token in the GraphQL context. To see more details, click [here](./README.md#authorization).
194194

@@ -242,6 +242,72 @@ OpenAPI-to-GraphQL.createGraphQLSchema(oas, {
242242
})
243243
```
244244

245+
## Custom Type and Field Names and Enum Values
246+
247+
The `x-graphql-type-name`, `x-graphql-field-name`, and `x-graphql-enum-mapping` OAS extensions can be used to configure the types and field names as well as enum values.
248+
249+
The type and field names and enum values that OpenAPI-to-GraphQL generates may not be adequate or may not be consistent over different versions so this is a way to guarantee consistency.
250+
251+
`x-graphql-type-name` and `x-graphql-field-name` can be added to JSON schema to change the type name as well as change a field name.
252+
253+
```diff
254+
{
255+
"type": "object",
256+
+ "x-graphql-type-name": "Response",
257+
"properties": {
258+
"code": {
259+
"type": "integer",
260+
+ "x-graphql-field-name": "statusCode",
261+
}
262+
}
263+
}
264+
```
265+
266+
`x-graphql-field-name` can be added to an [operation object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#operationObject) to change a field name under the top-level `Query`, `Mutation`, and `Subscription` types.
267+
268+
```diff
269+
{
270+
"/pet/findByStatus": {
271+
"get": {
272+
+ "x-graphql-field-name": "getPetsByStatus",
273+
"parameters": [
274+
...
275+
],
276+
"responses": {
277+
...
278+
}
279+
}
280+
}
281+
}
282+
```
283+
284+
`x-graphql-field-name` can be added to a [link object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#linkObject) to change the name of a linked field (see section on [Nested Objects](https://github.com/IBM/openapi-to-graphql/tree/master/packages/openapi-to-graphql#nested-objects)).
285+
286+
```diff
287+
{
288+
+ "x-graphql-field-name": "orderPet",
289+
"operationId": "getPetById",
290+
"parameters": {
291+
"petId": "$response.body.petId"
292+
},
293+
"description": "Link from Order to Pet"
294+
}
295+
```
296+
297+
`x-graphql-enum-mapping` can be added to a JSON schema to change the enum values. It is a mapping from the original enum values to the ones that should used in the GraphQL interface.
298+
299+
```diff
300+
{
301+
"type": "string",
302+
"enum": ["available", "pending", "sold"],
303+
+ "x-graphql-enum-mapping": {
304+
+ "available": "INITIAL",
305+
+ "pending": "IN_PROGRESS",
306+
+ "sold": "SOLD"
307+
+ }
308+
}
309+
```
310+
245311
## Authentication
246312

247313
By default, OpenAPI-to-GraphQL will wrap API requests that need authentication in corresponding `viewers`, which allow the user to pass required credentials. OpenAPI-to-GraphQL currently supports viewers for basic authentication and API keys. For example, a query using an API key viewer is:
@@ -264,7 +330,7 @@ mutation {
264330
}
265331
```
266332

267-
OpenAPI-to-GraphQL further provides `anyAuth` viewers (for queries and mutations), which allow the user to simultaneously provide information for multiple authentication mechanisms. AnyAuth viewers allow OpenAPI-to-GraphQL to resolve nested queries and mutations that encompass API requests with different authentication mechanisms. For example, consider the following query:
333+
OpenAPI-to-GraphQL further provides `anyAuth` viewers (for queries and mutations), which allow the user to simultaneously provide information for multiple authentication mechanisms. `anyAuth` viewers allow OpenAPI-to-GraphQL to resolve nested queries and mutations that encompass API requests with different authentication mechanisms. For example, consider the following query:
268334

269335
```javascript
270336
{

0 commit comments

Comments
 (0)