<style> .reveal { font-size: 35px; } li { margin-top: 20px; } .reveal blockquote { width: 100%; } </style> # Decision log: GraphQL Link til slides: https://hackmd.io/@skeen/graphql --- ## Hvorfor et nyt API i MO? MO's nuværende API: - Dårlig performance, især pga. overfetching og n+1 kald. - Frontend-specifik, men bruges til data integrationer. - Ingen CRUD operationer - Besværligt at arbejde med --- ## Motiverende eksempel Fra `fix_departments.py`: ``` mo_unit = self.helper.read_ou(org_uuid) if "status" in mo_unit: # Unit does not exist in MO logger.warning("Unknown unit {}, will create".format(org_uuid)) self.create_single_department(org_uuid, date) ``` Ønsket API kald: Se om organisationsenhed findes. Faktisk API kald til: `localhost:5000/service/ou/{uuid}/`. --- ## Svar fra /ou/{uuid}/ ``` { ... "settings": { ... }, "org": { ... }, "org_unit_type": { ... "top_level_facet": { ... }, "facet": { ... }, }, "time_planning": { ... "top_level_facet": { ... }, "facet": { ... }, }, "org_unit_level": { ... "top_level_facet": { ... }, "facet": { ... }, }, "parent": { ... "settings": { ... }, "org": { ... }, "org_unit_type": { ... "top_level_facet": { ... }, "facet": { ... }, }, "time_planning": { ... "top_level_facet": { ... }, "facet": { ... }, }, "org_unit_level": { ... "top_level_facet": { ... }, "facet": { ... }, }, "parent": { ... "settings": { ... }, "org": { ... }, "org_unit_type": { ... "top_level_facet": { ... }, "facet": { ... }, }, "time_planning": { ... "top_level_facet": { ... }, "facet": { ... }, }, "org_unit_level": { ... "top_level_facet": { ... }, "facet": { ... }, }, "parent": { ... "settings": { ... }, "org": { ... }, "org_unit_type": { ... "top_level_facet": { ... }, "facet": { ... }, }, "time_planning": { ... "top_level_facet": { ... }, "facet": { ... }, }, "org_unit_level": { ... "top_level_facet": { ... }, "facet": { ... }, }, "parent": null, } } } } ``` Faktisk svar: 500 linjer JSON. --- ## Trace af /ou/{uuid}/ ![](https://i.imgur.com/7Irk2oS.png) --- ## Løsning: Specifikke endepunkter? ``` class UnitDetails(enum.Enum): # name & userkey only MINIMAL = 0 # with child count NCHILDREN = 1 # with everything except child count SELF = 2 # same as above, but with all parents FULL = 3 # mininal and integration_data INTEGRATION = 4 # name, path and UUID PATH = 5 ``` En dystopisk fremtid: ``` MINIMAL_WITH_PATH = 121 ``` --- ## Hvad er GraphQL? > A query language for your API > > GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. > GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools. --- ## Hvordan virker det? Describe your data: ``` type Project { name: String tagline: String contributors: [User] } ``` Ask for what you want: ``` { project(name: "GraphQL") { tagline } } ``` Get predictable results: ``` { "project": { "tagline": "A query language for APIs" } } ``` --- ## Tilbage til vores eksempel Query: ``` query OrgUnitExistsQuery { org_units(uuids: ["{uuid_here}"]) { uuid } } ``` Exists: ``` { "data": { "org_units": [{"uuid": "{uuid_here}"}] } } ``` Does not exist: ``` { "data": { "org_units": [] } } ``` --- ## Trace af query ![](https://i.imgur.com/IC5ZLJJ.png) --- ## Benefits Forced to create an extensible Graph datamodel: ![](https://i.imgur.com/yz1hlZU.png) --- ## Benefits - One Graph model; Not lots and lots of data formats. - No under- or overfetching, just exactly what you need. - Decoupling between Graph model and data layer. - We can change the underlying data layer, make performance optimisations, etc. without affecting the endpoints. - GraphQL is an established framework, we can hire people with GraphQL experience. - Option to unify all users on one API, no need for client specific endpoints. --- ## Library <svg fill="none" viewBox="0 0 1952 2357" height="500"><path d="M811 809c-120 0-237-40-334-113-88-66-237-194-249-311a127 127 0 0130-98c24-28 58-44 101-47l13-1c77 0 177 43 272 114-7-88 18-153 71-175h1c9-4 18-6 28-7h5c56 0 99 58 131 125C935 143 1027 7 1124 0h7c14 0 28 3 41 9 136 59 78 330 29 464a576 576 0 01-292 324l-9 5-10 1a524 524 0 01-79 6z" fill="#1D1D1B"></path><path d="M842 736c-116 8-231-27-325-98-129-98-214-197-220-260a54 54 0 0113-43c12-15 29-22 53-24 76-5 196 55 299 149l97 89-34-130c-22-87-15-161 16-174l1-1 6-2c17-1 59 41 99 158l42 125 33-128c49-189 146-321 206-325 6 0 11 1 17 3 66 29 48 216-9 373a506 506 0 01-256 285l-38 3z" fill="url(#prefix__paint0_linear)"></path><path d="M1075 2357c-32-1-62-12-87-33-27 16-58 24-89 23-42 1-83-13-116-39-27-21-51-45-74-71-12 6-25 9-38 9-32 0-59-18-76-31a1669 1669 0 01-416-532c-12-23-65-131-71-221-34-45-60-96-75-150a205 205 0 0132-176c-53-61-77-152-59-245a626 626 0 01187-305c26-24 53-37 81-37a88 88 0 0168 29c27-31 60-57 96-76 33-18 71-28 109-28 40 0 80 12 114 34a237 237 0 01211-131 216 216 0 01157 69c7-12 14-23 22-33a133 133 0 01129-43c76 13 146 51 200 108l11-16a86 86 0 0165-31c6 0 12 0 17 2 28 5 75 26 126 95a83 83 0 0140 2h2a75 75 0 0185-14c155 69 206 304 222 489 7 80 13 262-66 332l-1 12c-28 277-143 579-301 789-8 10-24 31-50 42-20 47-50 87-103 88h-2c-11 0-23-2-33-7a96 96 0 01-59 32l-21 1-27-3-3 4a118 118 0 01-88 41h-3c-15 0-29-4-42-11a87 87 0 01-50 30l-24 2z" fill="#1D1D1B"></path><path d="M1870 1004c-12-134-51-372-180-429-33-15-3 102 3 138l-3-1c-13-32-47-112-78-120-26-7-28 29-19 73h-3c-29-65-85-158-139-169-42-8-32 113-19 199-56-131-143-239-273-262-113-20-105 163-94 270-17-127-97-295-247-255-88 23-125 103-138 196-34-92-130-134-220-86-63 33-119 99-145 173-5-59-23-158-85-99A553 553 0 0064 898c-19 94 20 201 116 231v5l-19 4c-61 18-86 89-71 147 15 53 41 102 77 144-5 69 47 183 63 214 93 180 237 383 397 508 81 63 45-67 22-134 55 97 83 162 167 227 60 48 172 23 156-54 31 72 74 94 104 87 25-5 30-64 22-115-6-38-7-44-15-82h4c17 63 53 177 96 178 39 0 65-45 60-102l-7-60c25 113 45 123 78 119 37-6 45-82 47-156 1-37 0-19-2-56l4-1c6 50 5 188 53 187 36-1 54-80 69-139-5 48-1 78 31 36 154-204 261-491 287-751 2-17 1-34-2-51 6 1 13 0 18-3 58-29 58-199 51-277zm-601-28h5c16 0 36 30 37 50 1 15-4 26-19 27-18 1-33-25-35-47-1-15 3-25 12-30zm34 198l7-2c7 37 22 108 45 159-14-4-30-6-45-6 6-48-1-111-7-151zm-236-448l4 34c5 43 20 167 44 293-21-48-51-88-97-102l-1-8c54-64 58-139 50-217zm34 680c51-5 64-105 60-166 16 50 33 92 53 119-59 56-62 176-52 281-15-121-79-245-200-256 7-54 1-129-5-169-5-38-13-78-20-115l5-1c24 80 75 314 159 307zM951 927c-20 4-38-23-41-51-2-21 8-34 18-36 18-4 36 26 39 53 2 14 1 30-16 34zm-647-81c9 61 41 89 90 72 7-2 25-18 31-23-16 80-19 162-9 243 10 78 44 197 104 271-106 15-89 124-58 214 17 50 38 100 61 150-31-35-68-60-110-62l-2-5c21-10 45-36 9-133-9-23-19-45-31-66 69-53 43-223 31-287-20-107-65-262-196-279v-5c35-21 63-52 80-90zm55 831c-15-3-34-26-43-45-5-12-4-22 5-21 12 2 36 29 44 47 5 12 6 21-6 19zm79 135c-14-4-34-28-44-47-6-13-5-23 4-21 12 3 38 33 47 51 5 12 5 21-7 17zm245-166l-2-6c36-10 79-9 57-115-6-28-21-52-42-70 51-49 43-169 32-228-23-127-67-271-141-377 44 10 73-20 90-60 4 59 11 114 17 155 12 93 80 361 161 463-75 58-58 203-32 315-33-45-79-78-140-77zm161 368c25-13 38-36 43-63 13 41 26 82 41 122-19-28-44-48-79-42l-5-17zm225-149c-15 3-32-26-37-53-4-21 2-33 10-34 14-3 35 27 40 53 2 15-1 32-13 34zm63 24l-1-8c37-67 40-137 34-209l17 124c4 25 17 110 35 204-16-50-41-94-85-110v-1zm246-121c-15 6-27-18-29-44-2-20 7-33 14-36 14-5 30 20 31 45 0 13-4 30-16 35zm125 172c5-67 0-156-67-164v-8c37-57 59-123 64-191 19 48 50 86 98 69-50 92-76 192-95 293v1zm166-500c-4-70-53-154-126-143-55 9-70 79-71 132-9-18-20-35-34-49 59-41 75-219 73-288v-13c39 138 90 263 146 247 45-14 60-146 62-205h6c5 36 15 72 28 106l-3 1c-73 20-83 139-81 212z" fill="url(#prefix__paint1_linear)"></path><path d="M325 340c10 5-11 72 26 115-36-27-50-73-37-101 4-9 8-15 11-14zm463-30c-6 1-19-42-48-48 11-1 21 3 30 9 9 7 15 16 18 27v12zm374-63c-4 8-7 13-9 11-5-7 20-79 5-137 17 40 18 96 4 126zm466 1126c0 6-1 12-3 13-10 2-16-51-65-65 41 3 68 27 68 52m143-683c1 6 0 10-1 10-4-3-15-53-42-84 21 18 39 51 43 74zm-117-16l-1 8c-4-2-11-43-32-66 18 15 32 40 33 58zm-116-61c3 9 4 15 1 14-9-1-38-71-87-103 39 17 76 57 86 89zm-221-60c2 9 0 18-2 17-36-17-73-85-126-111 48 12 116 46 128 94zm-46 514a76 76 0 01-45-73c1-10 2-16 6-16 7 1 6 64 39 89zm-203-37c1 8 1 15-1 15-9 1-26-67-68-81 17 2 33 9 45 21 13 12 21 28 24 45zM881 876c8-1 22 66 65 81-35-1-64-32-67-63-1-10-1-17 2-18zm66-344c3 10-1 19-3 19-37-10-74-77-141-77 51-13 130 2 144 58zm-333 98c1 11 0 21-4 23-22 9-51-75-132-60 68-25 130-4 136 37zM247 976c41-5 76 37 87 78 3 12-3 19-6 20-12 4-32-87-81-98zm39-261c-1 14-3 26-7 28-15 9-18-88-55-77 37-24 65 0 62 49zm0 918c-3-8-3-11-1-10 3 1 23 44 48 64-20-12-37-31-47-54zm89 133c-2-5-2-8-1-7 4 1 22 40 45 59-19-13-34-31-44-52zm165 114c-12-5-51-95-110-140 46 24 96 83 109 122 3 11 4 19 1 18zm264-102c-14 0-58-77-129-102 56 8 113 48 127 83 4 12 5 19 2 19zm140 381c-9-4-28-61-73-96 38 19 70 55 74 83 2 8 1 13-1 13zm-260-673c-12 4-79-54-154-53 56-11 122 7 146 36 8 9 11 16 8 17zm262-54c47 5 101 23 112 68 3 10-1 19-3 19-30-6-68-55-115-78l6-9zm55 391c-2-10-2-17 0-17 7-1 26 71 62 90-28-4-55-41-62-73zm175 203c-7 2-21-71-58-86 29 2 55 33 59 69 1 9 1 16-1 17zm155-306c2-10 4-17 6-17 7-1-3 67 21 84-23-4-33-38-27-67zm141 141c0 8-1 15-3 16-7 5-9-60-45-63 30-7 50 15 48 47zm-63-414c-20 5-39-52-117-72l2-4c68 4 119 33 121 60 1 9-2 15-6 16zm374-129c0 6-2 10-3 10-5 2-6-43-35-57 11 2 21 8 28 16 7 9 10 20 10 31z" fill="#fff"></path><defs><linearGradient id="prefix__paint0_linear" x1="679.3" y1="82.8" x2="921.6" y2="743.4" gradientUnits="userSpaceOnUse"><stop stop-color="#11998E"></stop><stop offset="1" stop-color="#38EF7D"></stop></linearGradient><linearGradient id="prefix__paint1_linear" x1="849.7" y1="512.3" x2="1259.1" y2="2299.7" gradientUnits="userSpaceOnUse"><stop stop-color="#EE0979"></stop><stop offset="1" stop-color="#FF6A00"></stop></linearGradient></defs></svg> https://strawberry.rocks/
{"metaMigratedAt":"2023-06-16T15:59:49.812Z","metaMigratedFrom":"YAML","title":"Decision log: GraphQL","breaks":true,"description":"Om hvorfor GraphQL blev introduceret til MO","slideOptions":"{\"transition\":\"fade\"}","contributors":"[{\"id\":\"2f63493c-0d49-42cd-a9ff-01ea06ac3a7b\",\"add\":30123,\"del\":18926}]"}
    538 views