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
Now, this `db` object will be available to us within the `context` object in the resolver implementation. So, we might write something like this:
392
392
393
393
```
394
394
const resolvers = {
395
-
Query: {
396
-
author: (obj, args, context) => {
397
-
return context.db.execute('SELECT * FROM authors WHERE id=?', args.id);
398
-
},
399
-
},
395
+
Query: {
396
+
author: (obj, args, context) => {
397
+
return context.db.execute('SELECT * FROM authors WHERE id=?', args.id);
398
+
},
399
+
},
400
400
};
401
401
```
402
402
@@ -417,39 +417,39 @@ Ideally, our code for the resolver should look something like this:
417
417
import { AuthorModel } from './models/author';
418
418
419
419
const resolvers = {
420
-
Query: {
421
-
author: (obj, args, context) => {
422
-
return AuthorModel.find(args.id, context);
423
-
},
424
-
},
420
+
Query: {
421
+
author: (obj, args, context) => {
422
+
return AuthorModel.find(args.id, context);
423
+
},
424
+
},
425
425
};
426
426
```
427
427
428
428
Our `AuthorModel` object can be written as:
429
429
430
430
```
431
431
export const AuthorModel = {
432
-
find: (id, context) {
433
-
const dbResult = context.db.execute('SELECT * FROM authors WHERE id=?', id);
434
-
return new Author(dbResult);
435
-
}
432
+
find: (id, context) {
433
+
const dbResult = context.db.execute('SELECT * FROM authors WHERE id=?', id);
434
+
return new Author(dbResult);
435
+
}
436
436
};
437
437
```
438
438
439
439
where `Author` is a simple data class that contains properties of authors we care about. You might ask: what's the benefit of doing this? Now, we're no longer stuffing our GraphQL resolvers full of logic that's dependent on the database that we're using or the schema within the database. Instead, all the resolvers have to care about is the `Author` object. For example, let's say our `Author` GraphQL type has a `name` type. We may now implement this resolver as:
0 commit comments