DEV Community

Jess Edwards
Jess Edwards

Posted on

GraphQL: A brief introduction into queries and mutations

You've probably heard of "queries" and "mutations" a bunch of times - in this article I'll go over the basics of what they are and how to use them.

In short

On a basic level, we can think of queries as doing the same thing as a GET request in a RESTful API. Mutations can be compared to methods like DELETE, PUT, and PATCH, or anything else that updates the data.

Queries

Queries are kinda like functions in JavaScript. They can be named or anonymous, take arguments or not take them, but they always return some sort of data.

{
  posts{
    title
    author
  }
}
Enter fullscreen mode Exit fullscreen mode

You can think of a query as asking for a specific field on an object. We can't query the whole object, we have to query the fields it contains.

// won't work
{
  posts{
  }
}
Enter fullscreen mode Exit fullscreen mode

GraphQL still recognises queries when they don't have a name or use the 'query' operation type. It's shorthand syntax, but it is better to specify the type of operation we make and give it a name. The operation type can be query, mutation, or subscription, and we have creative freedom in choosing a name for it.

It is best to choose a name that reflects what is happening (GetPosts to get all posts, UpdatePost to update posts etc.) and its conventional to use PascalCase when we name queries and mutations.

Arguments

Queries, like functions, can take arguments - in some cases they might even be required. If an argument (or field) is required, it'll have an exclamation mark after it and won't work if you try to query it without the parameter it requires.

query{
  postByID(id:3){
    title
  }
}
Enter fullscreen mode Exit fullscreen mode

Variables

In the code snippet above, we're writing our arguments directly into our query string, but there will be times when we want our arguments to be dynamic, because we might not know in advance what the query criteria will be, e.g. give me all the posts with ___ as the author.

We can make this work by using variables. To use variables, our query has to have an operation type and a name.

query GetPostByID($id:Int!){
  postByID(id:$id){
    title
  }
}

Enter fullscreen mode Exit fullscreen mode

We've given the query a name, declared the parameter it takes, and the type of that argument. We will have already declared that this query takes a parameter and the type of it in the Schema, so its good to check that whatever we're using here is the same as what we've already declared.

We can now replace the hard coded value in the query with $arg. This arg has to have the same name as the parameter. The $ doesn't do anything special, its just highlights the variable.

Using variables helps make your code more DRY and dynamic.

Default variables

We can provide default values by adding the default value after the type declaration.

query GetPostByID($id:Int!=2){
  postByID(id:$id){
    title
  }
}
Enter fullscreen mode Exit fullscreen mode

If we do this, we can call the query without passing a variable. When we do pass a variable the default is overridden.

Mutations

We write mutations in the same way. We use the mutations operation type, pass in the data we want to mutate, and can use variables to make the mutation more dynamic.

Testing this out for yourself

If you want to mess around with basic queries and mutations yourself you can do so with this super basic demo app. Happy coding :)

Top comments (2)

Collapse
 
stradivario profile image
Kristiqn Tachev

Hey nice simple and clean!

Maybe you will like to check what i have created when i found out about graphql before 5 years :)))

It is called @gapi (Graphql API)

github.com/Stradivario/gapi

It is a really easy and scalable graphql framework written in typescript.
There is a bunch of modules out there which are plug and play into the system.

I have started my journey with writing my first dependency injection called @rxdi it stands for Reactive Extension Dependency Injection written with the help of rxjs.
This dependency injection gives me the opportunity to create @gapi and many modules which works with the whole ecosystem.

You may look also at this link github.com/rxdi/rxdi-monorepo

Because @rxdi is so flexible i have decided to create also a Front end framework using web components you can find the starter here github.com/rxdi/starter-client-sid...

Cheers and wish you a great day!

Collapse
 
jah_edw profile image
Jess Edwards

Gapi sounds awesome, I'll check it out! Thanks for sharing :)