You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/openapi-to-graphql/README.md
+70-4Lines changed: 70 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -83,7 +83,7 @@ For example, let's say we have an API that has an operation called `GET /users/{
83
83
84
84
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:
85
85
86
-
```
86
+
```graphql
87
87
query {
88
88
user(userId: "Alan") {
89
89
currentEmployerId
@@ -100,7 +100,7 @@ To create nested object types for arrays, you will need to keep the following in
100
100
101
101
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:
102
102
103
-
```
103
+
```graphql
104
104
query {
105
105
friends(userId: "Alan") {
106
106
currentEmployerId
@@ -188,7 +188,7 @@ Resolver options:
188
188
189
189
Authentication options:
190
190
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._
192
192
193
193
-`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).
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
+
245
311
## Authentication
246
312
247
313
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 {
264
330
}
265
331
```
266
332
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:
0 commit comments