Skip to content
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ nytprof.out
*.bs
/_eumm/
.DS_Store
/local/
cpanfile.snapshot
6 changes: 6 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Revision history for Perl extension graphql-perl

{{$NEXT}}

- original version

2 changes: 1 addition & 1 deletion lib/GraphQL/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ sub stringify {
return
ref($value)
&& $value == NULLISH ? 'null'
: ref($value) ? encode_json($value)
: ref($value) ? JSON->new->canonical(1)->encode($value)
: qq'"$value"';
}

Expand Down
2 changes: 1 addition & 1 deletion lib/GraphQL/Validator/Rule/FieldsOnCorrectType.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ sub undefined_field_message {
my $message = qq`Cannot query field "$field_name" on type "${ stringify_type($type) }".`;

if ($suggested_type_names && @$suggested_type_names) {
my $suggestions = quoted_or_list($suggested_type_names);
my $suggestions = quoted_or_list([sort @$suggested_type_names]);
$message .= " Did you mean to use an inline fragment on $suggestions?";
}
elsif ($suggested_field_names && @$suggested_field_names) {
Expand Down
5 changes: 5 additions & 0 deletions minil.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name = "GraphQL"

# badges = ["travis"]
module_maker="ModuleBuildTiny"

9 changes: 5 additions & 4 deletions t/graphql/execute-resolve.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use warnings;

use Test::More;
use Test::Deep;
use JSON qw/encode_json/;

use GraphQL qw/graphql :types/;
use GraphQL qw/:types/;
use GraphQL::Language::Parser qw/parse/;
use GraphQL::Execute qw/execute/;

use lib "t/lib";
use test_helper qw/graphql execute encode_json/;

sub testSchema {
my $testField = shift;
Expand Down Expand Up @@ -103,7 +104,7 @@ subtest 'uses provided resolve function' => sub {
};

is_deeply graphql($schema, '{ test(aInt: -123, aStr: "String!") }', 'Source!'), {
data => { test => '["Source!",{"aStr":"String!","aInt":-123}]' }
data => { test => '["Source!",{"aInt":-123,"aStr":"String!"}]' }
};
};

Expand Down
16 changes: 10 additions & 6 deletions t/graphql/execute-variables.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ use warnings;
use DDP;
use Test::More;
use Test::Deep;
use JSON qw/encode_json/;

use GraphQL qw/graphql :types/;
use GraphQL qw/:types/;
use GraphQL::Nullish qw/NULLISH/;
use GraphQL::Language::Parser qw/parse/;
use GraphQL::Execute qw/execute/;
use GraphQL::Util qw/stringify/;

use lib "t/lib";
use test_helper qw/graphql execute encode_json/;

my $TestComplexScalar = GraphQLScalarType(
name => 'ComplexScalar',
serialize => sub {
Expand Down Expand Up @@ -362,10 +363,13 @@ EOQ
# NOTE: Flunky because of unordered hashes
cmp_deeply $e, noclass(superhashof({
locations => [{ line => 2, column => 19 }],
message => qq'Variable "\$input" got invalid value ${ \encode_json($params->{input}) }.'
. qq'\nIn field "nb": Expected "String!", found null.'
. qq'\nIn field "na": In field "c": Expected "String!", found null.'
}));

my $err_msgs = [sort split("\n", $e->{message})];
is_deeply $err_msgs, [qq'In field "na": In field "c": Expected "String!", found null.',
qq'In field "nb": Expected "String!", found null.',
qq'Variable "\$input" got invalid value ${ \encode_json($params->{input}) }.'];

};

subtest 'errors on addition of unknown input field' => sub {
Expand Down
4 changes: 2 additions & 2 deletions t/graphql/type.t
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ subtest 'defines an enum type with a value of `null` and `undefined`' => sub {
},
);

is_deeply $EnumTypeWithNullishValue->get_values, [
is_deeply [sort {$a->{name} cmp $b->{name}} @{$EnumTypeWithNullishValue->get_values}], [sort {$a->{name} cmp $b->{name}} (
{
name => 'NULL',
description => undef,
Expand All @@ -192,7 +192,7 @@ subtest 'defines an enum type with a value of `null` and `undefined`' => sub {
deprecation_reason => undef,
value => 'UNDEFINED', # XXX WAS value: undefined
},
];
)];
};

subtest 'defines an object type with deprecated field' => sub {
Expand Down
18 changes: 14 additions & 4 deletions t/graphql/type/introspection.t
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ subtest 'identifies deprecated fields'=> sub {
}
EOQ

is_deeply graphql($schema, $request), {
is_deeply sort_keys( graphql( $schema, $request ), [qw/fields/] ), {
data => {
__type => {
name => 'TestType',
Expand Down Expand Up @@ -1114,7 +1114,7 @@ subtest 'respects the includeDeprecated parameter for fields'=> sub {
}
EOQ

is_deeply graphql($schema, $request), {
is_deeply sort_keys( graphql( $schema, $request ), [qw/trueFields falseFields omittedFields/] ), {
data => {
__type => {
name => 'TestType',
Expand Down Expand Up @@ -1166,7 +1166,7 @@ subtest 'identifies deprecated enum values'=> sub {
}
EOQ

is_deeply graphql($schema, $request), {
is_deeply sort_keys( graphql( $schema, $request ), [qw/enum_values/] ), {
data => {
__type => {
name => 'TestEnum',
Expand Down Expand Up @@ -1229,7 +1229,7 @@ subtest 'respects the includeDeprecated parameter for enum values'=> sub {
}
EOQ

is_deeply graphql($schema, $request), {
is_deeply sort_keys( graphql( $schema, $request ), [qw/trueValues falseValues omittedValues/] ), {
data => {
__type => {
name => 'TestEnum',
Expand Down Expand Up @@ -1400,3 +1400,13 @@ EOQ
};

done_testing;

sub sort_keys {
my($result, $keys) = @_;

foreach my $key (@$keys){
$result->{data}->{__type}->{$key} = [sort {$b->{name} cmp $a->{name}} @{$result->{data}->{__type}->{$key}}];
}

return $result;
}
2 changes: 1 addition & 1 deletion t/graphql/validator/rules/fields_on_correct_type.t
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ subtest 'Defined on implementors queried on union' => sub {
name
}',
#TODO: NOTE: ORIG [undefined_field( 'name', 'CatOrDog', ['Being', 'Pet', 'Canine', 'Dog', 'Cat'], [], 3, 9)]
[undefined_field( 'name', 'CatOrDog', ['Pet', 'Being', 'Canine', 'Dog', 'Cat'], [], 3, 9)]
[undefined_field( 'name', 'CatOrDog', [sort ('Pet', 'Being', 'Canine', 'Dog', 'Cat')], [], 3, 9)]
);
};

Expand Down
71 changes: 71 additions & 0 deletions t/lib/test_helper.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package test_helper;

# this project's tests randomly fail comparisons due to the uncertain ordering of hash keys.
# test_helper is a shim between the existing tests and GraphQL-Perl as well as JSON to address this issue.
#
# the test_helper package wraps 'graphql' and the lower level 'execute' methods
# so that the response keys can be sorted prior to comparision testing.
#
# similarly 'encode_json' is provide so that JSON will sort before encoding.
#
# place the following in tests that need sorting support.
#
# use lib "t/lib";
# use test_helper qw/graphql execute encode_json/;

use strict;
use warnings;

use Exporter qw/import/;

use JSON qw//;
use GraphQL;
use GraphQL::Execute;

our @ISA = qw(Exporter);
our @EXPORT_OK = (
qw/
graphql
execute
encode_json
/
);

# enable sorted keys in JSON testing
my $json = JSON->new->canonical(1);

sub encode_json {
$json->encode(shift);
}

sub graphql {
return sort_response( GraphQL::graphql(@_) );
}

sub execute {
return sort_response( GraphQL::Execute::execute(@_) );
}

sub sort_response {
my $resp = shift;

if ( 'HASH' eq ref $resp
&& defined $resp->{data}
&& 'HASH' eq ref $resp->{data} )
{
foreach my $key ( keys %{ $resp->{data} } ) {

# if data looks like json then lets try to parse and re-encode it sorted
if ( $resp->{data}->{$key}
&& $resp->{data}->{$key} =~ m/^\{[^}]+\}/ )
{
$resp->{data}->{$key}
= $json->encode( $json->decode( $resp->{data}->{$key} ) );
}
}
}

return $resp;
}

1;