Passed
Push — master ( 80b04f...98ef0e )
by GRASSIOT
10:23
created

init-graphiql.js ➔ onEditOperationName   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
1
// Parse the search string to get url parameters.
2
var search = window.location.search;
3
var parameters = {};
4
search.substr(1).split('&').forEach(function (entry) {
5
    var eq = entry.indexOf('=');
6
    if (eq >= 0) {
7
        parameters[decodeURIComponent(entry.slice(0, eq))] =
8
            decodeURIComponent(entry.slice(eq + 1));
9
    }
10
});
11
12
// If variables was provided, try to format it.
13
if (parameters.variables) {
14
    try {
15
        parameters.variables =
16
            JSON.stringify(JSON.parse(parameters.variables), null, 2);
17
    } catch (e) {
0 ignored issues
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
18
        // Do nothing, we want to display the invalid JSON as a string, rather
19
        // than present an error.
20
    }
21
}
22
23
// When the query and variables string is edited, update the URL bar so
24
// that it can be easily shared
25
function onEditQuery(newQuery) {
26
    parameters.query = newQuery;
27
    updateURL();
28
}
29
30
function onEditVariables(newVariables) {
31
    parameters.variables = newVariables;
32
    updateURL();
33
}
34
35
function onEditOperationName(newOperationName) {
36
    parameters.operationName = newOperationName;
37
    updateURL();
38
}
39
40
function updateURL() {
41
    var newSearch = '?' + Object.keys(parameters).filter(function (key) {
42
        return Boolean(parameters[key]);
43
    }).map(function (key) {
44
        return encodeURIComponent(key) + '=' +
45
            encodeURIComponent(parameters[key]);
46
    }).join('&');
47
    history.replaceState(null, null, newSearch);
48
}
49
50
// Defines a GraphQL fetcher using the fetch API.
51
function graphQLFetcher(graphQLParams) {
52
    return fetch(window.location.pathname, {
53
        method: 'post',
54
        headers: {
55
            'Accept': 'application/json',
56
            'Content-Type': 'application/json',
57
        },
58
        body: JSON.stringify(graphQLParams),
59
        credentials: 'include',
60
    }).then(function (response) {
61
        return response.text();
62
    }).then(function (responseBody) {
63
        try {
64
            return JSON.parse(responseBody);
65
        } catch (error) {
66
            return responseBody;
67
        }
68
    });
69
}
70
71
// Render <GraphiQL /> into the body.
72
ReactDOM.render(
0 ignored issues
show
Bug introduced by
The variable ReactDOM seems to be never declared. If this is a global, consider adding a /** global: ReactDOM */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
73
    React.createElement(GraphiQL, {
0 ignored issues
show
Bug introduced by
The variable GraphiQL seems to be never declared. If this is a global, consider adding a /** global: GraphiQL */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable React seems to be never declared. If this is a global, consider adding a /** global: React */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
74
        fetcher: graphQLFetcher,
75
        query: parameters.query,
76
        variables: parameters.variables,
77
        operationName: parameters.operationName,
78
        onEditQuery: onEditQuery,
79
        onEditVariables: onEditVariables,
80
        onEditOperationName: onEditOperationName
81
    }),
82
    document.getElementById('graphiql')
83
);
84