You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
GraphQL is a query lenguage that gets exactly the data that we ask for instead of static predefined responses.
21
21
22
-
First of all we are going to setup a mongo database to work with, we are not going to get in details with this so you can go to the https://docs.nestjs.com/techniques/mongodb[Nestjs documentation] to find more.
23
-
24
-
First we need to install a couple of dependencies:
Now let's create our schema, inside `todos/schemas` we are going to create a file `todo.schema.ts` with:
57
+
The `typePaths` indicates the location of the schema definition files.
48
58
49
-
[source,typescript]
50
-
----
51
-
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
52
-
import { Document } from 'mongoose';
59
+
The `definitions` indicates the file where the typescript definitions will automatically save, adding the `outputAs: 'class'` saves those definitions as classes.
53
60
54
-
@Schema()
55
-
export class Todo extends Document {
56
-
@Prop()
57
-
id!: number;
61
+
=== Playground
58
62
59
-
@Prop()
60
-
task: string | undefined;
63
+
Once we have imported GraphQL we can access it's playground by default on `http://localhost:3000/graphql`.
61
64
62
-
@Prop()
63
-
status: boolean | undefined;
64
-
}
65
+
The playground allow us to test or resolvers, we can call a query this way:
As we can see, we get a json "data" with an array of results.
84
97
85
-
To interact with the database we need a service, first we create a module with `devon4node generate module todos` and inside create the service `todos.service.ts`.
98
+
And for our mutations it's very similar, in this case we create a todo with task "rebuild house" and we are going to ask on the response just for the task data, we don't want the id.
In this case we return just one item so there is no array, we also got just the `task data` but if we want the `id`` too, we just have to add it on the request.
`@Resolver()` indicates that the next class is a resolver.
@@ -204,49 +194,37 @@ export class TodosResolver {
204
194
205
195
`@Mutation` is used to create or modify data.
206
196
207
-
The `@mutation` will create the next schema in or autogenerated schema file:
208
-
[source,typescript]
209
-
----
210
-
type Mutation {
211
-
createTodo( task: String ): Todo
212
-
}
213
-
----
214
-
215
-
And the `@Query` would do the same:
216
-
[source,typescript]
217
-
----
218
-
type Query {
219
-
todos: [Todo]
220
-
}
221
-
----
222
-
223
197
Here we have also an argument decorator `@Args` which is an object with the arguments passed into the field in the query.
224
198
225
-
Learn more about resolvers, mutations and their argument decorators on the https://docs.nestjs.com/graphql/resolvers#schema-first[NestJS documentation].
199
+
By default we can access the query or mutation using it's name, for example:
226
200
227
-
228
-
Now start the server and go to `http://localhost:3000/graphql` you should see a playground, here you can test your resolvers.
229
-
230
-
For try querys you only need to write a json with what you need:
201
+
For the `deleteTodo` mutation.
231
202
232
203
[source,typescript]
233
204
----
234
-
{
235
-
findAll{
205
+
mutation {
206
+
deleteTodo( id: "6f7ed2q8" ){
207
+
id,
236
208
task
237
209
}
238
210
}
239
211
----
240
212
241
-
To test the mutation you can:
213
+
But if we write something different on the decorator, we change the name, for example:
242
214
215
+
For the `findAll` query, we named it `todos`.
243
216
[source,typescript]
244
217
----
245
-
mutation{
246
-
createTodo (
247
-
task: "aasas"
248
-
){
249
-
id, task
218
+
{
219
+
todos{
220
+
id,
221
+
task
250
222
}
251
223
}
252
224
----
225
+
Also if we go back to the `schema.graphql`, we will see how we define the query with `todos`.
226
+
227
+
Learn more about resolvers, mutations and their argument decorators on the https://docs.nestjs.com/graphql/resolvers#schema-first[NestJS documentation].
228
+
229
+
230
+
Now start the server and go to `http://localhost:3000/graphql` you should see a playground, here you can test your resolvers.
0 commit comments