Let’s create a simple ‘to do’ application using Svelte and GraphQL. The objective is to demonstrate getting started on a GraphQL query and insert operation in a Svelte component.
If you are new to Svelte, you may want to go over what Svelte can do from the official site, or see Svelte quick overview.
Also, we will not quite dwell on the backend server in this post - you can follow along if you have any GraphQL backend. If not, I suggest you get started with a GraphQL setup of your own in the next 15 min. I will use the data structure and statements from the server setup described in that post.
I gather you would have installed degit
to make your svelte
life easier.
Create a new project using degit
.
|
|
This will copy svelte template and install all required components.
We will also install Apollo client for GraphQL. Go back to command window and do another npm
.
|
|
Run your application and say ‘hello’ to the world.
|
|
Open the project folder in VS Code and start changing stuff.
main.js
Change your main.js
for fun.
|
|
App.svelte
Open App.svelte
. There are two main things we do here -
- Point Apollo client towards our GraphQL server URL
- Create a
Todo
Svelte component (we will create this next)
Also, centre the whole thing. Never trust developers who don’t put content at the centre.
|
|
You might remember from the server setup post linked earlier that we are running GraphQL server at port 5050. Other code is largely self explanatory and not that important.
Todo.svelte
Create a new file Todo.svelte
in the same directory as App.svelte
. We will code most of our application here.
First: write the GraphQL queries - one for query and one for insert, and get the client ready.
|
|
Next: add functionality to fetch to-do from server. You can incrementally add code to perform query and update operations within the <script>
block.
|
|
Add a function to insert a to-do record, and declare a variable which will hold the to-do value temporarily.
|
|
Typically, we would want to just take the input, add to the list of to-dos, and call it a day. Here, we are ’re-fetching’ the results after insert just because we can.
PostGraphile passes back the the field values in update operation back in its response, other technologies/frameworks may not. This is where re-fetch has a role to play. Depending on your use case, you may or may not want to throw a query at server immediately after the update (or do it in one go on the server, which is the sane thing to do).
Add the HTML to the same Todo.svelte
file to render the results fetched from server -
|
|
We do the following tasks in the above block -
- Wait for the fetch operation from the function. The function gets executed when the component gets loaded. The
Todo.svelte
component loads when called byApp.svelte
, which itself is called bymain.js
- A helpful message
...loading
is displayed till the data is fetched - Once data is fetched, we use the standard GraphQL node traversal to fetch
nodes
from result and display them on the page using a{#each}
loop. - Catch any exceptions
By now, you should see the to-do records fetched and displayed on the page.
Add a form element before or after the list displayed in the previous section. This comprises of just a single input box and a submit
button.
|
|
Bind the input box to the (temporary) to-do variable we had declared earlier. We prevent default action on button click so that the page does not go into wild, unnecessary refresh.
And, there it is - your beautiful looking app that can insert or query to-do’s.
Your app can now insert to-do records and fetch those to-do’s - what more can a human ask for?
Full Code for Todo.svelte
Copy/paste was never made more easier (until next section - you can just clone the repository from Github).
|
|
Final final code
Find all code on GitHub