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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -1
  3. data/lib/graphql/client.rb +27 -14
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef076f9ef38c77eab90a79b2c0e26433fc213966
4
- data.tar.gz: 967304326cf13c1419cf7d58544f176d22e062c7
3
+ metadata.gz: 35d033dd34fc07b0321711ab80afe22a67d6bc24
4
+ data.tar.gz: fdc1351584a59b2fd907cb9231d3385f65c9ff7c
5
5
  SHA512:
6
- metadata.gz: 4631d663bdd60ccb619f171d3690e1289b1989251e7834932f8ac9ab426719f2bb8a710ba5766b0a307c56ebf8d6c9861e46861789f5fa160c6f9c8136fb1def
7
- data.tar.gz: 2549a05547302f9da144400ac806a3d6e076cc4bfe3d282db70c56db3d01b9f3ebbf9805b732e69ab68d7a778c2564750fef283d97e33d4c10839bc7674b05b0
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
- Schema = GraphQL::Client.load_schema("path/to/schema.json")
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
@@ -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
- else
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
- load_schema(
41
- schema.execute(
42
- document: IntrospectionDocument,
43
- operation_name: "IntrospectionQuery",
44
- variables: {},
45
- context: {}
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)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.21
4
+ version: 0.0.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - GitHub