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

Apollo Server 2 Micro integration #1279

Merged
merged 12 commits into from
Jul 5, 2018
Merged

Conversation

hwillson
Copy link
Member

@hwillson hwillson commented Jul 1, 2018

Hi all - here's my pass at the Apollo Server 2 Micro integration (sorry for the delay @evans! 😳). This integration should be pretty much feature complete, based on the functionality offered by apollo-server-express. A few comments:

  • Since Micro is intended to be used to create focused microservices, the ApolloServer.applyMiddleware approach doesn't quite fit. Micro doesn't have middleware capabilities, so passing a Micro instance into ApolloServer, to make sure additional functionality is included during the request/response chain, won't work (well, we could make it work, but it would require a bit of questionable hacking). For this reason, instead of passing a Micro instance into ApolloServer, I'm building an async function in ApolloServer (that includes all of the needed Apollo Server functionality) that can then be used in the calling application as the default exported Micro module. The README shows examples of how this works, but essentially the Micro handler function is generated using ApolloServer.graphqlHandler(). This is similar in concept to how the apollo-server-lambda integration works, but it's using ApolloServer.createHandler() instead. I prefer the more specific graphqlHandler naming, but I'm happy to change this is you'd prefer to see these lined up.
  • I noticed that some of the other integrations include additional dependencies and code to handle things like CORS. Instead of including additional deps in this module, I've added examples in the README that show how to do things like add in CORS support, use fully custom routing, etc. Micro makes this really easy to do since you're just wrapping functions to add in functionality. If you think I should move these deps into the apollo-server-micro package though, definitely let me know.
  • I've marked this PR as a work in progress, as I haven't committed my tests yet. I'm still tweaking them a bit, and will add them in tomorrow.

Associated with #1088.

Thanks!

@hwillson hwillson changed the title [WIP] Apollo Server 2 Micro integration Apollo Server 2 Micro integration Jul 2, 2018
@hwillson
Copy link
Member Author

hwillson commented Jul 2, 2018

Tests have been added, so I've removed the WIP. This should now be ready for review. Thanks!

Copy link
Contributor

@evans evans left a comment

Choose a reason for hiding this comment

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

@hwillson This is so incredible! I walked through the setup locally and it's great. I left some comments about the structuring of the content in the README and about graphqlHander vs createHandler. Also I'm going to defer to you on the cors, since I'm not sure where micro is most used. If it's ever used where another site might request from it, then we should probably include cors.

Thank you so much for putting this together!


4) After an `npm start`, access `http://localhost:3000` to run queries using
[`graphql-playground`](https://github.com/prismagraphql/graphql-playground),
or send GraphQL requests directly to `http://localhost:3000/graphql`.
Copy link
Contributor

Choose a reason for hiding this comment

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

For 2.0, we've been collocating these two endpoints and sending GraphQL Playground based on the accepts type sent with the request. If it's possible to do that with micro, that would be incredible.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just read the code and it looks like we do the colocation! I'm comfortable with this or to make the first link /graphql as well

Copy link
Member Author

Choose a reason for hiding this comment

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

Great - I'll make them the same. Thanks!


// Prepares and returns an async function that can be used by Micro to handle
// GraphQL requests.
public graphqlHandler({
Copy link
Contributor

Choose a reason for hiding this comment

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

I think naming this createHandler would be better, since it lines up with lambda. Since Apollo Server is GraphQL specific, I'm not sure the graphql portion is necessary. Happy to differ to your judgement if the micro community expects something different

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good - I'll adjust, thanks!

}

// Backwards compatibility with AS 1.x
export const microGraphql = graphqlMicro;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need this, since we're on 2.0 and the other integrations don't export backwards compatible middleware/handlers

```js
const cors = require('micro-cors')();
...
module.exports = cors(apolloServer.graphqlHandler());
Copy link
Contributor

Choose a reason for hiding this comment

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

Since micro is for microservices, I think best practice is to leave cors out by default, since microservices are supposed to be unaccessible by browsers. If you're experience is that they are called from another website in any regular usage pattern, then we should include cors by default.

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's leave CORS out for now. It's easy enough for end users to add in and we've documented a small example, so hopefully that's enough for now. We can always adjust this based on user feedback. Thanks!

1) Package installation.

```sh
npm install --save micro apollo-server-micro
Copy link
Contributor

Choose a reason for hiding this comment

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

For now, we should stick with apollo-server-micro@rc. We'll replace it when the release comes out!


## Basic GraphQL Microservice

This example demonstrates how to setup a simple microservice using Micro, that
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there needs to be a comma before using to make this grammatically correct


```js
...
module.exports = cors(apolloServer.graphqlHandler({ path: '/data' }));
Copy link
Contributor

Choose a reason for hiding this comment

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

We should leave out the cors option here, since using the previous example. It might also be nice to link to the #basic-graphql-microservice example above when we mention it, in case forgetful readers(like me) didn't check the title 😊

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I see, this tutorial is cumulative. I'd slightly lean towards standalone sections, since users can pull from the grab bag without something else sharp poking them. Though I'm happy to see it as a cumulative deal if you prefer. If it is building, I'm worried that people will get confused by the headers at the same level.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point - I'll make each section standalone. Thanks!

@hwillson
Copy link
Member Author

hwillson commented Jul 5, 2018

Thanks for the review @evans! I believe I've covered everything (let me know if there's something I missed).

@evans evans merged commit c356bcf into version-2 Jul 5, 2018
@evans
Copy link
Contributor

evans commented Jul 5, 2018

@hwillson Thank you so much!

@hwillson hwillson deleted the hwillson/micro-integration branch July 5, 2018 18:54
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants