Closed Silthus closed 9 months ago
Thanks @vnugent for the feedback on Discord about discussing the pros and cons of using the existing GraphQL API vs. creating a new REST endpoint first.
For documentation purposes, I will continue the discussion here.
First off: you are absolutely right to discuss the pros and cons before going forward with such a change. You built such an amazing product and we are very happy to hear your thoughts and feedback about the change we are about to implement.
Frankly, we were so deep into coding, that we forgot to share our train of thought. So here is the pro and con list we went through before deciding to go with the extra /import
endpoint. We are eager to hear your thoughts about it and more than willing to change things if you disagree or have concerns.
import.json
This is done using a separate endpoint, swapping out the apollo-server package against the apollo-server-express package (see here for more details).
apollo-server
against apollo-server-express
This would be done by processing the json document on the client constructing a GraphQL query and sending it to the server.
We believe that doing the little bit of extra work and implementing a custom endpoint for importing bulk data is the right way to go.
The main reason for that is the transaction safety ensuring that a single .json
file can be imported, using multiple retries to fix possible errors, without breaking the import and leaving the database in a partial state.
The second biggest reason we prefer the server side import is the additional complexity it brings to changing the GraphQL endpoints. By separating it from the regular API, a new contributor or existing collaborator does not need to touch the frontend of the bulk importer when changing the GraphQL API. Also we don't really know how it would behave with large amounts of data.
Here are some additional ideas and thoughts we had regarding the import feature, which also influenced our decision:
The idea is to add an export function that allows exporting selected areas or regions with individual properties into a single json file which then can be edited and uploaded using the import page.
This would allow for a very easy way to update existing data. The export would be similar to the existing openbeta-export, except that it produces a single json file in the schema required by the import.
One issue with Openbeta is the quality of data. Adding such an export and import workflow could allow individual, non developers, to start cleaning up data in a larger scope.
@Silthus I'd be more comfortable if we still use GQL as the interface (not introducing REST in this PR). You can create a new GQL bulk mutation and reuse the all DB operations in import-json.ts. There's no need to call existing update APIs.
OK, I can understand why you are more comfortable with sticking to the GraphQL only API.
But I am not quite sure what you mean. Can you share a small example snippet or link to some docs? I just need to direction to head towards and will change the bulk import accordingly.
You can piggyback Area GQL. I'm using updateAreaSortingOrder
mutation as an example.
bulkUpload
or whatever.
@vnugent thanks that helped a lot!
I adjusted the code accordingly, the only thing left to do are some failing tests where the input doesn't match up.
Let me know what you think of the changes.
I intentionally left the exchange of the apollo-server-express package in there to decouple the tests from a real running server listening on a port on localhost. This significantly speeds up testing and reduces complexity without any disadvantages.
I have a small request to check both nullness and defined. Other than that we're good to merge.
Thanks I learned something again today :)
Hey Openbeta Team :)
As discussed in https://github.com/OpenBeta/open-tacos/issues/1031 it would be great to have a simple page where users can bulk import data using a simple, validated json schema.
This PR adds the required functionality to the backend, enabling the integration of a bulk import in the frontend. The frontend is also actively being worked on by @l4u532.
What this PR does:
apollo-server
toapollo-server-express
allowing the integration of additional http endpoints without changing the graphql api. You can read more about the swapping of the packages here: https://www.apollographql.com/docs/apollo-server/v3/integrations/middleware/#swapping-out-apollo-server/import
for authorized users, allowing the import of a defined json schema. The schema will be defined in the PR for the frontend.Let me know what you think. I will happily make any adjustments to the PR.