-
Notifications
You must be signed in to change notification settings - Fork 3
GraphQL (Construction Process of Mutation)
Naomi Trevino edited this page Sep 20, 2023
·
5 revisions
- Write the mutation query in SQL. This goes in
/types/queries
. This particular one updates the document table in the database.
- This gets used in
/src/database_sql.rs
as a DataLoader.DocumentMetadataUpdate
is a GraphQLInputObject
defined elsewhere in a rust file (/src/document.rs
) and will match with what we give it in the front end.
- This is the
InputObject
. All fields under it also need to beInputObject
s as well or it will not compile. Here,written_at
has a customDateInput
type that will allow it to be used as a field here, since theDate
type based off the built-inDate
type from thechrono
library is not one.
- This is the
DateInput
type created for this mutation. Notice the function on line 57. It allows us to convertDateInput
intoDate
. And if you look back to the image on point 2 line 408, we map that conversion in order to send the date values into the query as aDate
type. The front-endDatePicker
generates a Date type, we convert it into aDateInput
and use that down untildatabase_sql.rs
where we change it back into theDate
type.
- This part in
/graphql/src/query.rs
connects the back-end to the GraphQL front-end. We can give it a UserGroup guard as done here if needed. It calls the DataLoader fromdatabase_sql.rs
and runs it.
- This part in
/graphql/src/queries.graphql
connects directly to that function inqueries.rs
shown in point 5. In line 327, it needs to take in the InputObject being used for the mutation. The name (in yellow can be anything). In line 328,updateDocumentMetadata
is what it needs to be named. It is thecamelCase
version of the function inqueries.rs
. If you put something like,updateDocumentMetadata2
, it won’t link to the function inquery.rs
and won’t work.
- To use the mutation in the TypeScript front-end, we need to create a form context to send our user-inputted values into the GraphQL mutation (which connects further down to change the SQL database).
In line 19, declare document as the fragment in queries.graphql. This provides a structure for how we send data into the form. You can see it is based off the fields existing in the AnnotatedDoc type in index.ts. It is called again line 32 downwards
(values: { document },)
See therunUpdate
on line 44.document
is of typeDocumentMetadataUpdate InputObject
defined indocument.rs
. Forid, title, and writtenAt
, we pass in the values collected from the fragment. Right hand side values need to be convertible into left hand side values. In the case ofwrittenAt
, (if a value is inputted) we send it in as a typescript object{date, month year}
. In point 4 theDateInput
object can be created in that way. So it can be passed in like that.
- The last step is the part that is actually visible by the user on the website. This
EditDocPanel
component has thedocument
prop, since that is where we extract the document id to be sent into the mutation. Line 68 callsuseForm()
which is imported from the form context at the bottom of point 7's image.
- This part of the
EditDocPanel
component handles the input of theid
andwrittenAt
values. Whenever a change is made to theDatePickerComponent
, the state changes to reflect that. InuseEffect
, we manually push theid
anddate
values to be used in the form. This is how to send predetermined values into the form. This function is found onunstable_FormState<V>
in the node module/website/node_modules/reakit/ts/Form/FormState.d.ts
file
edit-doc-data-panel.tsx
collects user input and sends it to form defined in edit-doc-data-form-context.tsx
. With those values, it calls runUpdate
which uses the GraphQL UpdateDocumentMetadata
mutation.
Line 328 updateDocumentMetadata
is the camelCase
of update_document_metadata
in queries.rs
and links this GraphQL mutation to it. Then, it calls the DataLoader
in database_sql.rs
which calls the SQL file mutation which updates the database.