Skip to content

Commit 0fb7d4f

Browse files
committed
docs: updated readme and examples
1 parent 990bb30 commit 0fb7d4f

File tree

5 files changed

+1284
-1
lines changed

5 files changed

+1284
-1
lines changed

README.md

+44
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,50 @@ Both yaml and json specifications are supported.
1212
yarn add graphql-binding-openapi
1313
```
1414

15+
## How it works
16+
17+
A service endpoint that uses the Swagger/OpenAPI specification contains a definition file (in either JSON or YAML format). This definition file has the following structure:
18+
```json
19+
{
20+
"swagger": "2.0",
21+
"info": { },
22+
"host": "petstore.swagger.io",
23+
"basePath": "/v2",
24+
"tags": [ ],
25+
"schemes": [ "http" ],
26+
"paths": { },
27+
"securityDefinitions": {},
28+
"definitions": { },
29+
"externalDocs": { }
30+
}
31+
```
32+
An example for the petstore endpoint can be found [here](./example/petstore.json).
33+
34+
This endpoint definition is transformed into a GraphQL schema, with all the paths from the endpoint translated into queries and mutations. This schema looks like this:
35+
```graphql
36+
type Query {
37+
findPetsByStatus(status: [String]): [findPetsByStatus_items]
38+
findPetsByTags(tags: [String]): [findPetsByTags_items]
39+
getPetById(petId: String): getPetById
40+
# other queries
41+
}
42+
43+
type Mutation {
44+
addPet(body: param_addPet_body): addPet
45+
updatePet(body: param_updatePet_body): updatePet
46+
updatePetWithForm(petId: String, name: String, status: String): updatePetWithForm
47+
deletePet(api_key: String, petId: String): deletePet
48+
# other mutations
49+
}
50+
```
51+
The full schema for the petstore endpoint can be found [here](./petstore.graphql).
52+
53+
The remote executable GraphQL schema (containing all the resolvers for querying the original endpoint) is exposed as a binding by `graphql-binding-openapi`, making each query and mutation available as a method on the binding class, for example:
54+
```js
55+
petstore.query.findPetsByStatus({ status: "available" })
56+
petstore.mutation.addPet({ /* mutation arguments */ })
57+
```
58+
1559
## Example
1660

1761
### Standalone

example/.graphqlconfig.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
projects:
2+
petstore:
3+
schemaPath: petstore.graphql
4+
extensions:
5+
openapi:
6+
definition: petstore.json

example/petstore.graphql

+255
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
# source:
2+
# timestamp: Mon Jan 29 2018 16:16:30 GMT+0100 (W. Europe Standard Time)
3+
4+
type addPet {
5+
"""default field"""
6+
empty: String
7+
}
8+
9+
type createUser {
10+
"""default field"""
11+
empty: String
12+
}
13+
14+
type createUsersWithArrayInput {
15+
"""default field"""
16+
empty: String
17+
}
18+
19+
type createUsersWithListInput {
20+
"""default field"""
21+
empty: String
22+
}
23+
24+
type deleteOrder {
25+
"""default field"""
26+
empty: String
27+
}
28+
29+
type deletePet {
30+
"""default field"""
31+
empty: String
32+
}
33+
34+
type deleteUser {
35+
"""default field"""
36+
empty: String
37+
}
38+
39+
type findPetsByStatus_items {
40+
id: String
41+
category: findPetsByStatus_items_category
42+
name: String
43+
photoUrls: [String]
44+
tags: [findPetsByStatus_items_tags_items]
45+
46+
"""pet status in the store"""
47+
status: String
48+
}
49+
50+
type findPetsByStatus_items_category {
51+
id: String
52+
name: String
53+
}
54+
55+
type findPetsByStatus_items_tags_items {
56+
id: String
57+
name: String
58+
}
59+
60+
type findPetsByTags_items {
61+
id: String
62+
category: findPetsByTags_items_category
63+
name: String
64+
photoUrls: [String]
65+
tags: [findPetsByTags_items_tags_items]
66+
67+
"""pet status in the store"""
68+
status: String
69+
}
70+
71+
type findPetsByTags_items_category {
72+
id: String
73+
name: String
74+
}
75+
76+
type findPetsByTags_items_tags_items {
77+
id: String
78+
name: String
79+
}
80+
81+
type getInventory {
82+
"""default field"""
83+
empty: String
84+
}
85+
86+
type getOrderById {
87+
id: String
88+
petId: String
89+
quantity: Int
90+
shipDate: String
91+
92+
"""Order Status"""
93+
status: String
94+
complete: Boolean
95+
}
96+
97+
type getPetById {
98+
id: String
99+
category: getPetById_category
100+
name: String
101+
photoUrls: [String]
102+
tags: [getPetById_tags_items]
103+
104+
"""pet status in the store"""
105+
status: String
106+
}
107+
108+
type getPetById_category {
109+
id: String
110+
name: String
111+
}
112+
113+
type getPetById_tags_items {
114+
id: String
115+
name: String
116+
}
117+
118+
type getUserByName {
119+
id: String
120+
username: String
121+
firstName: String
122+
lastName: String
123+
email: String
124+
password: String
125+
phone: String
126+
127+
"""User Status"""
128+
userStatus: Int
129+
}
130+
131+
type loginUser {
132+
"""default field"""
133+
empty: String
134+
}
135+
136+
type logoutUser {
137+
"""default field"""
138+
empty: String
139+
}
140+
141+
type Mutation {
142+
addPet(body: param_addPet_body): addPet
143+
updatePet(body: param_updatePet_body): updatePet
144+
updatePetWithForm(petId: String, name: String, status: String): updatePetWithForm
145+
deletePet(api_key: String, petId: String): deletePet
146+
placeOrder(body: param_placeOrder_body): placeOrder
147+
148+
"""
149+
For valid response try integer IDs with positive integer value. Negative or non-integer values will generate API errors
150+
"""
151+
deleteOrder(orderId: String): deleteOrder
152+
153+
"""This can only be done by the logged in user."""
154+
createUser(body: param_createUser_body): createUser
155+
createUsersWithArrayInput(body: param_createUsersWithArrayInput_body): createUsersWithArrayInput
156+
createUsersWithListInput(body: param_createUsersWithListInput_body): createUsersWithListInput
157+
158+
"""This can only be done by the logged in user."""
159+
updateUser(username: String, body: param_updateUser_body): updateUser
160+
161+
"""This can only be done by the logged in user."""
162+
deleteUser(username: String): deleteUser
163+
}
164+
165+
"""Pet object that needs to be added to the store"""
166+
input param_addPet_body {
167+
"""default field"""
168+
empty: String
169+
}
170+
171+
"""Created user object"""
172+
input param_createUser_body {
173+
"""default field"""
174+
empty: String
175+
}
176+
177+
"""List of user object"""
178+
input param_createUsersWithArrayInput_body {
179+
"""default field"""
180+
empty: String
181+
}
182+
183+
"""List of user object"""
184+
input param_createUsersWithListInput_body {
185+
"""default field"""
186+
empty: String
187+
}
188+
189+
"""order placed for purchasing the pet"""
190+
input param_placeOrder_body {
191+
"""default field"""
192+
empty: String
193+
}
194+
195+
"""Pet object that needs to be added to the store"""
196+
input param_updatePet_body {
197+
"""default field"""
198+
empty: String
199+
}
200+
201+
"""Updated user object"""
202+
input param_updateUser_body {
203+
"""default field"""
204+
empty: String
205+
}
206+
207+
type placeOrder {
208+
id: String
209+
petId: String
210+
quantity: Int
211+
shipDate: String
212+
213+
"""Order Status"""
214+
status: String
215+
complete: Boolean
216+
}
217+
218+
type Query {
219+
"""Multiple status values can be provided with comma separated strings"""
220+
findPetsByStatus(status: [String]): [findPetsByStatus_items]
221+
222+
"""
223+
Muliple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
224+
"""
225+
findPetsByTags(tags: [String]): [findPetsByTags_items]
226+
227+
"""Returns a single pet"""
228+
getPetById(petId: String): getPetById
229+
230+
"""Returns a map of status codes to quantities"""
231+
getInventory: getInventory
232+
233+
"""
234+
For valid response try integer IDs with value >= 1 and <= 10. Other values will generated exceptions
235+
"""
236+
getOrderById(orderId: String): getOrderById
237+
loginUser(username: String, password: String): loginUser
238+
logoutUser: logoutUser
239+
getUserByName(username: String): getUserByName
240+
}
241+
242+
type updatePet {
243+
"""default field"""
244+
empty: String
245+
}
246+
247+
type updatePetWithForm {
248+
"""default field"""
249+
empty: String
250+
}
251+
252+
type updateUser {
253+
"""default field"""
254+
empty: String
255+
}

0 commit comments

Comments
 (0)