graphql-client 0.0.21 → 0.0.22
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -1
- data/lib/graphql/client.rb +27 -14
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 35d033dd34fc07b0321711ab80afe22a67d6bc24
|
4
|
+
data.tar.gz: fdc1351584a59b2fd907cb9231d3385f65c9ff7c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d7f52e410a8929897c9b22f14b19a1bce944a0133d41eb1fc1f6947124c7e9770415115fd9563af11b3b6cb80e4f33fe6a87e104a41dc4bcfc15e65448186f42
|
7
|
+
data.tar.gz: 535bc0149c5447b2004428bf92cc9e4806a5e84aa044e23e62c232fc9e0b3e5e0b66d8dba2b2a9eab8ed76e00925e7fca99ca4b1aed4057313a16a038806ea72
|
data/README.md
CHANGED
@@ -18,7 +18,10 @@ module SWAPI
|
|
18
18
|
# Fetch latest schema on boot,
|
19
19
|
Schema = GraphQL::Client.load_schema(HTTP)
|
20
20
|
# However, its smart to dump this to a JSON file and load from disk
|
21
|
-
|
21
|
+
#
|
22
|
+
# GraphQL::Client.dump_schema(HTTP, "path/to/schema.json")
|
23
|
+
#
|
24
|
+
# Schema = GraphQL::Client.load_schema("path/to/schema.json")
|
22
25
|
|
23
26
|
Client = GraphQL::Client.new(schema: Schema, execute: HTTP)
|
24
27
|
end
|
data/lib/graphql/client.rb
CHANGED
@@ -6,6 +6,7 @@ require "graphql/client/query_result"
|
|
6
6
|
require "graphql/client/response"
|
7
7
|
require "graphql/language/nodes/deep_freeze_ext"
|
8
8
|
require "graphql/language/operation_slice"
|
9
|
+
require "json"
|
9
10
|
|
10
11
|
module GraphQL
|
11
12
|
# GraphQL Client helps build and execute queries against a GraphQL backend.
|
@@ -21,8 +22,6 @@ module GraphQL
|
|
21
22
|
|
22
23
|
attr_accessor :document_tracking_enabled
|
23
24
|
|
24
|
-
IntrospectionDocument = GraphQL.parse(GraphQL::Introspection::INTROSPECTION_QUERY).deep_freeze
|
25
|
-
|
26
25
|
def self.load_schema(schema)
|
27
26
|
case schema
|
28
27
|
when GraphQL::Schema
|
@@ -30,23 +29,37 @@ module GraphQL
|
|
30
29
|
when Hash
|
31
30
|
GraphQL::Schema::Loader.load(schema)
|
32
31
|
when String
|
33
|
-
if schema.end_with?(".json")
|
32
|
+
if schema.end_with?(".json") && File.exist?(schema)
|
34
33
|
load_schema(File.read(schema))
|
35
|
-
|
34
|
+
elsif schema =~ /\A\s*{/
|
36
35
|
load_schema(JSON.parse(schema))
|
37
36
|
end
|
38
37
|
else
|
39
|
-
if schema.respond_to?(:execute)
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
38
|
+
load_schema(dump_schema(schema)) if schema.respond_to?(:execute)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
IntrospectionDocument = GraphQL.parse(GraphQL::Introspection::INTROSPECTION_QUERY).deep_freeze
|
43
|
+
|
44
|
+
def self.dump_schema(schema, io = nil)
|
45
|
+
unless schema.respond_to?(:execute)
|
46
|
+
raise TypeError, "expected schema to respond to #execute(), but was #{schema.class}"
|
49
47
|
end
|
48
|
+
|
49
|
+
result = schema.execute(
|
50
|
+
document: IntrospectionDocument,
|
51
|
+
operation_name: "IntrospectionQuery",
|
52
|
+
variables: {},
|
53
|
+
context: {}
|
54
|
+
)
|
55
|
+
|
56
|
+
if io
|
57
|
+
io = IO.new(io, "w") if io.is_a?(String)
|
58
|
+
io.write(JSON.pretty_generate(result))
|
59
|
+
io.close_write
|
60
|
+
end
|
61
|
+
|
62
|
+
result
|
50
63
|
end
|
51
64
|
|
52
65
|
def initialize(schema: nil, execute: nil)
|