diff --git a/extensions/graphql/README.md b/extensions/graphql/README.md index 11dff2ad0710..5eb394181527 100644 --- a/extensions/graphql/README.md +++ b/extensions/graphql/README.md @@ -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 { + 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 { + return this.recipeRepo.getAll(); + } + + @mutation(returns => Recipe) + async addRecipe(@arg('recipe') recipe: RecipeInput): Promise { + return this.recipeRepo.add(recipe); + } + + @fieldResolver() + async numberInCollection(@root() recipe: Recipe): Promise { + 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 { 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, ) {} } ``` @@ -54,14 +101,6 @@ export class RecipeResolver implements ResolverInterface { 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 @@ -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 @@ -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) diff --git a/extensions/graphql/graphql-demo.png b/extensions/graphql/graphql-demo.png new file mode 100644 index 000000000000..a050867b1e7b Binary files /dev/null and b/extensions/graphql/graphql-demo.png differ diff --git a/extensions/graphql/src/__tests__/fixtures/graphql-test/src/models/index.ts b/extensions/graphql/src/__tests__/fixtures/graphql-test/src/models/index.ts deleted file mode 100644 index 01c75564d759..000000000000 --- a/extensions/graphql/src/__tests__/fixtures/graphql-test/src/models/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -// Copyright IBM Corp. 2019. All Rights Reserved. -// Node module: @loopback/graphql -// This file is licensed under the MIT License. -// License text available at https://opensource.org/licenses/MIT - -export * from './recipe.model'; diff --git a/extensions/graphql/src/__tests__/fixtures/graphql-test/src/models/recipe.model.ts b/extensions/graphql/src/__tests__/fixtures/graphql-test/src/models/recipe.model.ts deleted file mode 100644 index 6bfe8b34e46b..000000000000 --- a/extensions/graphql/src/__tests__/fixtures/graphql-test/src/models/recipe.model.ts +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright IBM Corp. 2019. All Rights Reserved. -// Node module: @loopback/graphql -// This file is licensed under the MIT License. -// License text available at https://opensource.org/licenses/MIT - -import {Entity, model, property} from '@loopback/repository'; - -@model() -export class Recipe extends Entity { - @property({ - type: 'string', - id: true, - generated: false, - }) - id: string; - - constructor(data?: Partial) { - super(); - Object.assign(this, data); - } -} - -export interface RecipeRelations { - // describe navigational properties here -} - -export type RecipeWithRelations = Recipe & RecipeRelations;