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

docs: add tutorial to expose GraphQL APIs #1899

merged 1 commit into from
Oct 26, 2018

Conversation

dhmlau
Copy link
Member

@dhmlau dhmlau commented Oct 22, 2018

Fixes #1775

Based on the QuickLab we've created for Node+JS Interactive, this PR added the GraphQL APIs part as one of the tutorials.

lb4 example todo
```

### Install OASGraph and required dependencies
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: all upper case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Will change it.
Actually our docs are not quite inconsistent on the capitalization of the titles. :(

Copy link
Contributor

@jannyHou jannyHou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@marioestradarosa
Copy link
Contributor

@dhmlau,

I think this line is missing from inside the src/application.ts file, or can it be resolved differently?

options = Object.assign({}, {
       rest: {
         openApiSpec: {
           servers: [{url:'http://localhost:3000'}],
         }
       }
    });

otherwise, the query in the doc will return

{
  "errors": [
    {
      "message": "Invalid URI \"/todos\"",
      "locations": [

@dhmlau dhmlau force-pushed the graphql branch 2 times, most recently from be7dcc2 to cb77b58 Compare October 23, 2018 00:22
@dhmlau
Copy link
Member Author

dhmlau commented Oct 23, 2018

Thanks @marioestradarosa for catching that. Somehow I thought it's no longer needed! PTAL again.

@marioestradarosa
Copy link
Contributor

Thanks @marioestradarosa for catching that. Somehow I thought it's no longer needed! PTAL again.

@dhmlau , I have just open a PR #1900 that I think can make things easier for the application in this matter. If it lands, then I believe we should then modify the examples accordingly.

thoughts?

Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks mostly good.

---
lang: en
title: 'Exposing GraphQL APIs'
keywords: LoopBack 4.0, LoopBack 4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add "GraphQL" keyword too?

and the required dependencies:

```sh
npm i --save oasgraph express-graphql
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am curious, why do we have to install express-graphql ourselves? I would expect the CLI command oasgraph to be self-contained and come with all dependencies.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The OASGraph is meant to be used as a library to transform from OpenAPI to GraphQL schema, It doesn't need express-graphql, since you can use any other package once you have your schema converted. The main idea of the library is to be used inside the application.

However the CLI is used to test the implementation and it requires express-graphql. It is not a separate package (like monorepo), it is just a single file that implements the GraphQL server with the generated schema. IMO, it is not meant for production.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @marioestradarosa. @dhmlau has pointed me to IBM/openapi-to-graphql#30, I have a better understanding now.

So, assuming oasgraph is not going to change, I am proposing the following changes:

Include express in the modules to install, we must not rely on express being installed in top-level node_modules by being a dependency of @loopback/rest.

$ npm i --save oasgraph express express-graphql

Explain the users why we are asking them to install those two extra express modules and point them to the discussion in IBM/openapi-to-graphql#30 to learn more and/or give us feedback.

docs/site/exposing-graphql-apis.md Show resolved Hide resolved
The expected output looks like this:

```json
{ "data": { "todos": [ { "id": 1, "title":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you reformat the data to make it easier to read please? E.g. use JSON.stringify(data, null, 2), or perhaps put each todos array item on its own line.

{ "data": { "todos": [
  {"id": 1, /*...*/},
  {"id": 2, /*...*/},
  // etc.
]}}

@@ -98,6 +98,11 @@ children:
- title: 'Run and Test it'
url: soap-calculator-tutorial-run-and-test.html
output: 'web, pdf'

- title: 'Exposing GraphQL APIs'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still a Tutorial? I feel that "Exposing GraphQL APIs" and "Deploying to IBM Cloud" don't belong to Tutorials section, I would certainly not look for these pages in Tutorials myself!

Can we introduce a new section "Guides" please and move both pages there?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about turning them into a How to sections ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to sounds good to me.

@marioestradarosa
Copy link
Contributor

@dhmlau, according to PR #1900 and @raymondfeng suggestions, here is the final code for the application.ts, this will populate the server property automatically.

options = Object.assign(
      {
        rest: {
          openApiSpec: {
            setServersFromRequest: true,
          },
        },
      },
      options,
    );

@bajtos
Copy link
Member

bajtos commented Oct 23, 2018

Hmm, we may want to use a deep-assign implementation instead of Object.assign- Object assign will overwrite the entire rest configuration with the object provided by the caller.

command:

```sh
node_modules/.bin/oasgraph http://localhost:3000/openapi.json
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified as npx oasgraph http://localhost:3000/openapi.json

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just need to mention the user that npx comes bundled with npm version >5.2 . it works fine, I have just tested, didn't know about npx 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node.js 8.9.0, the minimal Node.js version required by LoopBack 4, comes with npm version 5.5.1, which includes npx. IMO, there is no need to explain how to run oasgraph on npm version older than 5.2.

Having said that, I think most LoopBack users are probably not familiar with npx yet, so we should give them a pointer on where to learn more.

For example, add the following paragraph after the code snippet showing how to invoke npx:

_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))_

@marioestradarosa
Copy link
Contributor

marioestradarosa commented Oct 23, 2018

Object assign will overwrite the entire rest configuration with the object provided by the caller.

I thought that adding options as the end parameter prevents this overwrite. I did some tests.

If we place the options as the second parameter as it is here in this comment, then the config that comes in by the caller takes precedence.

On the other hand, if we place it as the first parameter, the rest property defined here in applications.ts takes precedence over the the one that is coming from the caller as follows:

options = Object.assign(
    options,
      {
        rest: {
          openApiSpec: {
            setServersFromRequest: true,
          },
        },
      },
    );

Which one is better for this case?.

@dhmlau
Copy link
Member Author

dhmlau commented Oct 24, 2018

Thanks for your feedback! To move this PR forward, I'm going to incorporate the feedback with the major changes:

Will create a new task for having the oasgraph and LB rest server running on the same port.
EDITED: Created #1905 for having the oasgraph and LB rest server running on the same port.

command:

```sh
node_modules/.bin/oasgraph http://localhost:3000/openapi.json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node.js 8.9.0, the minimal Node.js version required by LoopBack 4, comes with npm version 5.5.1, which includes npx. IMO, there is no need to explain how to run oasgraph on npm version older than 5.2.

Having said that, I think most LoopBack users are probably not familiar with npx yet, so we should give them a pointer on where to learn more.

For example, add the following paragraph after the code snippet showing how to invoke npx:

_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))_

docs/site/exposing-graphql-apis.md Show resolved Hide resolved
docs/site/exposing-graphql-apis.md Show resolved Hide resolved
docs/site/exposing-graphql-apis.md Show resolved Hide resolved
Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@marioestradarosa
Copy link
Contributor

@dhmlau , I think your PR is now ready to land 👍

@dhmlau dhmlau merged commit 9fd37c9 into master Oct 26, 2018
@dhmlau dhmlau deleted the graphql branch October 26, 2018 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants