Skip to content

Commit 90af813

Browse files
committed
fix(graphql-playground-react): Fix variable editor
Closes #372
1 parent 0eb2492 commit 90af813

File tree

9 files changed

+345
-39
lines changed

9 files changed

+345
-39
lines changed

packages/graphql-playground-react/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"license": "SEE LICENSE IN LICENSE",
1616
"scripts": {
1717
"start": "node scripts/start.js",
18-
"prepare": "yarn build",
18+
"prepublishOnly": "yarn build",
1919
"copy-styles": "cat src/styles/*.css > playground.css",
2020
"build": "rimraf dist build dist && yarn build-app && yarn build-package && yarn copy-styles",
2121
"tsc": "tsc -p tsconfig.build.json",
@@ -113,7 +113,7 @@
113113
"calculate-size": "^1.1.1",
114114
"classnames": "^2.2.5",
115115
"codemirror": "^5.27.4",
116-
"codemirror-graphql": "^0.6.12",
116+
"codemirror-graphql": "0.6.11",
117117
"cuid": "^1.3.8",
118118
"graphcool-styles": "0.2.4",
119119
"graphcool-tmp-ui": "^0.0.11",
@@ -125,6 +125,7 @@
125125
"keycode": "^2.1.9",
126126
"lodash": "^4.17.4",
127127
"lodash.debounce": "^4.0.8",
128+
"markdown-it": "^8.4.0",
128129
"marked": "^0.3.9",
129130
"polished": "^1.9.0",
130131
"postcss-modules": "^0.6.4",

packages/graphql-playground-react/src/components/Playground/ExecuteButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class ExecuteButton extends React.Component<
189189
this.onOptionSelected(
190190
this.props.operations.find(
191191
op => op.name.value === upEvent.target.textContent,
192-
),
192+
) || this.props.operations[0],
193193
)
194194
firstTime = false
195195
}

packages/graphql-playground-react/src/components/Playground/GraphQLEditor.tsx

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import { parse, print, GraphQLSchema } from 'graphql'
44
import * as cn from 'classnames'
55
import ExecuteButton from './ExecuteButton'
66
import { QueryEditor } from './QueryEditor'
7-
import { VariableEditor } from 'graphiql/dist/components/VariableEditor'
87
import CodeMirrorSizer from 'graphiql/dist/utility/CodeMirrorSizer'
9-
import getQueryFacts from 'graphiql/dist/utility/getQueryFacts'
108
import getSelectedOperationName from 'graphiql/dist/utility/getSelectedOperationName'
119
import debounce from 'graphiql/dist/utility/debounce'
1210
import find from 'graphiql/dist/utility/find'
@@ -30,6 +28,8 @@ import { getSessionDocs } from '../../selectors/sessionDocs'
3028
import { styled } from '../../styled/index'
3129
import TopBar from './TopBar/TopBar'
3230
import { SharingProps } from '../Share'
31+
import getQueryFacts from './util/getQueryFacts'
32+
import { VariableEditor } from './VariableEditor'
3333

3434
/**
3535
* The top-level React component for GraphQLEditor, intended to encompass the entire
@@ -507,6 +507,7 @@ export class GraphQLEditor extends React.PureComponent<
507507
hideGutters={this.props.hideGutters}
508508
readOnly={this.props.readonly}
509509
useVim={this.props.useVim}
510+
onClickReference={this.handleClickReference}
510511
/>
511512
<div className="variable-editor" style={variableStyle}>
512513
<div
@@ -618,7 +619,7 @@ export class GraphQLEditor extends React.PureComponent<
618619
}
619620
const data = JSON.stringify({
620621
query: this.state.query,
621-
variables: variables,
622+
variables,
622623
operationName: this.state.operationName,
623624
})
624625
let sessionHeaders
@@ -632,18 +633,18 @@ export class GraphQLEditor extends React.PureComponent<
632633
const headers = {
633634
'Accept-Encoding': 'gzip, deflate, br',
634635
'Content-Type': 'application/json',
635-
'Accept': '*/*',
636-
'Connection': 'keep-alive',
637-
'DNT': '1',
638-
'Origin': location.origin ||
639-
this.props.session
640-
.endpoint,
636+
Accept: '*/*',
637+
Connection: 'keep-alive',
638+
DNT: '1',
639+
Origin: location.origin || this.props.session.endpoint,
641640
...sessionHeaders,
642641
}
643-
const headersString = Object.keys(headers).map(key => {
644-
const value = headers[key]
645-
return `-H '${key}: ${value}'`
646-
}).join(' ')
642+
const headersString = Object.keys(headers)
643+
.map(key => {
644+
const value = headers[key]
645+
return `-H '${key}: ${value}'`
646+
})
647+
.join(' ')
647648
return `curl '${
648649
this.props.session.endpoint
649650
}' ${headersString} --data-binary '${data}' --compressed`
@@ -681,6 +682,10 @@ export class GraphQLEditor extends React.PureComponent<
681682
this.resultComponent = ref
682683
}
683684

