Skip to content

Commit c9755e5

Browse files
committed
Allow loading schema from SDL definition file as well as JSON
If passing a path to a file with a name that ends in ".graphql" or ".graphqls" (matching the filenames used in [graphql-ruby](https://github.com/rmosolgo/graphql-ruby/blob/master/lib/graphql/schema.rb#L116)), attempt to load the schema from that file assuming it's a schema definition rather than a JSON schema.
1 parent 72a2eec commit c9755e5

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/graphql/client.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ def self.load_schema(schema)
5858
GraphQL::Schema.from_definition(schema)
5959
elsif schema =~ /\A\s*{/
6060
load_schema(JSON.parse(schema, freeze: true))
61+
elsif schema.start_with?("schema")
62+
GraphQL::Schema.from_definition(schema)
6163
end
6264
else
6365
if schema.respond_to?(:execute)

test/test_client_schema.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ def test_load_schema_from_definition_string
4848
assert_equal "AwesomeQuery", schema.query.graphql_name
4949
end
5050

51+
def test_load_schema_from_definition_file
52+
definition = Schema.to_definition
53+
Tempfile.create(["schema", ".graphql"]) do |file|
54+
file.write(definition)
55+
file.close
56+
schema = GraphQL::Client.load_schema(file.path)
57+
assert_equal "AwesomeQuery", schema.query.graphql_name
58+
end
59+
end
60+
5161
def test_load_schema_ignores_missing_path
5262
refute GraphQL::Client.load_schema("#{__dir__}/missing-schema.json")
5363
end

0 commit comments

Comments
 (0)