Implement schema loading from adapter

This commit is contained in:
Joshua Peek 2016-09-13 17:35:11 -07:00
Родитель a145e9955d
Коммит e86cad66c9
2 изменённых файлов: 43 добавлений и 11 удалений

Просмотреть файл

@ -21,6 +21,8 @@ module GraphQL
attr_accessor :document_tracking_enabled
IntrospectionDocument = GraphQL.parse(GraphQL::Introspection::INTROSPECTION_QUERY).deep_freeze
def self.load_schema(schema)
case schema
when GraphQL::Schema
@ -33,6 +35,17 @@ module GraphQL
else
load_schema(JSON.parse(schema))
end
else
if schema.respond_to?(:execute)
load_schema(
schema.execute(
document: IntrospectionDocument,
operation_name: "IntrospectionQuery",
variables: {},
context: {}
)
)
end
end
end
@ -236,17 +249,6 @@ module GraphQL
Response.for(definition, result)
end
IntrospectionDocument = GraphQL.parse(GraphQL::Introspection::INTROSPECTION_QUERY).deep_freeze
def fetch_schema
execute.execute(
document: IntrospectionDocument,
operation_name: "IntrospectionQuery",
variables: {},
context: {}
)
end
# Internal: FragmentSpread and FragmentDefinition extension to allow its
# name to point to a lazily defined Proc instead of a static string.
module LazyName

Просмотреть файл

@ -0,0 +1,30 @@
require "graphql"
require "graphql/client"
require "json"
require "minitest/autorun"
class TestClientSchema < MiniTest::Test
QueryType = GraphQL::ObjectType.define do
name "AwesomeQuery"
field :version, !types.Int
end
Schema = GraphQL::Schema.define(query: QueryType)
def test_load_schema_identity
schema = GraphQL::Client.load_schema(Schema)
assert_equal "AwesomeQuery", schema.query.name
end
def test_load_schema_from_introspection_query_result
result = Schema.execute(GraphQL::Introspection::INTROSPECTION_QUERY)
schema = GraphQL::Client.load_schema(result)
assert_equal "AwesomeQuery", schema.query.name
end
def test_load_schema_from_json_string
json = JSON.generate(Schema.execute(GraphQL::Introspection::INTROSPECTION_QUERY))
schema = GraphQL::Client.load_schema(json)
assert_equal "AwesomeQuery", schema.query.name
end
end