Skip to content

Commit f3d7eed

Browse files
committed
Improved error handling
1 parent 60fba63 commit f3d7eed

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

docs/playground/page.js

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,29 @@ class Playground extends React.Component {
7676
}
7777

7878
this.pypyjs = this.pypy_interpreter.ready().then(() => {
79-
return this.pypy_interpreter.exec("import graphene");
79+
return this.pypy_interpreter.exec(`
80+
import graphene
81+
import js
82+
from collections import OrderedDict
83+
from graphql.core.execution.executor import Executor
84+
from graphql.core.execution.middlewares.sync import SynchronousExecutionMiddleware
85+
from graphql.core.error import GraphQLError, format_error
86+
87+
def get_wrapped(f):
88+
if hasattr(f, 'func_closure') and f.func_closure:
89+
return get_wrapped(f.func_closure[0].cell_contents)
90+
return f
91+
92+
class TrackResolver(SynchronousExecutionMiddleware):
93+
@staticmethod
94+
def run_resolve_fn(resolver, original_resolver):
95+
if resolver.func.__module__ == '__main__':
96+
line = get_wrapped(resolver.func).resolver.func_code.co_firstlineno
97+
js.globals.markLine(line-3)
98+
return SynchronousExecutionMiddleware.run_resolve_fn(resolver, original_resolver)
99+
100+
__graphene_executor = Executor([TrackResolver()], map_type=OrderedDict)
101+
`);
80102
}).then(() => {
81103
this.createSchema(baseCode);
82104
}).then(() => {
@@ -110,21 +132,21 @@ class Playground extends React.Component {
110132
this.createSchema(this.editor.getValue());
111133
}
112134
createSchema(code) {
135+
console.log('createSchema');
113136
this.validSchema = null;
114137
this.pypyjs.then(() => {
115138
return this.pypy_interpreter.exec(`
116139
schema = None
117140
${code}
118141
assert schema, 'You have to define a schema'
119142
`)
143+
}).then(() => {
144+
console.log('NO ERRORS');
145+
this.validSchema = true;
120146
}, (err) => {
147+
console.log('ERROR', err);
121148
this.validSchema = false;
122149
// this.editor.setGutterMarker(5, "breakpoints", syntaxError());
123-
console.log('ERROR', err);
124-
}).then(() => {
125-
if (this.validSchema === null) {
126-
this.validSchema = true;
127-
}
128150
}).then(this.updateGraphiQL.bind(this));
129151
}
130152
updateGraphiQL() {
@@ -145,29 +167,8 @@ assert schema, 'You have to define a schema'
145167
// console.log('execute', query);
146168
return this.pypyjs.then(() => {
147169
var x = `
148-
import js
149170
import json
150-
from collections import OrderedDict
151-
from graphql.core.execution.executor import Executor
152-
from graphql.core.execution.middlewares.sync import SynchronousExecutionMiddleware
153-
from graphql.core.error import GraphQLError, format_error
154-
155-
def get_wrapped(f):
156-
if hasattr(f, 'func_closure') and f.func_closure:
157-
return get_wrapped(f.func_closure[0].cell_contents)
158-
return f
159-
160-
class TrackResolver(SynchronousExecutionMiddleware):
161-
@staticmethod
162-
def run_resolve_fn(resolver, original_resolver):
163-
if resolver.func.__module__ == '__main__':
164-
line = get_wrapped(resolver.func).resolver.func_code.co_firstlineno
165-
js.globals.markLine(line-3)
166-
return SynchronousExecutionMiddleware.run_resolve_fn(resolver, original_resolver)
167-
168-
executor = Executor([TrackResolver()], map_type=OrderedDict)
169-
170-
result = executor.execute(schema.schema, '''${query}''')
171+
result = __graphene_executor.execute(schema.schema, '''${query}''')
171172
result_dict = {};
172173
if result.errors:
173174
result_dict['errors'] = [format_error(e) for e in result.errors]

0 commit comments

Comments
 (0)