Skip to content

Commit

Permalink
chore(graphql): add more information to README
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed May 26, 2020
1 parent f3e824e commit 5f6206d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 47 deletions.
71 changes: 59 additions & 12 deletions extensions/graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,66 @@ This package can be used in two flavors:
- As a server for LoopBack applications
- As a middleware for LoopBack REST applications

## Add a resolver

Please add `recipe-resolver.ts` to `src/graphql-resolvers`.

```ts
@resolver(of => Recipe)
export class RecipeResolver implements ResolverInterface<Recipe> {
constructor(
// constructor injection of service
@repository('RecipeRepository')
private readonly recipeRepo: RecipeRepository,
@service(RecipeService) private readonly recipeService: RecipeService,
) {}

@query(returns => Recipe, {nullable: true})
async recipe(@arg('recipeId') recipeId: string) {
return this.recipeRepo.getOne(recipeId);
}

@query(returns => [Recipe])
async recipes(): Promise<Recipe[]> {
return this.recipeRepo.getAll();
}

@mutation(returns => Recipe)
async addRecipe(@arg('recipe') recipe: RecipeInput): Promise<Recipe> {
return this.recipeRepo.add(recipe);
}

@fieldResolver()
async numberInCollection(@root() recipe: Recipe): Promise<number> {
const index = await this.recipeRepo.findIndex(recipe);
return index + 1;
}

@fieldResolver()
ratingsCount(
@root() recipe: Recipe,
@arg('minRate', type => Int, {defaultValue: 0.0})
minRate: number,
): number {
return this.recipeService.ratingsCount(recipe, minRate);
}
}
```

## Use LoopBack dependency injections in resolver classes

All of LoopBack decorators for dependency injection , such as `@inject`,
`@service`, `repository`, and `config`, can be used with resolver classes.
`@service`, `@repository`, and `@config`, can be used with resolver classes.

```ts
import {service} from '@loopback/core';
@resolver(of => Recipe)
export class RecipeResolver implements ResolverInterface<Recipe> {
constructor(
// constructor injection of service using LoopBack
@service()
private readonly recipeService: RecipeService,
// constructor injection of service
@repository('RecipeRepository')
private readonly recipeRepo: RecipeRepository,
@service(RecipeService) private readonly recipeService: RecipeService,
) {}
}
```
Expand All @@ -54,14 +101,6 @@ export class RecipeResolver implements ResolverInterface<Recipe> {
The `GraphQLComponent` contributes a booter that discovers and registers
resolver classes from `src/graphql-resolvers` during `app.boot()`.

- recipe-resolver.ts

## Installation

```sh
npm install --save @loopback/graphql
```

## Try it out

```sh
Expand All @@ -77,6 +116,8 @@ Try http://[::1]:3000/graphql

Open http://127.0.0.1:3000/graphql in your browser to play with the GraphiQL.

![graphql-demo](graphql-demo.png)

1. Copy the query to the right panel:

```graphql
Expand Down Expand Up @@ -113,6 +154,12 @@ query GetRecipe1 {
}
```

## Installation

```sh
npm install --save @loopback/graphql
```

## Contributions

- [Guidelines](https://github.com/strongloop/loopback-next/blob/master/docs/CONTRIBUTING.md)
Expand Down
Binary file added extensions/graphql/graphql-demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {plainToClass} from 'class-transformer';
import {RecipesDataSource} from '../datasources/recipes.datasource';
import {RecipeInput} from '../graphql-types/recipe-input';
import {Recipe} from '../graphql-types/recipe-type';
import {RecipeRelations} from '../models';

@bind({
scope: BindingScope.SINGLETON,
Expand All @@ -27,7 +26,7 @@ export class RecipeRepository
extends DefaultCrudRepository<
Recipe,
typeof Recipe.prototype.id,
RecipeRelations
{}
>
implements LifeCycleObserver {
private static idCounter = 0;
Expand Down

0 comments on commit 5f6206d

Please sign in to comment.