685+
handleClickReference = reference => {
686+
//
687+
}
688+
684689
/**
685690
* Inspect the query, automatically filling in selection sets for non-leaf
686691
* fields which do not yet have them.

packages/graphql-playground-react/src/components/Playground/QueryEditor.tsx

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import * as React from 'react'
1010
import { GraphQLSchema } from 'graphql'
11+
import * as MD from 'markdown-it'
1112

1213
import onHasCompletion from './onHasCompletion'
1314
/**
@@ -28,6 +29,7 @@ export interface Props {
2829
onEdit?: (value: string) => void
2930
onHintInformationRender?: (elem: any) => void
3031
onRunQuery?: () => void
32+
onClickReference?: (reference: any) => void
3133
placeholder?: string
3234
readOnly?: boolean
3335
hideLineNumbers?: boolean
@@ -36,6 +38,8 @@ export interface Props {
3638
useVim?: boolean
3739
}
3840

41+
const md = new MD()
42+
3943
export class QueryEditor extends React.Component<Props, {}> {
4044
private cachedValue: string
4145
private editor: any
@@ -61,12 +65,16 @@ export class QueryEditor extends React.Component<Props, {}> {
6165
require('codemirror/addon/edit/closebrackets')
6266
require('codemirror/addon/fold/foldgutter')
6367
require('codemirror/addon/fold/brace-fold')
68+
require('codemirror/addon/search/search')
69+
require('codemirror/addon/search/searchcursor')
70+
require('codemirror/addon/search/jump-to-line')
71+
require('codemirror/addon/dialog/dialog')
6472
require('codemirror/addon/lint/lint')
65-
require('codemirror/addon/display/placeholder')
6673
require('codemirror/keymap/sublime')
67-
require('codemirror/keymap/vim')
6874
require('codemirror-graphql/hint')
6975
require('codemirror-graphql/lint')
76+
require('codemirror-graphql/info')
77+
require('codemirror-graphql/jump')
7078
require('codemirror-graphql/mode')
7179

7280
const gutters: any[] = []
@@ -102,9 +110,18 @@ export class QueryEditor extends React.Component<Props, {}> {
102110
},
103111
hintOptions: {
104112
schema: this.props.schema,
105-
closeOnUnfocus: true,
113+
closeOnUnfocus: false, // TODO reenable!!
106114
completeSingle: false,
107115
},
116+
info: {
117+
schema: this.props.schema,
118+
renderDescription: text => md.render(text),
119+
onClick: this.props.onClickReference,
120+
},
121+
jump: {
122+
schema: this.props.schema,
123+
onClick: this.props.onClickReference,
124+
},
108125
gutters,
109126
extraKeys: {
110127
'Cmd-Space': () => this.editor.showHint({ completeSingle: true }),
@@ -143,16 +160,24 @@ export class QueryEditor extends React.Component<Props, {}> {
143160
// Ensure the changes caused by this update are not interpretted as
144161
// user-input changes which could otherwise result in an infinite
145162
// event loop.
163+
146164
this.ignoreChangeEvent = true
147165
if (this.props.schema !== prevProps.schema) {
148166
this.editor.options.lint.schema = this.props.schema
149167
this.editor.options.hintOptions.schema = this.props.schema
168+
this.editor.options.info.schema = this.props.schema
169+
this.editor.options.jump.schema = this.props.schema
170+
CodeMirror.signal(this.editor, 'change', this.editor)
150171
if (this.props.schema) {
172+
const oldGetType = this.editor.options.hintOptions.schema.getType
151173
this.editor.options.hintOptions.schema.getType = type => {
152-
return type
174+
const result = oldGetType.call(
175+
this.editor.options.hintOptions.schema,
176+
type,
177+
)
178+
return result || type
153179
}
154180
}
155-
CodeMirror.signal(this.editor, 'change', this.editor)
156181
}
157182
if (
158183
this.props.value !== prevProps.value &&

packages/graphql-playground-react/src/components/Playground/SchemaExplorer.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@ export default function SchemaExplorer({ idl, modelName }: Props) {
2222
@p: .dn;
2323
}
2424
`}</style>
25-
<div className="header">
26-
Schema for „{modelName}
27-
</div>
25+
<div className="header">Schema for „{modelName}</div>
2826
<QueryEditor
2927
schema={null}
3028
value={idl || ''}
3129
readOnly={true}
3230
hideLineNumbers={true}
31+
onClickReference={handleClickReference}
3332
/>
3433
</div>
3534
)
3635
}
36+
37+
function handleClickReference() {
38+
//
39+
}

0 commit comments

Comments
 (0)