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
:description: This tutorial builds an API around the Northwind sample dataset with the Neo4j GraphQL Library.
2
+
= Creating an API for the Northwind data set
3
+
:description: This tutorial builds an API around the Northwind sample data set with the Neo4j GraphQL Library.
4
4
5
5
This tutorial uses the Neo4j GraphQL Library to build an API for the Northwind sample datset.
6
6
@@ -12,53 +12,64 @@ This model lends itself for a webshop API.
12
12
13
13
. Set up a new AuraDB instance.
14
14
Refer to link:https://neo4j.com/docs/aura/getting-started/create-instance/[Creating a Neo4j Aura instance].
15
-
. Populate the instance with the Northwind dataset.
16
-
.. In Aura, select **Learning**, then **Beginner** under **Getting started**.
17
-
.. Select the **Learn the basics** tile and scroll to page 4/11 in the left side menu.
18
-
.. Trigger the import with **Get the Northwind datset** and then **Run import** on the right hand side.
19
-
15
+
. Populate the instance with the Northwind data set.
16
+
+
20
17
[NOTE]
21
18
====
22
-
If you have completed the GraphQL and Aura Console getting started guide and would like to get rid of the example nodes you have created there, run the following in **Query**:
19
+
If you have completed the GraphQL and Aura Console getting started guide and would like to get rid of the example nodes you have created there, run the following in **Query** before populating your data base with the Northwind set:
23
20
24
21
[source,cypher]
25
22
----
26
23
MATCH (n) DETACH DELETE n;
27
24
----
28
25
====
26
+
+
27
+
.. In Aura, select **Learning**, then **Beginner** under **Getting started**.
28
+
.. Select the **Learn the basics** tile and scroll to page 4/11 in the left side menu.
29
+
.. Trigger the import with **Get the Northwind datset** and then **Run import** on the right hand side.
30
+
. The code examples in xref:#_use_the_api[] are in JavaScript.
29
31
30
32
31
33
== Goal
32
34
33
-
A webshop API which connects to the Northwind dataset should be able to:
35
+
A webshop API which connects to the Northwind data set should be able to:
34
36
35
37
* Create new customers
36
38
* Place orders
37
39
* Calculate prices for orders
38
40
* Filter products by supplier and category
39
41
42
+
See xref:#_use_the_api[] for example implementations.
43
+
44
+
45
+
== Create the GraphQL Data API
40
46
41
-
== Type definitions
47
+
See xref:getting-started/graphql-aura.adoc[] for steps on how to do this.
48
+
For the purpose of this tutorial, make sure to **Enable introspection** and **Enable field suggestions**.
42
49
43
-
First, make the nodes and relationships from xref:#_goal[] available:
50
+
51
+
=== Type definitions
52
+
53
+
Make the relevant nodes and relationships available by using these type definitions:
@@ -79,3 +90,274 @@ type ordersProperties @relationshipProperties {
79
90
}
80
91
----
81
92
93
+
Navigate to the link:https://studio.apollographql.com/sandbox/explorer[Apollo Studio] website and paste your GraphQL Data API URL to the **Sandbox** input.
94
+
Use the cog icon and add `x-api-key` and the API key for your data API under **Shared headers** and **Save**.
95
+
96
+
=== Make sure the API is working
97
+
98
+
Verify that the relevant parts of the Northwind data set are accessible:
99
+
100
+
[source, graphql, indent=0]
101
+
----
102
+
query {
103
+
categories {
104
+
categoryName
105
+
}
106
+
}
107
+
----
108
+
109
+
You should see as the **Response**:
110
+
111
+
[source, json, indent=0]
112
+
----
113
+
{
114
+
"data": {
115
+
"categories": [
116
+
{
117
+
"categoryName": "Beverages"
118
+
},
119
+
{
120
+
"categoryName": "Condiments"
121
+
},
122
+
{
123
+
"categoryName": "Confections"
124
+
},
125
+
{
126
+
"categoryName": "Dairy Products"
127
+
},
128
+
{
129
+
"categoryName": "Grains/Cereals"
130
+
},
131
+
{
132
+
"categoryName": "Meat/Poultry"
133
+
},
134
+
{
135
+
"categoryName": "Produce"
136
+
},
137
+
{
138
+
"categoryName": "Seafood"
139
+
}
140
+
]
141
+
}
142
+
}
143
+
----
144
+
145
+
== Use the API
146
+
147
+
The following sections provide simple examples of how to use the API in a webshop scenario.
148
+
149
+
150
+
=== Creating new customers
151
+
152
+
The following mutation creates a new customer by the name of "Jane Doe":
153
+
154
+
[source, graphql, indent=0]
155
+
----
156
+
mutation {
157
+
createCustomers(
158
+
input: [
159
+
{
160
+
contactName: "Jane Doe"
161
+
}
162
+
]
163
+
) {
164
+
customers {
165
+
contactName
166
+
}
167
+
}
168
+
}
169
+
----
170
+
171
+
To make it generic, pipe the `contactName` from an input into the query and send the request:
172
+
173
+
[source, javascript, indent=0]
174
+
----
175
+
// JavaScript example
176
+
----
177
+
178
+
179
+
=== Placing an order
180
+
181
+
To place an order, create a new order node that is linked to a number of product nodes and a customer node:
Note that the `orderID` is modeled as a UUID and therefore differs from the order IDs on orders that were already part of the Northwind data set.
207
+
The data model could be extended to account for that by keeping track of the order IDs in a separate system, making sure that the IDs are unique.
208
+
209
+
The products, how many of each of them as well as the customer who places the order must be known.
210
+
A shopping basket typically hands this information to the routine that is placing the order:
211
+
212
+
[source, javascript, indent=0]
213
+
----
214
+
// JavaScript example
215
+
----
216
+
217
+
218
+
=== Calculate prices for orders
219
+
220
+
Calculating the price of an order is achieved by accessing the `unitPrice` and `quantity` fields of the products that share the `ORDERS` relationship with the order.
221
+
222
+
Retrieve an order by its ID as well as the products associated with it:
The product of `quantity` and `unitPrice` is the total cost, which in this case is 116.25.
275
+
276
+
// maybe add another JavaScript example here, that accesses the JSON response and actually calculates the amount. This could also be made generic, so that it is not reliant on the orderID used here.
277
+
278
+
Note that there is no `discount` field on the `ORDERS` relationship and it is unclear how taxation works in this scenario.
279
+
280
+
281
+
=== Filter products
282
+
283
+
To filter products by category and supplier, first query for the `categoryName`s and supplier `companyName`s:
284
+
285
+
[source, graphql, indent=0]
286
+
----
287
+
query {
288
+
categories {
289
+
categoryName
290
+
}
291
+
suppliers {
292
+
companyName
293
+
}
294
+
}
295
+
----
296
+
297
+
Subsequent queries can now yield a filtered product list.
0 commit comments