Skip to content

Commit 569268b

Browse files
committed
electron: add dotenv support, fix session creation
1 parent 1b44407 commit 569268b

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@
1616
"hapi": "16.6.2",
1717
"koa": "2.4.1",
1818
"rimraf": "2.6.2"
19+
},
20+
"dependencies": {
21+
"dotenv": "^5.0.1"
1922
}
2023
}

packages/graphql-playground-electron/src/main/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ ipcMain.on('cwd', (event, msg) => {
9393
windowContext.windowByPath.set(cwd, window)
9494
})
9595

96+
ipcMain.on('CloseWindow', () => app.quit())
97+
9698
// This method will be called when Electron has finished
9799
// initialization and is ready to create browser windows.
98100
// Some APIs can only be used after this event occurs.

packages/graphql-playground-electron/src/renderer/components/App.tsx

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Playground, {
1313
getSessionsState,
1414
saveFile,
1515
newFileTab,
16+
getEndpoint,
1617
} from 'graphql-playground-react'
1718
import {
1819
getGraphQLConfig,
@@ -32,6 +33,8 @@ import { patchEndpointsToConfigData as patchPrismaEndpointsToConfigData } from '
3233
import { patchEndpointsToConfigData } from 'graphql-config-extension-graphcool'
3334
import { connect } from 'react-redux'
3435
import { errify } from '../utils/errify'
36+
import { createStructuredSelector } from 'reselect'
37+
import * as dotenv from 'dotenv'
3538

3639
// import { PermissionSession } from 'graphql-playground/lib/types'
3740

@@ -84,9 +87,10 @@ interface ReduxProps {
8487
selectPrevTab: () => void
8588
closeSelectedTab: () => void
8689
fetchSchema: () => void
87-
newSession: () => void
90+
newSession: (endpoint: string) => void
8891
saveFile: () => void
8992
newFileTab: (fileName: string, filePath: string, file: string) => void
93+
endpoint: string
9094
}
9195

9296
class App extends React.Component<ReduxProps, State> {
@@ -129,17 +133,18 @@ class App extends React.Component<ReduxProps, State> {
129133
handleSelectFolder = async (folderPath: string) => {
130134
try {
131135
// Get config from folderPath
136+
dotenv.config({ path: folderPath })
132137
const configPath = findGraphQLConfigFile(folderPath)
133138
const configString = fs.readFileSync(configPath, 'utf-8')
134139

135140
/* tslint:disable-next-line */
136-
if (configString.includes('${env:')) {
137-
errify(`You opened a .graphqlconfig file that includes environment variables.
138-
In order to use environment variables in the Playground, please start it from the graphql cli. Install with
139-
npm install -g graphql-cli
140-
Then open the graphql config with:
141-
cd ${folderPath}; graphql playground`)
142-
}
141+
// if (configString.includes('${env:')) {
142+
// errify(`You opened a .graphqlconfig file that includes environment variables.
143+
// In order to use environment variables in the Playground, please start it from the graphql cli. Install with
144+
// npm install -g graphql-cli
145+
// Then open the graphql config with:
146+
// cd ${folderPath}; graphql playground`)
147+
// }
143148

144149
const configDir = path.dirname(configPath)
145150
let config = await patchEndpointsToConfigData(
@@ -193,7 +198,7 @@ cd ${folderPath}; graphql playground`)
193198
}
194199

195200
newTab = () => {
196-
this.props.newSession()
201+
this.props.newSession(this.props.endpoint)
197202
}
198203

199204
closeTab = () => {
@@ -214,7 +219,12 @@ cd ${folderPath}; graphql playground`)
214219
window.addEventListener('keydown', this.handleKeyDown, true)
215220
this.consumeEvents()
216221
ipcRenderer.send('ready', '')
217-
if (!this.state.endpoint && !this.state.config && !this.state.configPath && !this.state.configString) {
222+
if (
223+
!this.state.endpoint &&
224+
!this.state.config &&
225+
!this.state.configPath &&
226+
!this.state.configString
227+
) {
218228
const workspace = this.deserializeWorkspace()
219229
if (workspace) {
220230
this.setState(workspace)
@@ -535,7 +545,11 @@ cd ${folderPath}; graphql playground`)
535545
this.newTab()
536546
break
537547
case 'Close':
538-
this.closeTab()
548+
if (!this.state.endpoint && !this.state.config) {
549+
ipcRenderer.send('CloseWindow')
550+
} else {
551+
this.closeTab()
552+
}
539553
break
540554
case 'Settings':
541555
this.openSettingsTab()
@@ -610,7 +624,11 @@ cd ${folderPath}; graphql playground`)
610624
}
611625
}
612626

613-
export default connect(null, {
627+
const mapStateToProps = createStructuredSelector({
628+
endpoint: getEndpoint,
629+
})
630+
631+
export default connect(mapStateToProps, {
614632
openSettingsTab,
615633
selectNextTab,
616634
selectPrevTab,

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ dotenv@^0.4.0:
214214
version "0.4.0"
215215
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-0.4.0.tgz#f6fb351363c2d92207245c737802c9ab5ae1495a"
216216

217+
dotenv@^5.0.1:
218+
version "5.0.1"
219+
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-5.0.1.tgz#a5317459bd3d79ab88cff6e44057a6a3fbb1fcef"
220+
217221
218222
version "1.1.1"
219223
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"

0 commit comments

Comments
 (0)