Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/graphql/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ def self.load_schema(schema)
when String
if schema.end_with?(".json") && File.exist?(schema)
load_schema(File.read(schema))
elsif (schema.end_with?(".graphql", ".graphqls")) && File.exist?(schema)
GraphQL::Schema.from_definition(schema)
elsif schema =~ /\A\s*{/
load_schema(JSON.parse(schema, freeze: true))
elsif schema.start_with?("schema")
GraphQL::Schema.from_definition(schema)
end
else
if schema.respond_to?(:execute)
Expand Down
16 changes: 16 additions & 0 deletions test/test_client_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ def test_load_schema_from_json_string
assert_equal "AwesomeQuery", schema.query.graphql_name
end

def test_load_schema_from_definition_string
definition = Schema.to_definition
schema = GraphQL::Client.load_schema(definition)
assert_equal "AwesomeQuery", schema.query.graphql_name
end

def test_load_schema_from_definition_file
definition = Schema.to_definition
Tempfile.create(["schema", ".graphql"]) do |file|
file.write(definition)
file.close
schema = GraphQL::Client.load_schema(file.path)
assert_equal "AwesomeQuery", schema.query.graphql_name
end
end

def test_load_schema_ignores_missing_path
refute GraphQL::Client.load_schema("#{__dir__}/missing-schema.json")
end
Expand Down