-
Notifications
You must be signed in to change notification settings - Fork 2k
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 for hapi #1058
Merged
Merged
Apollo server 2 for hapi #1058
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
08c1113
apollo-server-hapi: initial implementation of apollo-server base class
evans c55996d
apollo-server-hapi: fix graphql gui request, bind server listen, and …
evans a60b81e
apollo-server-hapi: update README to reflect Apollo-server 2 changes
evans 32a4fc8
apollo-server-hapi: remove subscriptions boolean from register option…
evans File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import * as hapi from 'hapi'; | ||
import { createServer, Server as HttpServer } from 'http'; | ||
import { ApolloServerBase, EngineLauncherOptions } from 'apollo-server-core'; | ||
import { parseAll } from 'accept'; | ||
import { renderPlaygroundPage } from 'graphql-playground-html'; | ||
|
||
import { graphqlHapi } from './hapiApollo'; | ||
|
||
export interface ServerRegistration { | ||
app: hapi.Server; | ||
server: ApolloServerBase<hapi.Request>; | ||
path?: string; | ||
} | ||
|
||
export interface HapiListenOptions { | ||
port?: number | string; | ||
host?: string; // default: ''. This is where engineproxy listens. | ||
pipePath?: string; | ||
graphqlPaths?: string[]; // default: ['/graphql'] | ||
innerHost?: string; // default: '127.0.0.1'. This is where Node listens. | ||
launcherOptions?: EngineLauncherOptions; | ||
} | ||
|
||
export const registerServer = async ({ | ||
app, | ||
server, | ||
path, | ||
}: ServerRegistration) => { | ||
if (!path) path = '/graphql'; | ||
|
||
await app.ext({ | ||
type: 'onRequest', | ||
method: function(request, h) { | ||
if (request.path !== path) { | ||
return h.continue; | ||
} | ||
|
||
if (!server.disableTools && request.method === 'get') { | ||
//perform more expensive content-type check only if necessary | ||
const accept = parseAll(request.headers); | ||
const types = accept.mediaTypes as string[]; | ||
const prefersHTML = | ||
types.find( | ||
(x: string) => x === 'text/html' || x === 'application/json', | ||
) === 'text/html'; | ||
|
||
if (prefersHTML) { | ||
return h | ||
.response( | ||
renderPlaygroundPage({ | ||
subscriptionsEndpoint: server.subscriptionsEnabled && path, | ||
endpoint: path, | ||
version: '1.4.0', | ||
}), | ||
) | ||
.type('text/html') | ||
.takeover(); | ||
} | ||
} | ||
return h.continue; | ||
}, | ||
}); | ||
|
||
await app.register({ | ||
plugin: graphqlHapi, | ||
options: { | ||
path: path, | ||
graphqlOptions: server.request.bind(server), | ||
route: { | ||
cors: true, | ||
}, | ||
}, | ||
}); | ||
|
||
server.use({ path, getHttp: () => app.listener }); | ||
|
||
const listen = server.listen.bind(server); | ||
server.listen = async options => { | ||
//requires that autoListen is false, so that | ||
//hapi sets up app.listener without start | ||
await app.start(); | ||
|
||
//While this is not strictly necessary, it ensures that apollo server calls | ||
//listen first, setting the port. Otherwise the hapi server constructor | ||
//sets the port | ||
if (app.listener.listening) { | ||
throw Error( | ||
` | ||
Ensure that constructor of Hapi server sets autoListen to false, as follows: | ||
|
||
const app = Hapi.server({ | ||
autoListen: false, | ||
//other parameters | ||
}); | ||
`, | ||
); | ||
} | ||
|
||
//starts the hapi listener at a random port when engine proxy used, | ||
//otherwise will start the server at the provided port | ||
return listen({ ...options }); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Odd — isn't that the express-specific class?