|
| 1 | += GraphQL and Aura Console |
| 2 | + |
| 3 | +This tutorial shows you how to create and use a GraphQL Data API in Aura Console. |
| 4 | + |
| 5 | +== Prerequisites |
| 6 | + |
| 7 | +Set up an AuraDB instance. |
| 8 | +Refer to link:https://neo4j.com/docs/aura/getting-started/create-instance/[Creating a Neo4j Aura instance]. |
| 9 | + |
| 10 | + |
| 11 | +== Create a GraphQL Data API |
| 12 | + |
| 13 | +In the Aura Console, select **Data services** > **Data APIs** from the left side navigation and then **Create API**. |
| 14 | + |
| 15 | + |
| 16 | +=== Details |
| 17 | + |
| 18 | +Provide a name for your new API as well the instance data for the instance you want to use under **Details**. |
| 19 | +Also do **Enable introspection** and **Enable field suggestions** for use with xref:#_via_apollo_server[Apollo Server]. |
| 20 | + |
| 21 | +[CAUTION] |
| 22 | +==== |
| 23 | +If you set **Enable introspection** and **Enable field suggestions** for production systems the information they provide can be used by malicious actors to reverse-engineer your GraphQL schema and execute arbitrary operations. |
| 24 | +
|
| 25 | +**Enable introspection** allows you to query the schema and discover the available queries, mutations, subscriptions, types and fields in the GraphQL API. |
| 26 | +
|
| 27 | +**Enable field suggestions** provides suggestions that hint towards GraphQL typos. |
| 28 | +Even with just field suggestions enabled, it is possible for a malicious actor to discover your entire schema. |
| 29 | +==== |
| 30 | + |
| 31 | + |
| 32 | +=== Type definitions |
| 33 | + |
| 34 | +Paste these **Type definitions**: |
| 35 | +[source, graphql, indent=0] |
| 36 | +---- |
| 37 | +type Product @node { |
| 38 | + productName: String |
| 39 | + category: [Category!]! @relationship(type: "PART_OF", direction: OUT) |
| 40 | +} |
| 41 | +
|
| 42 | +type Category @node { |
| 43 | + categoryName: String |
| 44 | + products: [Product!]! @relationship(type: "PART_OF", direction: IN) |
| 45 | +} |
| 46 | +---- |
| 47 | + |
| 48 | +The type definitions describe what parts of the graph database in the AuraDB are accessible by requests made via the GraphQL API. |
| 49 | + |
| 50 | +Alternatively, if you already have data in the AuraDB, you can obtain your type definitions via the https://graphql-toolbox.neo4j.io[Neo4j GraphQL Toolbox]. |
| 51 | +The toolbox can connect to the AuraDB and automatically create type definitions and allow GraphQL operations. |
| 52 | + |
| 53 | +If you are using link:https://graphql.org/learn/federation/[GraphQL Federation] then make sure to select **Enable GraphQL subgraph**. |
| 54 | + |
| 55 | + |
| 56 | +=== Cross-Origin Resource Sharing (CORS) policy |
| 57 | + |
| 58 | +To use your GraphQL API with a browser-based application, add an entry in your Cross-Origin Resource Sharing (CORS) policy. |
| 59 | +CORS is a browser-based security mechanism that prevents web pages from accessing resources from a server with a different origin. |
| 60 | + |
| 61 | +Add the URL `https://studio.apollographql.com` to the **Origin** box to enable the use of https://studio.apollographql.com/[Apollo Studio]. |
| 62 | +If you need more entries, **Add allowed origin** for those. |
| 63 | + |
| 64 | +[NOTE] |
| 65 | +==== |
| 66 | +The URL entered in the CORS policy must be an exact match. |
| 67 | +Wildcards are not supported. |
| 68 | +==== |
| 69 | + |
| 70 | + |
| 71 | +=== Authentication providers |
| 72 | + |
| 73 | +All requests to the GraphQL API are authenticated and there are two options for the type of authentication: API key or JSON Web Key Set (JWKS). |
| 74 | +It is possible to set up multiple authentication providers and select a different one for each request. |
| 75 | + |
| 76 | +For the purpose of this tutorial, avoid setting up a JWKS endpoint, select **API Key** from the dropdown list and enter a name. |
| 77 | +The API key will be shown after the GraphQL API key has been created. |
| 78 | + |
| 79 | +[CAUTION] |
| 80 | +==== |
| 81 | +It is not recommended to use API keys with user-facing applications that are using the GraphQL API. |
| 82 | +This is due to the risk of the API key being visible to malicious users and very little control over GraphQL operations. |
| 83 | +
|
| 84 | +A 3rd party identity provider with JWKS provides better security and allows for granular security rules, based on information within the JWKS token, to be defined within the type definitions to control GraphQL operations. |
| 85 | +==== |
| 86 | + |
| 87 | + |
| 88 | +=== Sizing |
| 89 | + |
| 90 | +Sizing a GraphQL API is based on the size and complexity of the type definitions and the expected workload. |
| 91 | +Larger sizes have a higher hourly cost. |
| 92 | +If the cost is not displayed, refer to the agreement you have with Neo4j. |
| 93 | + |
| 94 | + |
| 95 | +== API key |
| 96 | + |
| 97 | +**Create** the GraphQL API. |
| 98 | + |
| 99 | +Your API key is now displayed. |
| 100 | +Store this information securely as it cannot be retrieved again. |
| 101 | +Confirm with **I understand** and **Close**. |
| 102 | + |
| 103 | +The GraphQL API will be provisioned. |
| 104 | +You can see the status via **Data APIs** from the left side navigation. |
| 105 | + |
| 106 | + |
| 107 | +== Create nodes in the database |
| 108 | + |
| 109 | +When the status for the GraphQL API is **Ready** you can send GraphQL requests to it. |
| 110 | + |
| 111 | + |
| 112 | +=== Via cURL |
| 113 | + |
| 114 | +[source, bash, indent=0] |
| 115 | +---- |
| 116 | +curl --location <YOUR_GRAPHQL_API_URL> --header 'Content-Type: application/json' --header 'x-api-key: <YOUR_API_KEY>' --data '{ "query": "<YOUR_GRAPHQL_QUERY>" }' |
| 117 | +---- |
| 118 | + |
| 119 | +On the command line, execute: |
| 120 | + |
| 121 | +[source, bash, indent=0] |
| 122 | +---- |
| 123 | +curl --location <YOUR_GRAPHQL_API_URL> --header 'Content-Type: application/json' --header 'x-api-key: <YOUR_API_KEY>' --data '{ "query": "mutation { createProducts( input: [ { productName: \"New Product\" category: { create: [ { node: { categoryName: \"New Category\" } } ] } } ] ) { products { productName category { categoryName } } } }" }' |
| 124 | +---- |
| 125 | + |
| 126 | +Verify by querying the data you just added: |
| 127 | + |
| 128 | +[source, bash, indent=0] |
| 129 | +---- |
| 130 | +curl --location <YOUR_GRAPHQL_API_URL> --header 'Content-Type: application/json' --header 'x-api-key: <YOUR_API_KEY>' --data '{ "query": "query { products { productName category { categoryName } } }" }' |
| 131 | +---- |
| 132 | + |
| 133 | +You should see this: |
| 134 | + |
| 135 | +[source, bash, indent=0] |
| 136 | +---- |
| 137 | +{ "data": { "products": [ { "productName": "New Product", "category": [{ "categoryName": "New Category" }] } ] } } |
| 138 | +---- |
| 139 | + |
| 140 | + |
| 141 | +=== Via Apollo Server |
| 142 | + |
| 143 | +. On the https://studio.apollographql.com/sandbox/explorer[Apollo Studio] website, paste your GraphQL Data API URL to the **Sandbox** input. |
| 144 | +. Use the cog icon and add `x-api-key` and the API key for your data API under **Shared headers** and **Save**. |
| 145 | +. To start adding data, copy and paste the following mutation to the **Operation** panel to create a product and category in that product: |
| 146 | ++ |
| 147 | +[source, graphql, indent=0] |
| 148 | +---- |
| 149 | +mutation { |
| 150 | + createProducts( |
| 151 | + input: [ |
| 152 | + { |
| 153 | + productName: "New Product" |
| 154 | + category: { create: [{ node: { categoryName: "New Category" } }] } |
| 155 | + } |
| 156 | + ] |
| 157 | + ) { |
| 158 | + products { |
| 159 | + productName |
| 160 | + category { |
| 161 | + categoryName |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | +} |
| 166 | +---- |
| 167 | + |
| 168 | +. Use **Run** on the top right. |
| 169 | +You should get the following confirmation that the data has been created in the database in the **Response** panel: |
| 170 | ++ |
| 171 | +[source, json, indent=0] |
| 172 | +---- |
| 173 | +{ |
| 174 | + "data": { |
| 175 | + "createProducts": { |
| 176 | + "products": [ |
| 177 | + { |
| 178 | + "productName": "New Product", |
| 179 | + "category": [ |
| 180 | + { |
| 181 | + "categoryName": "New Category" |
| 182 | + } |
| 183 | + ] |
| 184 | + } |
| 185 | + ] |
| 186 | + } |
| 187 | + } |
| 188 | +} |
| 189 | +---- |
| 190 | + |
| 191 | +. Query the data you just added. |
| 192 | +Copy and paste the following query to the **Operations** panel: |
| 193 | ++ |
| 194 | +[source, graphql, indent=0] |
| 195 | +---- |
| 196 | +query { |
| 197 | + products { |
| 198 | + productName |
| 199 | + category { |
| 200 | + categoryName |
| 201 | + } |
| 202 | + } |
| 203 | +} |
| 204 | +---- |
| 205 | ++ |
| 206 | +Since you only created one "Product" node and one "Category" node, the **Response** panel shows the following: |
| 207 | ++ |
| 208 | +[source, json, indent=0] |
| 209 | +---- |
| 210 | +{ |
| 211 | + "data": { |
| 212 | + "products": [ |
| 213 | + { |
| 214 | + "productName": "New Product", |
| 215 | + "category": [ |
| 216 | + { |
| 217 | + "categoryName": "New Category" |
| 218 | + } |
| 219 | + ] |
| 220 | + } |
| 221 | + ] |
| 222 | + } |
| 223 | +} |
| 224 | +---- |
| 225 | + |
| 226 | + |
| 227 | +This concludes the tutorial. |
| 228 | +You now have a GraphQL Data API connected to a Neo4j AuraDB and you have added two nodes. |
| 229 | + |
| 230 | +To learn more, see xref:queries-aggregations/index.adoc[Queries and aggregations] and xref:getting-started/toolbox.adoc[Neo4j GraphQL Toolbox]. |
| 231 | +For more advanced database settings, refer to xref:driver-configuration.adoc[Driver configuration]. |
| 232 | + |
| 233 | +// Edit at: https://github.com/neo4j-graphacademy/courses/blob/main/asciidoc/courses/graphql-basics/promo.adoc |
| 234 | +include::https://raw.githubusercontent.com/neo4j-graphacademy/courses/main/asciidoc/courses/graphql-basics/promo.adoc[] |
0 commit comments