Skip to content

Commit

Permalink
feat(graphql): add an extension for type-graphql integration
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed May 24, 2020
1 parent 971a7c7 commit d03365f
Show file tree
Hide file tree
Showing 41 changed files with 3,765 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@
# - Standby owner(s): @deepakrkris
/extensions/apiconnect @raymondfeng @deepakrkris

#
# GraphQL
#
# - Issue label: GraphQL
# - Primary owner(s): @raymondfeng
/extensions/graphql @raymondfeng

#
# Build & internal tooling
#
Expand Down
2 changes: 1 addition & 1 deletion docs/site/MONOREPO.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The [loopback-next](https://github.com/strongloop/loopback-next) repository uses
| [extension-logging](https://github.com/strongloop/loopback-next/tree/master/extensions/logging) | @loopback/extension-logging | Add Winston Logger and Fluentd integration |
| [extension-metrics](https://github.com/strongloop/loopback-next/tree/master/extensions/metrics) | @loopback/extension-metrics | Report metrics to Prometheus |
| [fixtures/mock-oauth2-provider](https://github.com/strongloop/loopback-next/tree/master/fixtures/mock-oauth2-provider) | _(private)_ | An internal app to mock the OAuth2 authorization flow login with a social app like facebook, google etc |
| [http-caching-proxy](https://github.com/strongloop/loopback-next/tree/master/packages/http-caching-proxy) | @loopback/http-caching-proxy | A caching HTTP proxy for integration tests. NOT SUITABLE FOR PRODUCTION USE! |
| [graphql](https://github.com/strongloop/loopback-next/tree/master/extensions/graphql) | @loopback/graphql | Provide GraphQL capability with type-graphql | | [http-caching-proxy](https://github.com/strongloop/loopback-next/tree/master/packages/http-caching-proxy) | @loopback/http-caching-proxy | A caching HTTP proxy for integration tests. NOT SUITABLE FOR PRODUCTION USE! |
| [http-server](https://github.com/strongloop/loopback-next/tree/master/packages/http-server) | @loopback/http-server | A wrapper for creating HTTP/HTTPS servers |
| [metadata](https://github.com/strongloop/loopback-next/tree/master/packages/metadata) | @loopback/metadata | Utilities to help developers implement TypeScript decorators, define/merge metadata, and inspect metadata |
| [model-api-builder](https://github.com/strongloop/loopback-next/tree/master/packages/model-api-builder) | @loopback/model-api-builder | Types and helpers for packages contributing Model API builders. |
Expand Down
1 change: 1 addition & 0 deletions extensions/graphql/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=true
27 changes: 27 additions & 0 deletions extensions/graphql/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) IBM Corp. 2019.
Node module: @loopback/graphql
This project is licensed under the MIT License, full text below.

--------

MIT License

MIT License Copyright (c) IBM Corp. 2019

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
131 changes: 131 additions & 0 deletions extensions/graphql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# @loopback/graphql

This module provides integration with [GraphQL](https://graphql.org/) using
[type-graphql](https://typegraphql.com/).

## Stability: ⚠️Experimental⚠️

> Experimental packages provide early access to advanced or experimental
> functionality to get community feedback. Such modules are published to npm
> using `0.x.y` versions. Their APIs and functionality may be subject to
> breaking changes in future releases.
## Basic Use

```ts
export class MyApplication extends BootMixin(RestApplication) {
constructor(config: ApplicationConfig) {
super(config);
this.projectRoot = __dirname;
this.component(GraphQLComponent);
this.configure(GraphQLBindings.GRAPHQL_SERVER).to({asMiddlewareOnly: true});
}
}
```

## Configure GraphQLServer

This package can be used in two flavors:

- As a server for LoopBack applications
- As a middleware for LoopBack REST applications

## 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.

```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,
) {}
}
```

## Boot GraphQL resolvers

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
npm run build
node dist/__tests__/fixtures/graphql-test/src/index.js
```

You should see the following messages:

```sh
Server is running at http://[::1]:3000
Try http://[::1]:3000/graphql
```

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

1. Copy the query to the right panel:

```graphql
query GetRecipe1 {
recipe(recipeId: "1") {
title
description
ratings
creationDate
ratingsCount(minRate: 2)
averageRating
ingredients
numberInCollection
}
}
```

2. Click on the run icon:

```json
{
"data": {
"recipe": {
"title": "Recipe 1",
"description": "Desc 1",
"ratings": [0, 3, 1],
"creationDate": "2018-04-11T00:00:00.000Z",
"ratingsCount": 1,
"averageRating": 1.3333333333333333,
"ingredients": ["one", "two", "three"],
"numberInCollection": 1
}
}
}
```

## Contributions

- [Guidelines](https://github.com/strongloop/loopback-next/blob/master/docs/CONTRIBUTING.md)
- [Join the team](https://github.com/strongloop/loopback-next/issues/110)

## Tests

Run `npm test` from the root folder.

## Contributors

See
[all contributors](https://github.com/strongloop/loopback-next/graphs/contributors).

## License

MIT
Loading

0 comments on commit d03365f

Please sign in to comment.