graphiql-rails 1.5.0 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/assets/javascripts/graphiql/rails/application.js +1 -0
- data/app/assets/javascripts/graphiql/rails/graphiql_show.js +91 -0
- data/app/views/graphiql/rails/editors/show.html.erb +7 -92
- data/lib/graphiql/rails/version.rb +1 -1
- data/test/controllers/editors_controller_test.rb +19 -20
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6d429f568b6d396fb51add5b421cccec57c1e3e7
|
4
|
+
data.tar.gz: 87a72232fc6511e41a2dafa5d28dec603bb2689f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dcd7f622dd31a9af902527d30b3de1e3067523c256eca330ed59950ca81c1a6d33d441dd0520905c831fad5a1cd8402dc31d8e9e7b2b612871675b81dd3f703c
|
7
|
+
data.tar.gz: bae555bf6ec6e0f086fb41048d4169ce529380d544d7c202c2badbe80f7cae40ea97c10b2f4176d0395f95bfee255c1f4035079798f87a52510bd7c7c284ebf3
|
@@ -0,0 +1,91 @@
|
|
1
|
+
document.addEventListener("DOMContentLoaded", function(event) {
|
2
|
+
var graphiqlContainer = document.getElementById("graphiql-container");
|
3
|
+
var parameters = {};
|
4
|
+
|
5
|
+
var queryParams = graphiqlContainer.dataset.queryParams;
|
6
|
+
|
7
|
+
if (queryParams === 'true') {
|
8
|
+
// Parse the search string to get url parameters.
|
9
|
+
var search = window.location.search;
|
10
|
+
search.substr(1).split('&').forEach(function (entry) {
|
11
|
+
var eq = entry.indexOf('=');
|
12
|
+
if (eq >= 0) {
|
13
|
+
parameters[decodeURIComponent(entry.slice(0, eq))] =
|
14
|
+
decodeURIComponent(entry.slice(eq + 1));
|
15
|
+
}
|
16
|
+
});
|
17
|
+
// if variables was provided, try to format it.
|
18
|
+
if (parameters.variables) {
|
19
|
+
try {
|
20
|
+
parameters.variables =
|
21
|
+
JSON.stringify(JSON.parse(parameters.variables), null, 2);
|
22
|
+
} catch (e) {
|
23
|
+
// Do nothing, we want to display the invalid JSON as a string, rather
|
24
|
+
// than present an error.
|
25
|
+
}
|
26
|
+
}
|
27
|
+
// When the query and variables string is edited, update the URL bar so
|
28
|
+
// that it can be easily shared
|
29
|
+
function onEditQuery(newQuery) {
|
30
|
+
parameters.query = newQuery;
|
31
|
+
updateURL();
|
32
|
+
}
|
33
|
+
function onEditVariables(newVariables) {
|
34
|
+
parameters.variables = newVariables;
|
35
|
+
updateURL();
|
36
|
+
}
|
37
|
+
function updateURL() {
|
38
|
+
var newSearch = '?' + Object.keys(parameters).map(function (key) {
|
39
|
+
return encodeURIComponent(key) + '=' +
|
40
|
+
encodeURIComponent(parameters[key]);
|
41
|
+
}).join('&');
|
42
|
+
history.replaceState(null, null, newSearch);
|
43
|
+
}
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
|
48
|
+
// Defines a GraphQL fetcher using the fetch API.
|
49
|
+
var graphQLEndpoint = graphiqlContainer.dataset.graphqlEndpointPath;
|
50
|
+
function graphQLFetcher(graphQLParams) {
|
51
|
+
return fetch(graphQLEndpoint, {
|
52
|
+
method: 'post',
|
53
|
+
headers: JSON.parse(graphiqlContainer.dataset.headers),
|
54
|
+
body: JSON.stringify(graphQLParams),
|
55
|
+
credentials: 'include',
|
56
|
+
}).then(function(response) {
|
57
|
+
try {
|
58
|
+
return response.json();
|
59
|
+
} catch(error) {
|
60
|
+
return {
|
61
|
+
"status": response.status,
|
62
|
+
"message": "The server responded with invalid JSON, this is probably a server-side error",
|
63
|
+
"response": response.text(),
|
64
|
+
};
|
65
|
+
}
|
66
|
+
})
|
67
|
+
}
|
68
|
+
|
69
|
+
var initial_query = graphiqlContainer.dataset.initialQuery;
|
70
|
+
|
71
|
+
if (initial_query) {
|
72
|
+
var defaultQuery = initial_query;
|
73
|
+
} else {
|
74
|
+
var defaultQuery = undefined;
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
// Render <GraphiQL /> into the body.
|
79
|
+
var element_props = { fetcher: graphQLFetcher, defaultQuery: defaultQuery };
|
80
|
+
|
81
|
+
if (queryParams === 'true') {
|
82
|
+
queryParams = Object.assign({}, queryParams, { query: parameters.query, variables: parameters.variables, onEditQuery: onEditQuery, onEditVariables: onEditVariables });
|
83
|
+
}
|
84
|
+
|
85
|
+
ReactDOM.render(
|
86
|
+
React.createElement(GraphiQL, element_props,
|
87
|
+
React.createElement(GraphiQL.Logo, {}, graphiqlContainer.dataset.logo)
|
88
|
+
),
|
89
|
+
document.getElementById("graphiql-container")
|
90
|
+
);
|
91
|
+
});
|
@@ -7,97 +7,12 @@
|
|
7
7
|
<%= javascript_include_tag("graphiql/rails/application") %>
|
8
8
|
</head>
|
9
9
|
<body>
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
// Parse the search string to get url parameters.
|
18
|
-
var search = window.location.search;
|
19
|
-
search.substr(1).split('&').forEach(function (entry) {
|
20
|
-
var eq = entry.indexOf('=');
|
21
|
-
if (eq >= 0) {
|
22
|
-
parameters[decodeURIComponent(entry.slice(0, eq))] =
|
23
|
-
decodeURIComponent(entry.slice(eq + 1));
|
24
|
-
}
|
25
|
-
});
|
26
|
-
// if variables was provided, try to format it.
|
27
|
-
if (parameters.variables) {
|
28
|
-
try {
|
29
|
-
parameters.variables =
|
30
|
-
JSON.stringify(JSON.parse(parameters.variables), null, 2);
|
31
|
-
} catch (e) {
|
32
|
-
// Do nothing, we want to display the invalid JSON as a string, rather
|
33
|
-
// than present an error.
|
34
|
-
}
|
35
|
-
}
|
36
|
-
// When the query and variables string is edited, update the URL bar so
|
37
|
-
// that it can be easily shared
|
38
|
-
function onEditQuery(newQuery) {
|
39
|
-
parameters.query = newQuery;
|
40
|
-
updateURL();
|
41
|
-
}
|
42
|
-
function onEditVariables(newVariables) {
|
43
|
-
parameters.variables = newVariables;
|
44
|
-
updateURL();
|
45
|
-
}
|
46
|
-
function updateURL() {
|
47
|
-
var newSearch = '?' + Object.keys(parameters).map(function (key) {
|
48
|
-
return encodeURIComponent(key) + '=' +
|
49
|
-
encodeURIComponent(parameters[key]);
|
50
|
-
}).join('&');
|
51
|
-
history.replaceState(null, null, newSearch);
|
52
|
-
}
|
53
|
-
<% end %>
|
54
|
-
|
55
|
-
// Defines a GraphQL fetcher using the fetch API.
|
56
|
-
var graphQLEndpoint = "<%= graphql_endpoint_path %>";
|
57
|
-
function graphQLFetcher(graphQLParams) {
|
58
|
-
return fetch(graphQLEndpoint, {
|
59
|
-
method: 'post',
|
60
|
-
headers: <%= raw JSON.pretty_generate(GraphiQL::Rails.config.resolve_headers(self)) %>,
|
61
|
-
body: JSON.stringify(graphQLParams),
|
62
|
-
credentials: 'include',
|
63
|
-
}).then(function(response) {
|
64
|
-
try {
|
65
|
-
return response.json();
|
66
|
-
} catch(error) {
|
67
|
-
return {
|
68
|
-
"status": response.status,
|
69
|
-
"message": "The server responded with invalid JSON, this is probably a server-side error",
|
70
|
-
"response": response.text(),
|
71
|
-
};
|
72
|
-
}
|
73
|
-
})
|
74
|
-
}
|
75
|
-
|
76
|
-
<% if GraphiQL::Rails.config.initial_query %>
|
77
|
-
var defaultQuery = "<%= GraphiQL::Rails.config.initial_query.gsub("\n", '\n').gsub('"', '\"').html_safe %>";
|
78
|
-
<% else %>
|
79
|
-
var defaultQuery = undefined
|
80
|
-
<% end %>
|
81
|
-
|
82
|
-
// Render <GraphiQL /> into the body.
|
83
|
-
ReactDOM.render(
|
84
|
-
React.createElement(GraphiQL,
|
85
|
-
{
|
86
|
-
fetcher: graphQLFetcher,
|
87
|
-
defaultQuery: defaultQuery,
|
88
|
-
<% if GraphiQL::Rails.config.query_params %>
|
89
|
-
query: parameters.query,
|
90
|
-
variables: parameters.variables,
|
91
|
-
onEditQuery: onEditQuery,
|
92
|
-
onEditVariables: onEditVariables
|
93
|
-
<% end %>
|
94
|
-
},
|
95
|
-
<% if GraphiQL::Rails.config.logo %>
|
96
|
-
React.createElement(GraphiQL.Logo, {}, "<%= GraphiQL::Rails.config.logo %>")
|
97
|
-
<% end %>
|
98
|
-
),
|
99
|
-
document.getElementById("graphiql-container")
|
100
|
-
);
|
101
|
-
</script>
|
10
|
+
<%= content_tag :div, 'Loading...', id: 'graphiql-container', data: {
|
11
|
+
graphql_endpoint_path: graphql_endpoint_path,
|
12
|
+
initial_query: GraphiQL::Rails.config.initial_query,
|
13
|
+
logo: GraphiQL::Rails.config.logo,
|
14
|
+
headers: GraphiQL::Rails.config.resolve_headers(self),
|
15
|
+
query_params: GraphiQL::Rails.config.query_params
|
16
|
+
} %>
|
102
17
|
</body>
|
103
18
|
</html>
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
3
|
module GraphiQL
|
4
4
|
module Rails
|
@@ -16,28 +16,27 @@ module GraphiQL
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def graphql_params
|
19
|
-
{ params: { graphql_path:
|
19
|
+
{ params: { graphql_path: '/my/endpoint' } }
|
20
20
|
end
|
21
21
|
|
22
|
-
test
|
22
|
+
test 'renders GraphiQL' do
|
23
23
|
get :show, graphql_params
|
24
24
|
assert_response(:success)
|
25
|
-
assert_includes(@response.body,
|
26
|
-
|
27
|
-
assert_match(/application-\w+\.js/, @response.body, "it includes assets")
|
25
|
+
assert_includes(@response.body, 'my/endpoint', 'it uses the provided path')
|
26
|
+
assert_match(/application-\w+\.js/, @response.body, 'it includes assets')
|
28
27
|
end
|
29
28
|
|
30
|
-
test
|
29
|
+
test 'it uses initial_query config' do
|
31
30
|
GraphiQL::Rails.config.initial_query = '{ customQuery(id: "123") }'
|
32
31
|
get :show, graphql_params
|
33
|
-
assert_includes(@response.body, '"{ customQuery(id:
|
32
|
+
assert_includes(@response.body, '"{ customQuery(id: "123") }"')
|
34
33
|
|
35
34
|
GraphiQL::Rails.config.initial_query = nil
|
36
35
|
get :show, graphql_params
|
37
|
-
refute_includes(@response.body, '"{ customQuery(id:
|
36
|
+
refute_includes(@response.body, '"{ customQuery(id: "123") }"')
|
38
37
|
end
|
39
38
|
|
40
|
-
test
|
39
|
+
test 'it uses title config' do
|
41
40
|
GraphiQL::Rails.config.title = 'Custom Title'
|
42
41
|
get :show, graphql_params
|
43
42
|
assert_includes(@response.body, '<title>Custom Title</title>')
|
@@ -47,30 +46,30 @@ module GraphiQL
|
|
47
46
|
assert_includes(@response.body, '<title>GraphiQL</title>')
|
48
47
|
end
|
49
48
|
|
50
|
-
test
|
49
|
+
test 'it uses logo config' do
|
51
50
|
GraphiQL::Rails.config.logo = 'Custom Logo'
|
52
51
|
get :show, graphql_params
|
53
|
-
assert_includes(@response.body,
|
52
|
+
assert_includes(@response.body, %(data-logo="Custom Logo"))
|
54
53
|
|
55
54
|
GraphiQL::Rails.config.logo = nil
|
56
55
|
get :show, graphql_params
|
57
|
-
refute_includes(@response.body,
|
56
|
+
refute_includes(@response.body, %(data-logo="Custom Logo"))
|
58
57
|
end
|
59
58
|
|
60
|
-
test
|
59
|
+
test 'it uses query_params config' do
|
61
60
|
get :show, graphql_params
|
62
|
-
|
61
|
+
assert_includes(@response.body, %(data-query-params="false"))
|
63
62
|
|
64
63
|
GraphiQL::Rails.config.query_params = true
|
65
64
|
get :show, graphql_params
|
66
|
-
assert_includes(@response.body, "
|
65
|
+
assert_includes(@response.body, %(data-query-params="true"))
|
67
66
|
end
|
68
67
|
|
69
|
-
test
|
70
|
-
GraphiQL::Rails.config.headers[
|
68
|
+
test 'it renders headers' do
|
69
|
+
GraphiQL::Rails.config.headers['Nonsense-Header'] = ->(_view_ctx) { 'Value' }
|
71
70
|
get :show, graphql_params
|
72
|
-
assert_includes(@response.body,
|
73
|
-
assert_includes(@response.body,
|
71
|
+
assert_includes(@response.body, %("Nonsense-Header":"Value"))
|
72
|
+
assert_includes(@response.body, %("X-CSRF-Token":"))
|
74
73
|
end
|
75
74
|
end
|
76
75
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphiql-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Mosolgo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-01-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -147,6 +147,7 @@ files:
|
|
147
147
|
- app/assets/javascripts/graphiql/rails/application.js
|
148
148
|
- app/assets/javascripts/graphiql/rails/fetch-0.10.1.js
|
149
149
|
- app/assets/javascripts/graphiql/rails/graphiql-0.12.0.js
|
150
|
+
- app/assets/javascripts/graphiql/rails/graphiql_show.js
|
150
151
|
- app/assets/javascripts/graphiql/rails/react-16.6.0.js
|
151
152
|
- app/assets/javascripts/graphiql/rails/react-dom-16.6.0.js
|
152
153
|
- app/assets/stylesheets/graphiql/rails/application.css
|