-
Notifications
You must be signed in to change notification settings - Fork 8.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
lazy loading for infra assets #62363
Merged
mshustov
merged 2 commits into
elastic:master
from
mshustov:issue-62238-infra-async-assets
Apr 3, 2020
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { InMemoryCache, IntrospectionFragmentMatcher } from 'apollo-cache-inmemory'; | ||
import ApolloClient from 'apollo-client'; | ||
import { ApolloLink } from 'apollo-link'; | ||
import { createHttpLink } from 'apollo-link-http'; | ||
import { withClientState } from 'apollo-link-state'; | ||
import { CoreStart, HttpFetchOptions } from 'src/core/public'; | ||
import { InfraFrontendLibs } from './lib/lib'; | ||
import introspectionQueryResultData from './graphql/introspection.json'; | ||
import { InfraKibanaObservableApiAdapter } from './lib/adapters/observable_api/kibana_observable_api'; | ||
|
||
export function composeLibs(core: CoreStart) { | ||
const cache = new InMemoryCache({ | ||
addTypename: false, | ||
fragmentMatcher: new IntrospectionFragmentMatcher({ | ||
introspectionQueryResultData, | ||
}), | ||
}); | ||
|
||
const observableApi = new InfraKibanaObservableApiAdapter({ | ||
basePath: core.http.basePath.get(), | ||
}); | ||
|
||
const wrappedFetch = (path: string, options: HttpFetchOptions) => { | ||
return new Promise<Response>(async (resolve, reject) => { | ||
// core.http.fetch isn't 100% compatible with the Fetch API and will | ||
// throw Errors on 401s. This top level try / catch handles those scenarios. | ||
try { | ||
core.http | ||
.fetch(path, { | ||
...options, | ||
// Set headers to undefined due to this bug: https://github.com/apollographql/apollo-link/issues/249, | ||
// Apollo will try to set a "content-type" header which will conflict with the "Content-Type" header that | ||
// core.http.fetch correctly sets. | ||
headers: undefined, | ||
asResponse: true, | ||
}) | ||
.then(res => { | ||
if (!res.response) { | ||
return reject(); | ||
} | ||
// core.http.fetch will parse the Response and set a body before handing it back. As such .text() / .json() | ||
// will have already been called on the Response instance. However, Apollo will also want to call | ||
// .text() / .json() on the instance, as it expects the raw Response instance, rather than core's wrapper. | ||
// .text() / .json() can only be called once, and an Error will be thrown if those methods are accessed again. | ||
// This hacks around that by setting up a new .text() method that will restringify the JSON response we already have. | ||
// This does result in an extra stringify / parse cycle, which isn't ideal, but as we only have a few endpoints left using | ||
// GraphQL this shouldn't create excessive overhead. | ||
// Ref: https://github.com/apollographql/apollo-link/blob/master/packages/apollo-link-http/src/httpLink.ts#L134 | ||
// and | ||
// https://github.com/apollographql/apollo-link/blob/master/packages/apollo-link-http-common/src/index.ts#L125 | ||
return resolve({ | ||
...res.response, | ||
text: () => { | ||
return new Promise(async (resolveText, rejectText) => { | ||
if (res.body) { | ||
return resolveText(JSON.stringify(res.body)); | ||
} else { | ||
return rejectText(); | ||
} | ||
}); | ||
}, | ||
}); | ||
}); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
}; | ||
|
||
const HttpLink = createHttpLink({ | ||
fetch: wrappedFetch, | ||
uri: `/api/infra/graphql`, | ||
}); | ||
|
||
const graphQLOptions = { | ||
cache, | ||
link: ApolloLink.from([ | ||
withClientState({ | ||
cache, | ||
resolvers: {}, | ||
}), | ||
HttpLink, | ||
]), | ||
}; | ||
|
||
const apolloClient = new ApolloClient(graphQLOptions); | ||
|
||
const libs: InfraFrontendLibs = { | ||
apolloClient, | ||
observableApi, | ||
}; | ||
return libs; | ||
} |
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.
is there a reason we don't just stick this whole function behind an async import?
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.
I just think it would be a lot easier to implement correctly and maintain if everything was behind the await, not individual things being places behind the await
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.
Yeah, I will adopt this pattern in other plugins with less logic in the mount function. I'm not comfortable touching the plugin code here due to multiple dependencies between rendering functions.
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.
We agree and will clean up the mess for this plugin in a follow-up (#62413).