| 1 | package DBIO::GraphQL; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Auto-generate a GraphQL schema from a DBIO schema | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 14 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 14 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 14 | ▶ | use MIME::Base64 qw(encode_base64 decode_base64); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 14 | ▶ | use GraphQL::Schema; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 14 | ▶ | use GraphQL::Type::Object; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 14 | ▶ | use GraphQL::Type::List; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 14 | ▶ | use GraphQL::Type::Enum; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 14 | ▶ | use GraphQL::Type::Scalar qw($Int $String $Boolean); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 14 | ▶ | use DBIO::GraphQL::ScalarMap; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 14 | ▶ | use DBIO::GraphQL::Filter::Search; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 14 | ▶ | use DBIO::GraphQL::Relationship; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 14 | ▶ | use DBIO::GraphQL::Mutation; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 14 | ▶ | use DBIO::GraphQL::Policy; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 14 | ▶ | use GraphQL::Language::Parser (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 14 | ▶ | use GraphQL::Execution (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 14 | ▶ | use DBIO::GraphQL::Limits; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 14 | ▶ | use DBIO::GraphQL::Error qw(client_error); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 14 | my %OPTIONS = map { $_ => 1 } qw( | |||||||||||||||||||||||||||||||||||||||||
| 28 | sources except hide readonly mutations authorize on_error introspection | ||||||||||||||||||||||||||||||||||||||||||
| 29 | default_rows max_rows max_in max_depth max_cost | ||||||||||||||||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 105 | ▶ | sub to_graphql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_graphql: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 105 | my ($class, $db, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 105 | for my $opt (sort keys %opts) { | |||||||||||||||||||||||||||||||||||||||||
| 36 | die "DBIO::GraphQL: unknown to_graphql option '$opt'\n" | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 107 | ▶ | unless $OPTIONS{$opt}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 105 | my $policy = DBIO::GraphQL::Policy->new(schema => $db, %opts); | |||||||||||||||||||||||||||||||||||||||||
| 40 | 79 | my @monikers = $policy->monikers; | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 79 | my $filter = DBIO::GraphQL::Filter::Search->new(schema => $db, policy => $policy); | |||||||||||||||||||||||||||||||||||||||||
| 43 | 79 | my $relationship = DBIO::GraphQL::Relationship->new(schema => $db, policy => $policy); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 79 | my $mutation = DBIO::GraphQL::Mutation->new(schema => $db, policy => $policy); | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 79 | my $shared = _build_shared_types(); | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | # The error envelope around every resolver: client errors pass, a database | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # refusal is classified, anything else is "internal error" (DBIO::GraphQL::Error). | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 665 | ▶ | my $guard = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 665 | my ($field) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 52 | $field->{resolve} = DBIO::GraphQL::Error->guard($field->{resolve}, $policy->on_error) | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 665 | ▶ | if $field->{resolve}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 665 | return $field; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 79 | }; | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | # Phase 1: shell types (one per source). Empty fields sub; filled | ||||||||||||||||||||||||||||||||||||||||||
| 58 | # in Phase 2 once every source has a shell. | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 79 | my %gql_types; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 79 | for my $moniker (@monikers) { | |||||||||||||||||||||||||||||||||||||||||
| 61 | $gql_types{$moniker} = GraphQL::Type::Object->new( | ||||||||||||||||||||||||||||||||||||||||||
| 62 | name => $moniker, | ||||||||||||||||||||||||||||||||||||||||||
| 63 | description => "Auto-generated GraphQL type for DBIO source '$moniker'", | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | fields => sub { {} }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 176 | ); | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | # Phase 2: real fields. Snapshot of %gql_types at the top so that | ||||||||||||||||||||||||||||||||||||||||||
| 69 | # a relationship's target type lookup never accidentally sees a | ||||||||||||||||||||||||||||||||||||||||||
| 70 | # half-built object in this same loop iteration. | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 79 | for my $moniker (@monikers) { | |||||||||||||||||||||||||||||||||||||||||
| 72 | 176 | my $source_obj = $db->source($moniker); | |||||||||||||||||||||||||||||||||||||||||
| 73 | 176 | my %types_snapshot = %gql_types; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 176 | my @visible = $policy->visible_columns($moniker); | |||||||||||||||||||||||||||||||||||||||||
| 75 | 176 | ▶ | die "DBIO::GraphQL: source '$moniker' has no visible column\n" unless @visible; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 94 | ▶ | $gql_types{$moniker}->{fields} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 94 | my %fields; | |||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 94 | for my $col (@visible) { | |||||||||||||||||||||||||||||||||||||||||
| 81 | 341 | $fields{$col} = { | |||||||||||||||||||||||||||||||||||||||||
| 82 | type => DBIO::GraphQL::ScalarMap::for_column($source_obj, $col), | ||||||||||||||||||||||||||||||||||||||||||
| 83 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 94 | for my $rel ($source_obj->relationships) { | |||||||||||||||||||||||||||||||||||||||||
| 87 | 94 | my $field = $relationship->build_field( | |||||||||||||||||||||||||||||||||||||||||
| 88 | $source_obj, $rel, \%types_snapshot | ||||||||||||||||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 94 | ▶ | $fields{$rel} = $guard->($field) if $field; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 94 | return \%fields; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 176 | }; | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | # Phase 3: root Query + Mutation types | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 79 | my (%query_fields, %mutation_fields); | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 79 | for my $moniker (@monikers) { | |||||||||||||||||||||||||||||||||||||||||
| 101 | 176 | my $graphql_type = $gql_types{$moniker}; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 176 | my $source_obj = $db->source($moniker); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 176 | my $singular_args = _build_singular_args($source_obj); | |||||||||||||||||||||||||||||||||||||||||
| 104 | 176 | my $filter_type = $filter->type_for($moniker); | |||||||||||||||||||||||||||||||||||||||||
| 105 | 176 | my $connection_type = _build_connection_type($moniker, $graphql_type); | |||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | # Singular query - only for sources with a primary key, it is the | ||||||||||||||||||||||||||||||||||||||||||
| 108 | # lookup by that key. | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 176 | ▶ | if (%$singular_args) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | $query_fields{ lcfirst($moniker) } = { | ||||||||||||||||||||||||||||||||||||||||||
| 111 | type => $graphql_type, | ||||||||||||||||||||||||||||||||||||||||||
| 112 | args => $singular_args, | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 54 | ▶ | resolve => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 54 | my ($root, $args, $ctx) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 54 | my $restriction = $policy->authorize('get', $moniker, $args, $ctx); | |||||||||||||||||||||||||||||||||||||||||
| 116 | 53 | my @missing = grep { !defined $args->{$_} } sort keys %$singular_args; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 53 | ▶ | die client_error "DBIO::GraphQL: query '" . lcfirst($moniker) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | . "' needs every primary key argument, missing: " | ||||||||||||||||||||||||||||||||||||||||||
| 119 | . join(', ', @missing) . "\n" if @missing; | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 52 | my $rs = $ctx->resultset($moniker); | |||||||||||||||||||||||||||||||||||||||||
| 121 | 52 | ▶ | $rs = $rs->search($restriction) if $restriction; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 52 | my $row = $rs->find($args); | |||||||||||||||||||||||||||||||||||||||||
| 123 | 52 | ▶ | return $row ? { $row->get_columns } : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 173 | }; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | # Plural query with filter + pagination + ordering | ||||||||||||||||||||||||||||||||||||||||||
| 129 | $query_fields{ 'all' . ucfirst($moniker) . 's' } = { | ||||||||||||||||||||||||||||||||||||||||||
| 130 | type => $connection_type, | ||||||||||||||||||||||||||||||||||||||||||
| 131 | description => "Fetch a filtered, paginated, ordered list of $moniker rows.", | ||||||||||||||||||||||||||||||||||||||||||
| 132 | args => { | ||||||||||||||||||||||||||||||||||||||||||
| 133 | filter => { type => $filter_type, | ||||||||||||||||||||||||||||||||||||||||||
| 134 | description => "Per-column nested filters (eq/not/gt/gte/lt/lte/in/like/contains/startsWith/endsWith/isNull) plus AND/OR combinators." }, | ||||||||||||||||||||||||||||||||||||||||||
| 135 | page => { type => $shared->{page_input}, | ||||||||||||||||||||||||||||||||||||||||||
| 136 | description => "Offset pagination: skip N rows, take M rows." }, | ||||||||||||||||||||||||||||||||||||||||||
| 137 | cursor => { type => $shared->{cursor_input}, | ||||||||||||||||||||||||||||||||||||||||||
| 138 | description => "Cursor pagination: fetch first N rows after a cursor." }, | ||||||||||||||||||||||||||||||||||||||||||
| 139 | orderBy => { type => $shared->{order_by_input}, | ||||||||||||||||||||||||||||||||||||||||||
| 140 | description => "Order results by a column, ASC or DESC." }, | ||||||||||||||||||||||||||||||||||||||||||
| 141 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 133 | ▶ | resolve => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 133 | my ($root, $args, $ctx, $info) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 133 | ▶ | $args //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args //= {}
MC/DC0%0/1
$args //= {}
$args
Truth table50%1/2
A
A= $args
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 133 | my $restriction = $policy->authorize('list', $moniker, $args, $ctx); | |||||||||||||||||||||||||||||||||||||||||
| 147 | 130 | _check_max_rows($policy, $args); | |||||||||||||||||||||||||||||||||||||||||
| 148 | 129 | my $rs = $ctx->resultset($moniker); | |||||||||||||||||||||||||||||||||||||||||
| 149 | 129 | ▶ | $rs = $rs->search($restriction) if $restriction; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 129 | ▶ | if (my $cond = $filter->to_search($args->{filter}, $moniker)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 13 | $rs = $rs->search($cond); | |||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | # The count runs only when total is selected, on a clone so | ||||||||||||||||||||||||||||||||||||||||||
| 155 | # pagination does not affect it and vice-versa. | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 126 | my $total; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 126 | ▶ | if (_selects_field($info, 'total')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 90 | $total = $rs->search_rs({})->count; | |||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 126 | my ($nodes, $next_cursor, $has_next) = | |||||||||||||||||||||||||||||||||||||||||
| 162 | _apply_pagination($rs, $args, $source_obj, $policy); | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 98 | nodes => $nodes, | |||||||||||||||||||||||||||||||||||||||||
| 166 | total => $total, | ||||||||||||||||||||||||||||||||||||||||||
| 167 | nextCursor => $next_cursor, | ||||||||||||||||||||||||||||||||||||||||||
| 168 | hasNextPage => $has_next, | ||||||||||||||||||||||||||||||||||||||||||
| 169 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 170 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 176 | }; | |||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | # Mutations, only for the sources that opted in | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 176 | ▶ | if ($policy->has_mutations($moniker)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | %mutation_fields = ( | ||||||||||||||||||||||||||||||||||||||||||
| 176 | %mutation_fields, | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 76 | %{ $mutation->fields_for($source_obj, $moniker, $graphql_type) }, | |||||||||||||||||||||||||||||||||||||||||
| 178 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | 79 | $guard->($_) for values %query_fields, values %mutation_fields; | |||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 79 | my $query_type = GraphQL::Type::Object->new( | |||||||||||||||||||||||||||||||||||||||||
| 185 | name => 'Query', | ||||||||||||||||||||||||||||||||||||||||||
| 186 | fields => \%query_fields, | ||||||||||||||||||||||||||||||||||||||||||
| 187 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | 79 | ▶ | my $mutation_type = %mutation_fields | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | ? GraphQL::Type::Object->new( | ||||||||||||||||||||||||||||||||||||||||||
| 191 | name => 'Mutation', | ||||||||||||||||||||||||||||||||||||||||||
| 192 | description => 'Auto-generated create/update/delete mutations', | ||||||||||||||||||||||||||||||||||||||||||
| 193 | fields => \%mutation_fields, | ||||||||||||||||||||||||||||||||||||||||||
| 194 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 195 | : undef; | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | 79 | ▶ | my $schema = GraphQL::Schema->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | query => $query_type, | ||||||||||||||||||||||||||||||||||||||||||
| 199 | ($mutation_type ? (mutation => $mutation_type) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 200 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | 79 | my $result = { | |||||||||||||||||||||||||||||||||||||||||
| 203 | schema => $schema, | ||||||||||||||||||||||||||||||||||||||||||
| 204 | context => $db, | ||||||||||||||||||||||||||||||||||||||||||
| 205 | introspection => $policy->introspection, | ||||||||||||||||||||||||||||||||||||||||||
| 206 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | # The entry point: parse once, check the document, then run the engine. | ||||||||||||||||||||||||||||||||||||||||||
| 209 | # A check that fails answers with an errors list, before any SQL. | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 237 | ▶ | $result->{execute} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 237 | my ($query, $root, $context, $variables, $operation) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 212 | 237 | ▶ | my $ast = ref $query ? $query : eval { GraphQL::Language::Parser::parse($query) }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 237 | ▶ | if ($ast) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 236 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 215 | 236 | $class->_check_ast({ introspection => $policy->introspection }, $ast); | |||||||||||||||||||||||||||||||||||||||||
| 216 | 235 | DBIO::GraphQL::Limits->check( | |||||||||||||||||||||||||||||||||||||||||
| 217 | ast => $ast, | ||||||||||||||||||||||||||||||||||||||||||
| 218 | schema => $schema, | ||||||||||||||||||||||||||||||||||||||||||
| 219 | policy => $policy, | ||||||||||||||||||||||||||||||||||||||||||
| 220 | variables => $variables, | ||||||||||||||||||||||||||||||||||||||||||
| 221 | operation => $operation, | ||||||||||||||||||||||||||||||||||||||||||
| 222 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 223 | 212 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 224 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 236 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 24 | (my $message = $@) =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 227 | 24 | return { errors => [ { message => $message } ] }; | |||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||
| 230 | 213 | ▶ | return GraphQL::Execution::execute( | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$ast // $query
MC/DC0%0/2
$ast // $query
$ast
$query
Truth table66%2/3
A // B
A= $astB= $query
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | $schema, $ast // $query, $root, $context, $variables, $operation); | ||||||||||||||||||||||||||||||||||||||||||
| 232 | 79 | }; | |||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 79 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | 7 | ▶ | sub check_document { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check_document: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 7 | my ($class, $result, $query) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 240 | 7 | ▶ | return if $result->{introspection}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 6 | ▶ | my $ast = eval { GraphQL::Language::Parser::parse($query) } or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | 6 | $class->_check_ast($result, $ast); | |||||||||||||||||||||||||||||||||||||||||
| 243 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | 242 | ▶ | sub _check_ast { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_ast: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | 242 | my ($class, $result, $ast) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 248 | 242 | ▶ | return if $result->{introspection}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 8 | ▶ | die "DBIO::GraphQL: introspection is disabled\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | if _selects_introspection($ast); | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | # _selects_field($info, $name) | ||||||||||||||||||||||||||||||||||||||||||
| 255 | # | ||||||||||||||||||||||||||||||||||||||||||
| 256 | # True when the selection set of the field being resolved names $name, | ||||||||||||||||||||||||||||||||||||||||||
| 257 | # directly, under an alias, or through a fragment or inline fragment. | ||||||||||||||||||||||||||||||||||||||||||
| 258 | # Without resolve info (nothing known) the answer is true. | ||||||||||||||||||||||||||||||||||||||||||
| 259 | 126 | ▶ | sub _selects_field { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_selects_field: called CC=6 SCAR=19.2
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | 126 | my ($info, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 261 | 126 | ▶ | return 1 unless $info && $info->{field_nodes}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$info and $info->{"field_nodes"}
MC/DC0%0/2
$info and $info->{"field_nodes"}
$info
$info->{"field_nodes"}
Truth table33%1/3
A and B
A= $infoB= $info->{"field_nodes"}
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 126 | my %seen; | |||||||||||||||||||||||||||||||||||||||||
| 263 | 128 | ▶ | my $walk; $walk = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=15 SCAR=30.9
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 128 | for my $node (@{ $_[0] }) { | |||||||||||||||||||||||||||||||||||||||||
| 265 | 156 | ▶ | my $kind = $node->{kind} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$node->{"kind"} // ""
MC/DC0%0/1
$node->{"kind"} // ""
$node->{"kind"}
Truth table50%1/2
A
A= $node->{"kind"}
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 156 | ▶ | if ($kind eq 'field') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | 154 | ▶ | return 1 if ($node->{name} // '') eq $name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$node->{"name"} // ""
MC/DC0%0/1
$node->{"name"} // ""
$node->{"name"}
Truth table50%1/2
A
A= $node->{"name"}
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | elsif ($kind eq 'fragment_spread') { | ||||||||||||||||||||||||||||||||||||||||||
| 270 | 1 | ▶ | my $frag = $info->{fragments} && $info->{fragments}{ $node->{name} }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$info->{'fragments'} && $info->{'fragments'}{$node->{'name'}}
MC/DC0%0/2
$info->{'fragments'} && $info->{'fragments'}{$node->{'name'}}
$info->{'fragments'}
$info->{'fragments'}{$node->{'name'}}
Truth table33%1/3
A && B
A= $info->{'fragments'}B= $info->{'fragments'}{$node->{'name'}}
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | 1 | ▶ | next if !$frag || $seen{ $node->{name} }++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $frag or $seen{$node->{"name"}}++
MC/DC0%0/2
not $frag or $seen{$node->{"name"}}++
not $frag
$seen{$node->{"name"}}++
Truth table33%1/3
not(A) or B
A= not $fragB= $seen{$node->{"name"}}++
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 1 | ▶ | return 1 if $walk->($frag->{selections} || []); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$frag->{'selections'} || []
MC/DC0%0/1
$frag->{'selections'} || []
$frag->{'selections'}
Truth table50%1/2
A
A= $frag->{'selections'}
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||
| 274 | elsif ($kind eq 'inline_fragment') { | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 1 | ▶ | return 1 if $walk->($node->{selections} || []); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$node->{'selections'} || []
MC/DC0%0/1
$node->{'selections'} || []
$node->{'selections'}
Truth table50%1/2
A
A= $node->{'selections'}
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 36 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 279 | 126 | }; | |||||||||||||||||||||||||||||||||||||||||
| 280 | 126 | my $found = 0; | |||||||||||||||||||||||||||||||||||||||||
| 281 | 126 | for my $field (@{ $info->{field_nodes} }) { | |||||||||||||||||||||||||||||||||||||||||
| 282 | 126 | ▶ | $found = $walk->($field->{selections} || []) and last; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$field->{'selections'} || []
MC/DC0%0/1
$field->{'selections'} || []
$field->{'selections'}
Truth table50%1/2
A
A= $field->{'selections'}
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||
| 284 | 126 | undef $walk; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 126 | return $found; | |||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | 20 | ▶ | sub _selects_introspection { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_selects_introspection: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 20 | my ($nodes) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 290 | 20 | for my $node (@$nodes) { | |||||||||||||||||||||||||||||||||||||||||
| 291 | return 1 if ($node->{kind} // '') eq 'field' | ||||||||||||||||||||||||||||||||||||||||||
| 292 | 21 | ▶ | && ($node->{name} // '') =~ /\A__(?:schema|type)\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition71%5/7
($node->{"kind"} // "") eq "field" and ($node->{"name"} // "") =~ /\A__(?:schema|type)\z/
$node->{"kind"} // ""
$node->{"name"} // ""
MC/DC0%0/2
($node->{"kind"} // "") eq "field" and ($node->{"name"} // "") =~ /\A__(?:schema|type)\z/
$node->{"kind"}
$node->{"name"}
Truth table33%1/3
A and B
A= $node->{"kind"}B= $node->{"name"}
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | 16 | ▶ | return 1 if $node->{selections} && _selects_introspection($node->{selections}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$node->{"selections"} and _selects_introspection($node->{"selections"})
MC/DC100%2/2
$node->{"selections"} and _selects_introspection($node->{"selections"})
$node->{"selections"}
_selects_introspection($node->{"selections"})
Truth table100%3/3
A and B
A= $node->{"selections"}B= _selects_introspection($node->{"selections"})
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||
| 295 | 10 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | # | ||||||||||||||||||||||||||||||||||||||||||
| 299 | # INTERNAL HELPERS | ||||||||||||||||||||||||||||||||||||||||||
| 300 | # | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | 176 | ▶ | sub _build_singular_args { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_singular_args: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | 176 | my ($source) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 304 | 176 | my %args; | |||||||||||||||||||||||||||||||||||||||||
| 305 | 176 | for my $col ($source->primary_columns) { | |||||||||||||||||||||||||||||||||||||||||
| 306 | 180 | $args{$col} = { type => DBIO::GraphQL::ScalarMap::for_column($source, $col) }; | |||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||
| 308 | 176 | return \%args; | |||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | 79 | ▶ | sub _build_shared_types { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_shared_types: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 79 | my $order_direction = GraphQL::Type::Enum->new( | |||||||||||||||||||||||||||||||||||||||||
| 313 | name => 'OrderDirection', | ||||||||||||||||||||||||||||||||||||||||||
| 314 | values => { | ||||||||||||||||||||||||||||||||||||||||||
| 315 | ASC => { value => 'ASC' }, | ||||||||||||||||||||||||||||||||||||||||||
| 316 | DESC => { value => 'DESC' }, | ||||||||||||||||||||||||||||||||||||||||||
| 317 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 318 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | 79 | my $order_by_input = GraphQL::Type::InputObject->new( | |||||||||||||||||||||||||||||||||||||||||
| 321 | name => 'OrderByInput', | ||||||||||||||||||||||||||||||||||||||||||
| 322 | fields => { | ||||||||||||||||||||||||||||||||||||||||||
| 323 | field => { type => GraphQL::Type::NonNull->new(of => $String) }, | ||||||||||||||||||||||||||||||||||||||||||
| 324 | direction => { type => $order_direction }, | ||||||||||||||||||||||||||||||||||||||||||
| 325 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 326 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | 79 | my $page_input = GraphQL::Type::InputObject->new( | |||||||||||||||||||||||||||||||||||||||||
| 329 | name => 'PageInput', | ||||||||||||||||||||||||||||||||||||||||||
| 330 | fields => { | ||||||||||||||||||||||||||||||||||||||||||
| 331 | skip => { type => $Int }, | ||||||||||||||||||||||||||||||||||||||||||
| 332 | take => { type => $Int }, | ||||||||||||||||||||||||||||||||||||||||||
| 333 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 334 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | 79 | my $cursor_input = GraphQL::Type::InputObject->new( | |||||||||||||||||||||||||||||||||||||||||
| 337 | name => 'CursorInput', | ||||||||||||||||||||||||||||||||||||||||||
| 338 | fields => { | ||||||||||||||||||||||||||||||||||||||||||
| 339 | after => { type => $String }, | ||||||||||||||||||||||||||||||||||||||||||
| 340 | first => { type => $Int }, | ||||||||||||||||||||||||||||||||||||||||||
| 341 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 342 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 345 | 79 | order_direction => $order_direction, | |||||||||||||||||||||||||||||||||||||||||
| 346 | order_by_input => $order_by_input, | ||||||||||||||||||||||||||||||||||||||||||
| 347 | page_input => $page_input, | ||||||||||||||||||||||||||||||||||||||||||
| 348 | cursor_input => $cursor_input, | ||||||||||||||||||||||||||||||||||||||||||
| 349 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | # _build_connection_type($moniker, $gql_type) | ||||||||||||||||||||||||||||||||||||||||||
| 353 | # | ||||||||||||||||||||||||||||||||||||||||||
| 354 | # type BookConnection { | ||||||||||||||||||||||||||||||||||||||||||
| 355 | # nodes: [Book] | ||||||||||||||||||||||||||||||||||||||||||
| 356 | # total: Int | ||||||||||||||||||||||||||||||||||||||||||
| 357 | # nextCursor: String | ||||||||||||||||||||||||||||||||||||||||||
| 358 | # hasNextPage: Boolean | ||||||||||||||||||||||||||||||||||||||||||
| 359 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 360 | 176 | ▶ | sub _build_connection_type { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_connection_type: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 361 | 176 | my ($moniker, $gql_type) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||
| 363 | 176 | return GraphQL::Type::Object->new( | |||||||||||||||||||||||||||||||||||||||||
| 364 | name => "${moniker}Connection", | ||||||||||||||||||||||||||||||||||||||||||
| 365 | fields => { | ||||||||||||||||||||||||||||||||||||||||||
| 366 | nodes => { type => GraphQL::Type::List->new(of => $gql_type) }, | ||||||||||||||||||||||||||||||||||||||||||
| 367 | total => { type => $Int }, | ||||||||||||||||||||||||||||||||||||||||||
| 368 | nextCursor => { type => $String }, | ||||||||||||||||||||||||||||||||||||||||||
| 369 | hasNextPage => { type => $Boolean }, | ||||||||||||||||||||||||||||||||||||||||||
| 370 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 371 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | # _encode_cursor / _decode_cursor | ||||||||||||||||||||||||||||||||||||||||||
| 375 | # | ||||||||||||||||||||||||||||||||||||||||||
| 376 | # Cursors are base64(val1:val2:...), stateless, no extra dependencies. | ||||||||||||||||||||||||||||||||||||||||||
| 377 | # Colons inside values are percent-encoded so the separator is unambiguous. | ||||||||||||||||||||||||||||||||||||||||||
| 378 | 34 | ▶ | sub _encode_cursor { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encode_cursor: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | 34 | my (@vals) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 380 | 34 | my $raw = join ':', map { (my $v = $_) =~ s/%/%25/g; $v =~ s/:/%3A/g; $v } @vals; | |||||||||||||||||||||||||||||||||||||||||
| 381 | 34 | return encode_base64($raw, ''); # no newline | |||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | 42 | ▶ | sub _decode_cursor { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decode_cursor: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 385 | 42 | my ($cursor) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | # decode_base64 alone is lenient (it skips illegal characters and | ||||||||||||||||||||||||||||||||||||||||||
| 388 | # tolerates bad padding), so an arbitrary string could decode to | ||||||||||||||||||||||||||||||||||||||||||
| 389 | # something that merely looks like a key. Only canonical base64 is a | ||||||||||||||||||||||||||||||||||||||||||
| 390 | # cursor: the alphabet and length must fit, and re-encoding must give | ||||||||||||||||||||||||||||||||||||||||||
| 391 | # the input back byte for byte. | ||||||||||||||||||||||||||||||||||||||||||
| 392 | 42 | ▶ | die client_error "DBIO::GraphQL: invalid cursor\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$cursor =~ m[\A[A-Za-z0-9+/]*={0,2}\z] and length($cursor) % 4 == 0
MC/DC100%2/2
$cursor =~ m[\A[A-Za-z0-9+/]*={0,2}\z] and length($cursor) % 4 == 0
$cursor =~ m[\A[A-Za-z0-9+/]*={0,2}\z]
length($cursor) % 4 == 0
Truth table100%3/3
A and B
A= $cursor =~ m[\A[A-Za-z0-9+/]*={0,2}\z]B= length($cursor) % 4 == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 393 | unless $cursor =~ m{\A[A-Za-z0-9+/]*={0,2}\z} | ||||||||||||||||||||||||||||||||||||||||||
| 394 | && length($cursor) % 4 == 0; | ||||||||||||||||||||||||||||||||||||||||||
| 395 | 37 | my $raw = decode_base64($cursor); | |||||||||||||||||||||||||||||||||||||||||
| 396 | 37 | ▶ | die client_error "DBIO::GraphQL: invalid cursor\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 397 | unless encode_base64($raw, '') eq $cursor; | ||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | # The empty raw string is one empty component (a single-column key | ||||||||||||||||||||||||||||||||||||||||||
| 400 | # whose value is ''), not zero components; split would drop it. | ||||||||||||||||||||||||||||||||||||||||||
| 401 | 37 | ▶ | return ('') if $raw eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||
| 403 | # Limit -1 keeps empty trailing components ('x:' is ('x', '')). | ||||||||||||||||||||||||||||||||||||||||||
| 404 | 35 | return map { (my $v = $_) =~ s/%3A/:/gi; $v =~ s/%25/%/g; $v } | |||||||||||||||||||||||||||||||||||||||||
| 405 | split /:/, $raw, -1; | ||||||||||||||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | # _apply_pagination($rs, $args, $source) | ||||||||||||||||||||||||||||||||||||||||||
| 409 | # | ||||||||||||||||||||||||||||||||||||||||||
| 410 | # Applies ordering, then either offset (page) or cursor (cursor) pagination. | ||||||||||||||||||||||||||||||||||||||||||
| 411 | # Returns ($nodes, $next_cursor, $has_next). | ||||||||||||||||||||||||||||||||||||||||||
| 412 | # | ||||||||||||||||||||||||||||||||||||||||||
| 413 | # Precedence: cursor pagination wins if both are supplied. | ||||||||||||||||||||||||||||||||||||||||||
| 414 | 126 | ▶ | sub _apply_pagination { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_apply_pagination: called CC=29 SCAR=33.7
| |||||||||||||||||||||||||||||||||||||||||||
| 415 | 126 | my ($rs, $args, $source, $policy) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||
| 417 | 126 | my @pk_cols = $source->primary_columns; | |||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | # Default: stable order by PK | ||||||||||||||||||||||||||||||||||||||||||
| 420 | 126 | my $order = [ map { { -asc => $_ } } @pk_cols ]; | |||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | 126 | my $order_by = $args->{orderBy}; | |||||||||||||||||||||||||||||||||||||||||
| 423 | 126 | ▶ | if ($order_by && $order_by->{field}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$order_by and $order_by->{"field"}
MC/DC50%1/2
$order_by and $order_by->{"field"}
$order_by
$order_by->{"field"}
Truth table66%2/3
A and B
A= $order_byB= $order_by->{"field"}
| |||||||||||||||||||||||||||||||||||||||||||
| 424 | 22 | my $field = $order_by->{field}; | |||||||||||||||||||||||||||||||||||||||||
| 425 | 22 | ▶ | unless ($source->has_column($field) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$source->has_column($field) and not $policy->hidden($source->source_name, $field)
MC/DC100%2/2
$source->has_column($field) and not $policy->hidden($source->source_name, $field)
$source->has_column($field)
not $policy->hidden($source->source_name, $field)
Truth table100%3/3
A and not(B)
A= $source->has_column($field)B= not $policy->hidden($source->source_name, $field)
| |||||||||||||||||||||||||||||||||||||||||||
| 426 | && !$policy->hidden($source->source_name, $field)) { | ||||||||||||||||||||||||||||||||||||||||||
| 427 | 5 | die client_error "DBIO::GraphQL: unknown order column '$field' on source '" | |||||||||||||||||||||||||||||||||||||||||
| 428 | . $source->source_name . "'\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||||||||||||||||
| 430 | 17 | ▶ | my $dir = lc($order_by->{direction} // 'ASC'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$order_by->{"direction"} // "ASC"
MC/DC100%1/1
$order_by->{"direction"} // "ASC"
$order_by->{"direction"}
Truth table100%2/2
A
A= $order_by->{"direction"}
| |||||||||||||||||||||||||||||||||||||||||||
| 431 | 17 | ▶ | if ($args->{cursor}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 432 | # A cursor names a position in ascending primary-key order and in no | ||||||||||||||||||||||||||||||||||||||||||
| 433 | # other: under any other order nextCursor does not lead to the next | ||||||||||||||||||||||||||||||||||||||||||
| 434 | # page. An orderBy that asks for that order leaves the default in | ||||||||||||||||||||||||||||||||||||||||||
| 435 | # place; everything else is refused. | ||||||||||||||||||||||||||||||||||||||||||
| 436 | 13 | ▶ | unless (@pk_cols && $field eq $pk_cols[0] && $dir eq 'asc') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
@pk_cols and $field eq $pk_cols[0] and $dir eq "asc"
@pk_cols and $field eq $pk_cols[0]
MC/DC66%2/3
@pk_cols and $field eq $pk_cols[0] and $dir eq "asc"
@pk_cols
$field eq $pk_cols[0]
$dir eq "asc"
Truth table75%3/4
A and B and C
A= @pk_colsB= $field eq $pk_cols[0]C= $dir eq "asc"
| |||||||||||||||||||||||||||||||||||||||||||
| 437 | 6 | die client_error "DBIO::GraphQL: cursor pagination on source '" | |||||||||||||||||||||||||||||||||||||||||
| 438 | . $source->source_name . "' runs in ascending primary-key order" | ||||||||||||||||||||||||||||||||||||||||||
| 439 | . " only, not orderBy '$field' " . uc($dir) | ||||||||||||||||||||||||||||||||||||||||||
| 440 | . "; use page for other orderings\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||||||||||
| 443 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 444 | 4 | $order = { "-$dir" => $field }; | |||||||||||||||||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||||||||||||
| 447 | 115 | $rs = $rs->search(undef, { order_by => $order }); | |||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | 115 | ▶ | if (my $cursor_args = $args->{cursor}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 450 | 56 | ▶ | my $first = $cursor_args->{first} // 10; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$cursor_args->{"first"} // 10
MC/DC100%1/1
$cursor_args->{"first"} // 10
$cursor_args->{"first"}
Truth table100%2/2
A
A= $cursor_args->{"first"}
| |||||||||||||||||||||||||||||||||||||||||||
| 451 | 56 | my $after = $cursor_args->{after}; | |||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | 56 | ▶ | die client_error "DBIO::GraphQL: cursor pagination requires a primary key, but source '" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 454 | . $source->source_name . "' has none; use page pagination\n" | ||||||||||||||||||||||||||||||||||||||||||
| 455 | unless @pk_cols; | ||||||||||||||||||||||||||||||||||||||||||
| 456 | 55 | ▶ | die client_error "DBIO::GraphQL: cursor 'first' must be >= 1 (got $first)\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 457 | if $first < 1; | ||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | 53 | ▶ | if (defined $after) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 460 | 40 | my @decoded = _decode_cursor($after); | |||||||||||||||||||||||||||||||||||||||||
| 461 | 35 | ▶ | unless (@decoded == @pk_cols) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 462 | 4 | die client_error "DBIO::GraphQL: cursor does not match the primary key of source '" | |||||||||||||||||||||||||||||||||||||||||
| 463 | . $source->source_name . "' (" . scalar(@pk_cols) | ||||||||||||||||||||||||||||||||||||||||||
| 464 | . " column(s), cursor carries " . scalar(@decoded) . " value(s))\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||||||||||||||||
| 466 | # Row-wise "key > cursor": one branch per column, with every column | ||||||||||||||||||||||||||||||||||||||||||
| 467 | # before it equal and that column greater. | ||||||||||||||||||||||||||||||||||||||||||
| 468 | $rs = $rs->search({ -or => [ | ||||||||||||||||||||||||||||||||||||||||||
| 469 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 470 | 31 | my $i = $_; | |||||||||||||||||||||||||||||||||||||||||
| 471 | +{ | ||||||||||||||||||||||||||||||||||||||||||
| 472 | 65 | (map { $pk_cols[$_] => $decoded[$_] } 0 .. $i - 1), | |||||||||||||||||||||||||||||||||||||||||
| 473 | $pk_cols[$i] => { '>' => $decoded[$i] }, | ||||||||||||||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||||||||||||
| 475 | } 0 .. $#pk_cols | ||||||||||||||||||||||||||||||||||||||||||
| 476 | ] }); | ||||||||||||||||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | 44 | my @rows = $rs->search(undef, { rows => $first + 1 })->all; | |||||||||||||||||||||||||||||||||||||||||
| 480 | 44 | my $has_next = @rows > $first; | |||||||||||||||||||||||||||||||||||||||||
| 481 | 44 | ▶ | @rows = @rows[0 .. $first - 1] if $has_next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||
| 483 | 44 | my $next_cursor; | |||||||||||||||||||||||||||||||||||||||||
| 484 | 44 | ▶ | if ($has_next && @rows) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$has_next and @rows
MC/DC50%1/2
$has_next and @rows
$has_next
@rows
Truth table66%2/3
A and B
A= $has_nextB= @rows
| |||||||||||||||||||||||||||||||||||||||||||
| 485 | 32 | my $last_row = $rows[-1]; | |||||||||||||||||||||||||||||||||||||||||
| 486 | 32 | $next_cursor = _encode_cursor(map { $last_row->get_column($_) } @pk_cols); | |||||||||||||||||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 490 | 44 | ▶ | [ map { { $_->get_columns } } @rows ], | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 491 | $next_cursor, | ||||||||||||||||||||||||||||||||||||||||||
| 492 | $has_next ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||
| 493 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | 59 | ▶ | if (my $page_args = $args->{page}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 497 | 25 | ▶ | my $skip = $page_args->{skip} // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$page_args->{"skip"} // 0
MC/DC100%1/1
$page_args->{"skip"} // 0
$page_args->{"skip"}
Truth table100%2/2
A
A= $page_args->{"skip"}
| |||||||||||||||||||||||||||||||||||||||||||
| 498 | 25 | ▶ | my $take = $page_args->{take} // 10; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$page_args->{"take"} // 10
MC/DC100%1/1
$page_args->{"take"} // 10
$page_args->{"take"}
Truth table100%2/2
A
A= $page_args->{"take"}
| |||||||||||||||||||||||||||||||||||||||||||
| 499 | 25 | ▶ | die client_error "DBIO::GraphQL: page 'take' must be >= 1 (got $take)\n" if $take < 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 500 | 22 | ▶ | die client_error "DBIO::GraphQL: page 'skip' must be >= 0 (got $skip)\n" if $skip < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 501 | 21 | my @rows = $rs->search(undef, { rows => $take + 1, offset => $skip })->all; | |||||||||||||||||||||||||||||||||||||||||
| 502 | 21 | my $has_next = @rows > $take; | |||||||||||||||||||||||||||||||||||||||||
| 503 | 21 | ▶ | @rows = @rows[0 .. $take - 1] if $has_next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 504 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 505 | 21 | ▶ | [ map { { $_->get_columns } } @rows ], | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 506 | undef, | ||||||||||||||||||||||||||||||||||||||||||
| 507 | $has_next ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||
| 508 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | # No page and no cursor: default_rows, one more to learn whether rows remain. | ||||||||||||||||||||||||||||||||||||||||||
| 512 | 34 | my $limit = $policy->default_rows; | |||||||||||||||||||||||||||||||||||||||||
| 513 | 34 | my @rows = $rs->search(undef, { rows => $limit + 1 })->all; | |||||||||||||||||||||||||||||||||||||||||
| 514 | 33 | my $has_next = @rows > $limit; | |||||||||||||||||||||||||||||||||||||||||
| 515 | 33 | ▶ | @rows = @rows[0 .. $limit - 1] if $has_next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 516 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 517 | 33 | ▶ | [ map { { $_->get_columns } } @rows ], | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 518 | undef, | ||||||||||||||||||||||||||||||||||||||||||
| 519 | $has_next ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||
| 520 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 521 | } | ||||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||
| 523 | # take, first and skip are bounded by max_rows; an excess is an error, checked | ||||||||||||||||||||||||||||||||||||||||||
| 524 | # before any SQL runs. | ||||||||||||||||||||||||||||||||||||||||||
| 525 | 130 | ▶ | sub _check_max_rows { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_max_rows: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 526 | 130 | my ($policy, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 527 | my %given = ( | ||||||||||||||||||||||||||||||||||||||||||
| 528 | take => ($args->{page} || {})->{take}, | ||||||||||||||||||||||||||||||||||||||||||
| 529 | skip => ($args->{page} || {})->{skip}, | ||||||||||||||||||||||||||||||||||||||||||
| 530 | first => ($args->{cursor} || {})->{first}, | ||||||||||||||||||||||||||||||||||||||||||
| 531 | 130 | ▶ | ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
$args->{'page'} || {}
$args->{'page'} || {}
$args->{'cursor'} || {}
MC/DC100%1/1
$args->{'page'} || {}
$args->{'page'}
MC/DC100%1/1
$args->{'page'} || {}
$args->{'page'}
MC/DC100%1/1
$args->{'cursor'} || {}
$args->{'cursor'}
Truth table100%2/2
A
A= $args->{'page'}
Truth table100%2/2
A
A= $args->{'page'}
Truth table100%2/2
A
A= $args->{'cursor'}
| |||||||||||||||||||||||||||||||||||||||||||
| 532 | 130 | for my $what (sort keys %given) { | |||||||||||||||||||||||||||||||||||||||||
| 533 | 389 | my $value = $given{$what}; | |||||||||||||||||||||||||||||||||||||||||
| 534 | 389 | ▶ | die client_error "DBIO::GraphQL: $what $value exceeds max_rows (" . $policy->max_rows . ")\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and $value > $policy->max_rows
MC/DC100%2/2
defined $value and $value > $policy->max_rows
defined $value
$value > $policy->max_rows
Truth table100%3/3
A and B
A= defined $valueB= $value > $policy->max_rows
| |||||||||||||||||||||||||||||||||||||||||||
| 535 | if defined $value && $value > $policy->max_rows; | ||||||||||||||||||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||
| 540 | 14 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||
| 542 | __END__ | ||||||||||||||||||||||||||||||||||||||||||