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

Integrations export their own ApolloServer #1161

Merged
merged 7 commits into from
Jun 13, 2018

Conversation

evans
Copy link
Contributor

@evans evans commented Jun 12, 2018

This PR creates ApolloServer classes for the hapi, express, and cloudflare integrations. The constructor will continue to stay the same across all environments(ideally there would be a way to programmatically enforce this). The reasons to do this are the ApolloServer class in apollo-server brings the extra dependency on express, which does not work for serverless environments. The presumably more common path is going to be to use apollo-server-{variant}, so that should be equally as understandable/easy as the getting started experience. The arguments the context creation function will be set by the variant and be type safe, see #1110. The new typings should also solve #1140

The downside is that we won't be using the apollo-server package everywhere. I'm unconvinced this will be possible.

This PR stays backwards compatible with the dual import of apollo-server and apollo-server-express(below), however the documentation should be updated to reflect these changes.

The old way:

const express = require('express');
const { registerServer } = require('apollo-server-express');
const { ApolloServer, gql } = require('apollo-server');

const server = new ApolloServer({ typeDefs, resolvers });

const app = express();
registerServer({ server, app });

server.listen().then(({ url }) => {
  console.log(`🚀 Server ready at ${url}`);
});

The new way of importing would be as follows:

apollo-server

const { ApolloServer, gql } = require('apollo-server');
// There could be a possibility to import registerExpressServer, however I don't think we need it

const server = new ApolloServer({ typeDefs, resolvers });

server.listen().then(({ url }) => {
  console.log(`🚀 Server ready at ${url}`);
});

apollo-server-express

const express = require('express');
const { ApolloServer, gql, registerServer } = require('apollo-server-express');

const server = new ApolloServer({ typeDefs, resolvers });

const app = express();
registerServer({ server, app });

server.listen().then(({ url }) => {
  console.log(`🚀 Server ready at ${url}`);
});

apollo-server-hapi

const { registerServer, ApolloServer, gql } = require('apollo-server-hapi');

async function StartServer() {
  const server = new ApolloServer({ typeDefs, resolvers });

  await registerServer({
    server,
    //Hapi Server constructor options
    options: {
      host: 'localhost',
    },
  });

  server.listen().then(({ url }) => {
    console.log(`🚀  Server ready at ${url}`);
  });
}

StartServer().catch(error => console.log(error));

@evans
Copy link
Contributor Author

evans commented Jun 12, 2018

Note that options to registerServer for different integrations include things like:

  • cors
  • body parser
  • health-check
  • uploads
  • path

ApolloServer accepts anything that relates to the schema + engine reporting(the same across all envs), things like:

  • typeDefs
  • resolvers
  • context(note that the arguments are integration specific/provided)
  • directive
  • mocks
  • persistedQueries
  • extensions

@unicodeveloper
Copy link
Contributor

@evans I like this approach. Fewer imports for apollo-server variants. What's the ETA for getting this merged and tagged?

@evans evans added this to the Release 2.0 milestone Jun 12, 2018
@@ -115,7 +115,7 @@ new ApolloServer({

## registerServer

The `registerServer` method is from `apollo-server-express`. Middleware registration has been greatly simplified with this new method.
The `registerServer` method providec by all integrations. Middleware registration has been greatly simplified with this new method.
Copy link
Member

Choose a reason for hiding this comment

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

"provided". also it's a function, not a method.

Also I'm not sure that documenting something as simplifying something with a new method will age well? Maybe just be more explicit about what it does, like "This function is what lets you connect your ApolloServer to your web framework. Use it instead of the listen method when you want to customize your app's web behavior."
or something


Pass the cors options.
Pass the integration specific cors options.
Copy link
Member

Choose a reason for hiding this comment

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

"integration-specific" with hyphen. Maybe document that this is equivalent to the arguments to the cors or hapi-cors packages, with links?


* `bodyParser`: <`Object`> (express)

Pass the body parser options.
Copy link
Member

Choose a reason for hiding this comment

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

Link to body-parser?


export class ApolloServer extends ApolloServerBase {
async createGraphQLServerOptions(req: Request): Promise<GraphQLOptions> {
Copy link
Member

Choose a reason for hiding this comment

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

Why does this function take a request argument rather than an options argument like the one it calls? Also what is the point of this function and why does it have a different name than graphQLServerOptions?


export class ApolloServer extends ApolloServerBase {
async createGraphQLServerOptions(req: Request): Promise<GraphQLOptions> {
return super.graphQLServerOptions({ req });
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this be request rather than req?

const gql = String.raw;

export class ApolloServer extends ApolloServerBase {
async createGraphQLServerOptions(
Copy link
Member

Choose a reason for hiding this comment

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

yeah i don't get this. is it a typing thing?

@@ -22,4 +22,4 @@ export {
export { graphqlConnect, graphiqlConnect } from './connectApollo';
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't a-s-e need to export gql now too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@evans evans merged commit ba31cf7 into version-2 Jun 13, 2018
@evans evans deleted the server-2.0/intergations-ApolloServer branch June 13, 2018 00:47
@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.

3 participants