Skip to content

Commit 393dea4

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 aff5882 commit 393dea4

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

lib/graphql/client.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def self.load_schema(schema)
5454
when String
5555
if schema.end_with?(".json") && File.exist?(schema)
5656
load_schema(File.read(schema))
57+
elsif (schema.end_with?(".graphql", ".graphqls")) && File.exist?(schema)
58+
GraphQL::Schema.from_definition(schema)
5759
elsif schema =~ /\A\s*{/
5860
load_schema(JSON.parse(schema, freeze: true))
5961
end

test/test_client_schema.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ def test_load_schema_from_json_string
4242
assert_equal "AwesomeQuery", schema.query.graphql_name
4343
end
4444

45+
def test_load_schema_from_definition_string
46+
definition = Schema.to_definition
47+
schema = GraphQL::Client.load_schema(definition)
48+
assert_equal "AwesomeQuery", schema.query.graphql_name
49+
end
50+
4551
def test_load_schema_ignores_missing_path
4652
refute GraphQL::Client.load_schema("#{__dir__}/missing-schema.json")
4753
end

0 commit comments

Comments
 (0)