diff --git a/lib/graphql/client.rb b/lib/graphql/client.rb index 6fea76d..dcc4b0c 100644 --- a/lib/graphql/client.rb +++ b/lib/graphql/client.rb @@ -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) diff --git a/test/test_client_schema.rb b/test/test_client_schema.rb index bb8d7fb..351bce6 100644 --- a/test/test_client_schema.rb +++ b/test/test_client_schema.rb @@ -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