Skip to content

Allow loading schema from SDL definition file as well as JSON #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions lib/graphql/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ 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))
end
Expand Down
6 changes: 6 additions & 0 deletions test/test_client_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ 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_ignores_missing_path
refute GraphQL::Client.load_schema("#{__dir__}/missing-schema.json")
end
Expand Down
Loading