Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add tutorial to expose GraphQL APIs #1899

Merged
merged 1 commit into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions docs/site/exposing-graphql-apis.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
---
lang: en
title: 'Exposing GraphQL APIs'
keywords: LoopBack 4.0, LoopBack 4, GraphQL
sidebar: lb4_sidebar
permalink: /doc/en/lb4/exposing-graphql-apis.html
---

## Overview

The [OASGraph module](https://www.npmjs.com/package/oasgraph) creates a GraphQL
wrapper for existing REST APIs which are described by the OpenAPI specification.
This tutorial shows how to expose GraphQL APIs in an existing LoopBack
application.

### Prerequisite

Make sure you have a running LoopBack 4 application. In this tutorial, we'll use
the `todo` example. You can create this application by running the command
below:

```sh
lb4 example todo
```

### Install OASGraph and Required Dependencies

From your LoopBack application, run the following command to install OASGraph
and the required dependencies:

```sh
npm i --save oasgraph express express-graphql
```

### Start the GraphQL Server

Make sure your LoopBack application is running by going to
`http://localhost:3000/openapi.json`. If not, you can start it by running the
`npm start` command.

Now we will use the oasgraph CLI to set up a GraphQL HTTP Server backed by
express on port 3001. Specifying the OpenAPI spec generated by the
todo-application as the parameter, start up the server by running the following
command:

```sh
npx oasgraph http://localhost:3000/openapi.json
```

_Haven't heard about `npx` yet? It's a cool helper provided by `npm` and
available out of the box since Node.js 8.x. Learn more in their announcement
blog post:
[Introducing npx: an npm package runner](https://medium.com/@maybekatz/introducing-npx-an-npm-package-runner-55f7d4bd282b))_

That’s it! You’re now ready to try out some tests and requests in the browser at
http://localhost:3001/graphql.
bajtos marked this conversation as resolved.
Show resolved Hide resolved
bajtos marked this conversation as resolved.
Show resolved Hide resolved

{% include note.html content="
We are looking into ways how to expose the GraphQL endpoint alongside the main REST API,
on the same HTTP host and port. See
[issue #1905](https://github.com/strongloop/loopback-next/issues/1905).
" %}

### Try Out the GraphQL APIs

Here are some examples of the `query` and `mutation` calls:

1. To get all the to-do instances, run this query command:

```
query{
todos {
id
title
desc
}
}
```

The expected output looks like this:

```json
{ "data": { "todos": [
{ "id": 1, "title": "Take over the galaxy", "desc": "MWAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHA" },
{ "id": 2, "title": "destroy alderaan", "desc": "Make sure there are no survivors left!" },
{"id": 3, "title": "terrorize senate", "desc": "Tell them they're getting a budget
cut." },
{ "id": 4, "title": "crush rebel scum", "desc": "Every.Last.One." }
] } }
```

2. Create a to-do instance and retrieve its ID and title in the response object
using the following mutation command:

```
mutation {
postTodos(todoInput: {
title: "Take over the universe"
}) {
id
title
}
}
```

The expected output looks like this:

```json
{
"data": {
"postTodos": {
"id": 5,
"title": "Take over the universe"
}
}
}
```
15 changes: 12 additions & 3 deletions docs/site/sidebars/lb4_sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ children:
- title: 'Run and Test it'
url: soap-calculator-tutorial-run-and-test.html
output: 'web, pdf'
- title: 'Deploying to IBM Cloud'
url: Deploying-to-IBM-Cloud.html
output: 'web, pdf'

- title: 'Key concepts'
url: Concepts.html
Expand Down Expand Up @@ -194,6 +191,18 @@ children:
url: Error-handling.html
output: 'web, pdf'

- title: 'How tos'
url: Concepts.html
output: 'web, pdf'
children:
- title: 'Exposing GraphQL APIs'
url: exposing-graphql-apis.html
output: 'web, pdf'

- title: 'Deploying to IBM Cloud'
url: Deploying-to-IBM-Cloud.html
output: 'web, pdf'

- title: 'Booting an Application'
url: Booting-an-Application.html
output: 'web, pdf'
Expand Down