-
Notifications
You must be signed in to change notification settings - Fork 30
Add GraphiQL view #457
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
Merged
Merged
Add GraphiQL view #457
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,128 @@ | ||
/** | ||
* Copyright (C) NIWA & British Crown (Met Office) & Contributors. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
// Code related to GraphiQL | ||
|
||
import { parse } from 'graphql' | ||
import { createGraphQLUrls } from '@/utils/graphql' | ||
|
||
// TODO: https://github.com/apollographql/GraphiQL-Subscriptions-Fetcher/issues/16 | ||
// the functions hasSubscriptionOperation and graphQLFetcher are both from | ||
// the graphiql-subscriptions-fetcher. Unfortunately that project is archived | ||
// on GitHub, and is using the old API for subscription-transport-ws, which | ||
// is a dependency of Cylc UI. As we cannot use an older version, instead we | ||
// have the two functions here, patched as per issue to work with newer API. | ||
|
||
/** | ||
* Tell whether it is a query or subscription. | ||
* | ||
* @private | ||
* @param {{ | ||
* query: string | ||
* }}graphQlParams | ||
* @returns {boolean} true if the params contain a subscription, false otherwise | ||
*/ | ||
const hasSubscriptionOperation = function (graphQlParams) { | ||
const queryDoc = parse(graphQlParams.query) | ||
for (let _i = 0, _a = queryDoc.definitions; _i < _a.length; _i++) { | ||
const definition = _a[_i] | ||
if (definition.kind === 'OperationDefinition') { | ||
const operation = definition.operation | ||
if (operation === 'subscription') { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
/** | ||
* The GraphQL fetcher function. | ||
* | ||
* @param {Object|null} subscriptionsClient | ||
* @param {function} fallbackFetcher | ||
* @param {{ | ||
* subscription: Object | ||
* }} component | ||
* @returns {function(...[*]=)} | ||
*/ | ||
const graphQLFetcher = function (subscriptionsClient, fallbackFetcher, component) { | ||
component.subscription = null | ||
return function (graphQLParams) { | ||
if (subscriptionsClient && component.subscription !== null) { | ||
subscriptionsClient.unsubscribe(component.subscription) | ||
} | ||
if (subscriptionsClient && hasSubscriptionOperation(graphQLParams)) { | ||
return { | ||
subscribe: function (observer) { | ||
observer.next('Your subscription data will appear here after server publication!') | ||
const subscription = subscriptionsClient.request({ | ||
query: graphQLParams.query, | ||
variables: graphQLParams.variables | ||
}, function (error, result) { | ||
if (error) { | ||
observer.error(error) | ||
} else { | ||
observer.next(result) | ||
} | ||
}) | ||
component.subscription = subscription.subscribe((result, err) => { | ||
if (err) { | ||
observer.error(err) | ||
} else { | ||
observer.next(result) | ||
} | ||
}) | ||
return component.subscription | ||
} | ||
} | ||
} else { | ||
return fallbackFetcher(graphQLParams) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Fallback GraphQL fetcher. | ||
* | ||
* @param {*} graphQLParams | ||
* @returns {Promise<any | string>} | ||
*/ | ||
function fallbackGraphQLFetcher (graphQLParams) { | ||
// re-using same method UI uses to create GraphQL URL's used by its client with createGraphQLUrls() | ||
return fetch( | ||
createGraphQLUrls().httpUrl, | ||
{ | ||
method: 'post', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify(graphQLParams), | ||
credentials: 'include' | ||
} | ||
).then(function (response) { | ||
return response.json().catch(function () { | ||
return response.text() | ||
}) | ||
}) | ||
} | ||
|
||
export { | ||
graphQLFetcher, | ||
fallbackGraphQLFetcher | ||
} |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -54,6 +54,7 @@ class SubscriptionWorkflowService extends GQuery { | |
*/ | ||
constructor (httpUrl, subscriptionClient) { | ||
super() | ||
this.subscriptionClient = subscriptionClient | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needed this so that we could pass the same instance to the GraphiQL fetcher. Tried to access the object from the instance of |
||
this.apolloClient = createApolloClient(httpUrl, subscriptionClient) | ||
this.observable = null | ||
} | ||
|
This file contains hidden or 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,84 @@ | ||
<!-- | ||
Copyright (C) NIWA & British Crown (Met Office) & Contributors. | ||
|
||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
|
||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
|
||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
--> | ||
|
||
<template> | ||
<div id="graphiql" ref="graphiql">Loading...</div> | ||
</template> | ||
|
||
<script> | ||
import { mixin } from '@/mixins' | ||
import ReactDOM from 'react-dom' | ||
import React from 'react' | ||
import GraphiQL from 'graphiql' | ||
import { graphQLFetcher, fallbackGraphQLFetcher } from '@/graphql/graphiql' | ||
|
||
export default { | ||
mixins: [mixin], | ||
metaInfo () { | ||
return { | ||
title: 'GraphiQL' | ||
} | ||
}, | ||
data () { | ||
return { | ||
fetcher: null, | ||
subscription: null | ||
} | ||
}, | ||
mounted () { | ||
this.fetcher = this.createFetcher() | ||
ReactDOM.render( | ||
React.createElement(GraphiQL, { | ||
fetcher: this.fetcher, | ||
defaultVariableEditorOpen: false | ||
}), | ||
this.$refs.graphiql | ||
) | ||
}, | ||
beforeRouteLeave (to, from, next) { | ||
// Important to remember to unsubscribe, otherwise a user may accidentally create several | ||
// subscriptions/observers, causing performance issues on both frontend and backend. | ||
if (this.subscription !== null) { | ||
this.subscription.unsubscribe() | ||
this.subscription = null | ||
} | ||
next() | ||
}, | ||
methods: { | ||
createFetcher () { | ||
const subscriptionClient = this.$workflowService.subscriptionClient | ||
return subscriptionClient !== null | ||
? graphQLFetcher(subscriptionClient, fallbackGraphQLFetcher, this) : fallbackGraphQLFetcher | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<style scoped> | ||
@import '~graphiql/graphiql.min.css'; | ||
|
||
body { | ||
height: 100%; | ||
margin: 0; | ||
overflow: hidden; | ||
width: 100%; | ||
} | ||
|
||
#graphiql { | ||
height: 100vh; | ||
} | ||
</style> |
Oops, something went wrong.
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.
Looks weird to have this in a Vue app, but we wouldn't be the first ones to try
And actually works. The generated app JS size will increase, but for the user it should still be loaded dynamically when the GraphiQL view is accessed.
The same already happened anyway, and via a CDN, instead of local files. So now GraphiQL should work even if accessed in an environment without external connectivity (related to #386).