# frozen_string_literal: true require "spec_helper" describe GraphQL::Language::Parser do subject { GraphQL::Language::Parser } describe "anonymous fragment extension" do let(:document) { GraphQL.parse(query_string) } let(:query_string) {%| fragment on NestedType @or(something: "ok") { anotherNestedField } |} let(:fragment) { document.definitions.first } it "creates an anonymous fragment definition" do assert fragment.is_a?(GraphQL::Language::Nodes::FragmentDefinition) assert_equal nil, fragment.name assert_equal 1, fragment.selections.length assert_equal "NestedType", fragment.type.name assert_equal 1, fragment.directives.length assert_equal [2, 7], fragment.position end end it "parses empty arguments" do strings = [ "{ field { inner } }", "{ field() { inner }}", ] strings.each do |query_str| doc = subject.parse(query_str) field = doc.definitions.first.selections.first assert_equal 0, field.arguments.length assert_equal 1, field.selections.length end end it "parses the test schema" do schema = Dummy::Schema schema_string = GraphQL::Schema::Printer.print_schema(schema) document = subject.parse(schema_string) assert_equal schema_string, document.to_query_string end describe ".parse_file" do it "assigns filename to all nodes" do example_filename = "spec/support/parser/filename_example.graphql" doc = GraphQL.parse_file(example_filename) assert_equal example_filename, doc.filename field = doc.definitions[0].selections[0].selections[0] assert_equal example_filename, field.filename end it "raises errors with filename" do error_filename = "spec/support/parser/filename_example_error_1.graphql" err = assert_raises(GraphQL::ParseError) { GraphQL.parse_file(error_filename) } assert_includes err.message, error_filename error_filename_2 = "spec/support/parser/filename_example_error_2.graphql" err_2 = assert_raises(GraphQL::ParseError) { GraphQL.parse_file(error_filename_2) } assert_includes err_2.message, error_filename_2 assert_includes err_2.message, "3, 11" end end end