Query
Query is for retrieving results, similar to GET
in REST API.
Client side
Query can be done using the following format:
{
users {
username
}
}
However, it's recommended to add an operaton name as it will be easier to debug.
query GetUserName {
users {
username
}
}
Note: if you want to execute 2 query of the same name, you will need to use aliases, for example it's not possible to do the following
query GetUserName {
users(id: 123456) {
username
}
users(id: 343456) {
username
}
}
Serverside
We can start building our resolver that returns all the users, with database
object value as this.
var rootValue = {
users: () => {
return database;
},
}
For this query to work, we also needs to declare our schema type
var schema = buildSchema(`
type Query {
users: [User]
}
`)
Note: It's technically possible to write query
to mutate the data (similar to mutation
) because of resolver. However by convention we're not doing so - just like it's also technically possible to write GET
to behave as POST