📜 ⬆️ ⬇️

GraphQL queries. From simple to more complex



Hello World! Much has been said about the essence of GraphQL, but much less about the queries themselves. From simple to more complex, I will cover the topic. Interested - welcome under cat.

If someone is not familiar with GraphQL, have patience and read my introductory tutorials or articles on Habré. So it will be much easier to deal with requests.


Test environments



Requests


After any changes on the server, the documentation of requests will be updated if someone is not up to date. It will also automatically highlight the presence of errors.
')


Let's start with two simple types of queries, Queries (data retrieval) and Mutations (data modification).

Queries


With a simple query, you can refer to the user field and tell what values ​​the server should return: firstName and lastName .

 query myQuery{ user { firstName lastName } } 

When working with GraphiQL, you can omit the query before curly braces. However, when writing requests on the client, if you do NOT explicitly specify the type of request, there will be an error.

Mutations


To change the data, you must specify what we want to replace and what:

 mutation myMutation{ UserCreate(firstName: "Jane", lastName: "Dohe") } 

Queries are asynchronous, and mutations are sequential.

Subsciptions


GraphQL has a third type - subscriptions.

 subscriptions mySubscription{ user { firstName lastName } } 

They are completely analogous to queries and everything that applies to queries is suitable for subscriptions.

So far, everything is simple, but it is not suitable for project development.

Arguments


Arguments allow you to do the obvious things - substitute your values ​​and make a request. It does not matter whether it is a query, mutation or subscription.

First we determine which arguments to use. Next, apply them to the query field.

 query myQuery($id: '1'){ user (id:$id){ firstName lastName } } 

Such a query will find the user with id = '1' and return us his firstName and lastName .

Variables


Here it is more interesting, you can substitute your values. On a bare GraphiQL, you need to define a variable and designate its type, in full accordance with the documentation.

 query myQuery($id: ID){ //  user (id:$id){ firstName lastName } } 

 { "id": "595fdbe2cc86ed070ce1da52" //  } 

On the working draft, it will look like this:



ID! - The exclamation point at the end of the type tells GraphQL that this field is required.

A simple user does not need to worry about how to create a request on the client. For the popular JS frameworks, everything is ready: vue-apollo , react-apollo , apollo-angular .

Subqueries


The above approaches are applicable when the required database field is at the first level (without nesting). An example of a model on MongoDB:

 // MongoDB schema const schema = new mongoose.Schema({ firstName: { type: String }, lastName: { type: String }, }) export const USER_MODEL = mongoose.model('users', schema) // GraphQL type const user = { firstName: { type: GraphQLString }, lastName: { type: GraphQLString }, } export const USER = new GraphQLObjectType({ name: 'USER', fields: user }) 

Example with nested schema:

 // MongoDB schema const schema = new mongoose.Schema({ firstName: { type: String }, lastName: { type: String }, secure: { public_key: { type: String }, private_key: { type: String }, }, }) . . . const secure = new GraphQLObjectType({ name: "public_key", fields: { public_key: { type: GraphQLString } } }) export const USER = new GraphQLObjectType({ name: "USER", fields: { firstName: { type: GraphQLID }, secure: { type: secure } } }) 

Query query will look like this:

 query myQuery($id: ID){ user (id:$id){ firstName secure { public_key } } } 


Mutations + Queries


Mutations do not have to return true/false Boolean values. If the server in the field type instead of GraphQLBolean specify the type of the model, then you get the following:

 mutation auth($email: String!, $password: String!) { auth(email: $email, password: $password) { id secure { public_key } } } 



Conclusion


I reviewed the main queries that you might encounter during the development process. Write your suggestions, wishes in the comments and click the up arrow if the article was helpful.

Thanks for attention.

Source: https://habr.com/ru/post/332850/


All Articles