| 1 | package DBIO::GraphQL; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Auto-generate a GraphQL schema from a DBIO schema | ||||||||||||||||||||||||||||
| 3 | 7 | our $VERSION = '0.900001'; | |||||||||||||||||||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use MIME::Base64 qw(encode_base64 decode_base64); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use GraphQL::Schema; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use GraphQL::Type::Object; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use GraphQL::Type::List; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use GraphQL::Type::Enum; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 7 | ▶ | use GraphQL::Type::Scalar qw($Int $String $Boolean); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 7 | ▶ | use DBIO::GraphQL::ScalarMap; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 7 | ▶ | use DBIO::GraphQL::Filter::Search; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 7 | ▶ | use DBIO::GraphQL::Relationship; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 7 | ▶ | use DBIO::GraphQL::Mutation; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 16 | ▶ | sub to_graphql { | ||||||||||||||||||||||||||
|
Subroutine
to_graphql: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 16 | my ($class, $db) = @_; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 16 | my $filter = DBIO::GraphQL::Filter::Search->new(schema => $db); | |||||||||||||||||||||||||||
| 25 | 16 | my $relationship = DBIO::GraphQL::Relationship->new(schema => $db); | |||||||||||||||||||||||||||
| 26 | 16 | my $mutation = DBIO::GraphQL::Mutation->new(schema => $db); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 16 | my $shared = _build_shared_types(); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # Phase 1: shell types (one per source). Empty fields sub; filled | ||||||||||||||||||||||||||||
| 31 | # in Phase 2 once every source has a shell. | ||||||||||||||||||||||||||||
| 32 | 16 | my %gql_types; | |||||||||||||||||||||||||||
| 33 | 16 | for my $moniker ($db->sources) { | |||||||||||||||||||||||||||
| 34 | $gql_types{$moniker} = GraphQL::Type::Object->new( | ||||||||||||||||||||||||||||
| 35 | name => $moniker, | ||||||||||||||||||||||||||||
| 36 | description => "Auto-generated GraphQL type for DBIO source '$moniker'", | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | fields => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 38 | 33 | ); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # Phase 2: real fields. Snapshot of %gql_types at the top so that | ||||||||||||||||||||||||||||
| 42 | # a relationship's target type lookup never accidentally sees a | ||||||||||||||||||||||||||||
| 43 | # half-built object in this same loop iteration. | ||||||||||||||||||||||||||||
| 44 | 16 | for my $moniker ($db->sources) { | |||||||||||||||||||||||||||
| 45 | 33 | my $source_obj = $db->source($moniker); | |||||||||||||||||||||||||||
| 46 | 33 | my %types_snapshot = %gql_types; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 16 | ▶ | $gql_types{$moniker}->{fields} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 49 | 16 | my %fields; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 16 | for my $col ($source_obj->columns) { | |||||||||||||||||||||||||||
| 52 | 78 | $fields{$col} = { | |||||||||||||||||||||||||||
| 53 | type => DBIO::GraphQL::ScalarMap::for_column($source_obj, $col), | ||||||||||||||||||||||||||||
| 54 | }; | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 16 | for my $rel ($source_obj->relationships) { | |||||||||||||||||||||||||||
| 58 | 17 | my $field = $relationship->build_field( | |||||||||||||||||||||||||||
| 59 | $source_obj, $rel, \%types_snapshot | ||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||
| 61 | 17 | ▶ | $fields{$rel} = $field if $field; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 16 | return \%fields; | |||||||||||||||||||||||||||
| 65 | 33 | }; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Phase 3: root Query + Mutation types | ||||||||||||||||||||||||||||
| 69 | 16 | my (%query_fields, %mutation_fields); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 16 | for my $moniker ($db->sources) { | |||||||||||||||||||||||||||
| 72 | 33 | my $graphql_type = $gql_types{$moniker}; | |||||||||||||||||||||||||||
| 73 | 33 | my $source_obj = $db->source($moniker); | |||||||||||||||||||||||||||
| 74 | 33 | my $singular_args = _build_singular_args($source_obj); | |||||||||||||||||||||||||||
| 75 | 33 | my $filter_type = $filter->type_for($moniker); | |||||||||||||||||||||||||||
| 76 | 33 | my $connection_type = _build_connection_type($moniker, $graphql_type); | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # Singular query | ||||||||||||||||||||||||||||
| 79 | $query_fields{ lcfirst($moniker) } = { | ||||||||||||||||||||||||||||
| 80 | type => $graphql_type, | ||||||||||||||||||||||||||||
| 81 | args => $singular_args, | ||||||||||||||||||||||||||||
| 82 | 6 | ▶ | resolve => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 83 | 6 | my ($root, $args, $ctx) = @_; | |||||||||||||||||||||||||||
| 84 | 6 | ▶ | return unless grep { defined $args->{$_} } keys %$singular_args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 6 | my $row = $ctx->resultset($moniker)->find($args); | |||||||||||||||||||||||||||
| 86 | 6 | ▶ | return $row ? { $row->get_columns } : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | }, | ||||||||||||||||||||||||||||
| 88 | 33 | }; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # Plural query with filter + pagination + ordering | ||||||||||||||||||||||||||||
| 91 | $query_fields{ 'all' . ucfirst($moniker) . 's' } = { | ||||||||||||||||||||||||||||
| 92 | type => $connection_type, | ||||||||||||||||||||||||||||
| 93 | description => "Fetch a filtered, paginated, ordered list of $moniker rows.", | ||||||||||||||||||||||||||||
| 94 | args => { | ||||||||||||||||||||||||||||
| 95 | filter => { type => $filter_type, | ||||||||||||||||||||||||||||
| 96 | description => "Per-column nested filters (eq/not/gt/gte/lt/lte/in/like/contains/startsWith/endsWith/isNull) plus AND/OR combinators." }, | ||||||||||||||||||||||||||||
| 97 | page => { type => $shared->{page_input}, | ||||||||||||||||||||||||||||
| 98 | description => "Offset pagination: skip N rows, take M rows." }, | ||||||||||||||||||||||||||||
| 99 | cursor => { type => $shared->{cursor_input}, | ||||||||||||||||||||||||||||
| 100 | description => "Cursor pagination: fetch first N rows after a cursor." }, | ||||||||||||||||||||||||||||
| 101 | orderBy => { type => $shared->{order_by_input}, | ||||||||||||||||||||||||||||
| 102 | description => "Order results by a column, ASC or DESC." }, | ||||||||||||||||||||||||||||
| 103 | }, | ||||||||||||||||||||||||||||
| 104 | 23 | ▶ | resolve => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 105 | 23 | my ($root, $args, $ctx) = @_; | |||||||||||||||||||||||||||
| 106 | 23 | ▶ | $args //= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args //= {}
MC/DC0%0/1
$args //= {}
$args
Truth table50%1/2
A
A= $args
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 23 | my $rs = $ctx->resultset($moniker); | |||||||||||||||||||||||||||
| 109 | 23 | ▶ | if (my $cond = $filter->to_search($args->{filter}, $moniker)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 11 | $rs = $rs->search($cond); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # Count uses a clone so pagination does not affect the | ||||||||||||||||||||||||||||
| 114 | # count query and vice-versa. | ||||||||||||||||||||||||||||
| 115 | 23 | my $count_rs = $rs->search_rs({}); | |||||||||||||||||||||||||||
| 116 | 23 | my $total = $count_rs->count; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 23 | my ($nodes, $next_cursor, $has_next) = | |||||||||||||||||||||||||||
| 119 | _apply_pagination($rs, $args, $source_obj); | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | return { | ||||||||||||||||||||||||||||
| 122 | 20 | nodes => $nodes, | |||||||||||||||||||||||||||
| 123 | total => $total, | ||||||||||||||||||||||||||||
| 124 | nextCursor => $next_cursor, | ||||||||||||||||||||||||||||
| 125 | hasNextPage => $has_next, | ||||||||||||||||||||||||||||
| 126 | }; | ||||||||||||||||||||||||||||
| 127 | }, | ||||||||||||||||||||||||||||
| 128 | 33 | }; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # Mutations | ||||||||||||||||||||||||||||
| 131 | %mutation_fields = ( | ||||||||||||||||||||||||||||
| 132 | %mutation_fields, | ||||||||||||||||||||||||||||
| 133 | 33 | %{ $mutation->fields_for($source_obj, $moniker, $graphql_type) }, | |||||||||||||||||||||||||||
| 134 | ); | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 16 | my $query_type = GraphQL::Type::Object->new( | |||||||||||||||||||||||||||
| 138 | name => 'Query', | ||||||||||||||||||||||||||||
| 139 | fields => \%query_fields, | ||||||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 16 | my $mutation_type = GraphQL::Type::Object->new( | |||||||||||||||||||||||||||
| 143 | name => 'Mutation', | ||||||||||||||||||||||||||||
| 144 | description => 'Auto-generated create/update/delete mutations', | ||||||||||||||||||||||||||||
| 145 | fields => \%mutation_fields, | ||||||||||||||||||||||||||||
| 146 | ); | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 16 | my $schema = GraphQL::Schema->new( | |||||||||||||||||||||||||||
| 149 | query => $query_type, | ||||||||||||||||||||||||||||
| 150 | mutation => $mutation_type, | ||||||||||||||||||||||||||||
| 151 | ); | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | return { | ||||||||||||||||||||||||||||
| 154 | 16 | schema => $schema, | |||||||||||||||||||||||||||
| 155 | context => $db, | ||||||||||||||||||||||||||||
| 156 | }; | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # | ||||||||||||||||||||||||||||
| 160 | # INTERNAL HELPERS | ||||||||||||||||||||||||||||
| 161 | # | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 33 | ▶ | sub _build_singular_args { | ||||||||||||||||||||||||||
|
Subroutine
_build_singular_args: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 164 | 33 | my ($source) = @_; | |||||||||||||||||||||||||||
| 165 | 33 | my %args; | |||||||||||||||||||||||||||
| 166 | 33 | for my $col ($source->primary_columns) { | |||||||||||||||||||||||||||
| 167 | 34 | $args{$col} = { type => DBIO::GraphQL::ScalarMap::for_column($source, $col) }; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 33 | return \%args; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 16 | ▶ | sub _build_shared_types { | ||||||||||||||||||||||||||
|
Subroutine
_build_shared_types: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 173 | 16 | my $order_direction = GraphQL::Type::Enum->new( | |||||||||||||||||||||||||||
| 174 | name => 'OrderDirection', | ||||||||||||||||||||||||||||
| 175 | values => { | ||||||||||||||||||||||||||||
| 176 | ASC => { value => 'ASC' }, | ||||||||||||||||||||||||||||
| 177 | DESC => { value => 'DESC' }, | ||||||||||||||||||||||||||||
| 178 | }, | ||||||||||||||||||||||||||||
| 179 | ); | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 16 | my $order_by_input = GraphQL::Type::InputObject->new( | |||||||||||||||||||||||||||
| 182 | name => 'OrderByInput', | ||||||||||||||||||||||||||||
| 183 | fields => { | ||||||||||||||||||||||||||||
| 184 | field => { type => GraphQL::Type::NonNull->new(of => $String) }, | ||||||||||||||||||||||||||||
| 185 | direction => { type => $order_direction }, | ||||||||||||||||||||||||||||
| 186 | }, | ||||||||||||||||||||||||||||
| 187 | ); | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 16 | my $page_input = GraphQL::Type::InputObject->new( | |||||||||||||||||||||||||||
| 190 | name => 'PageInput', | ||||||||||||||||||||||||||||
| 191 | fields => { | ||||||||||||||||||||||||||||
| 192 | skip => { type => $Int }, | ||||||||||||||||||||||||||||
| 193 | take => { type => $Int }, | ||||||||||||||||||||||||||||
| 194 | }, | ||||||||||||||||||||||||||||
| 195 | ); | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 16 | my $cursor_input = GraphQL::Type::InputObject->new( | |||||||||||||||||||||||||||
| 198 | name => 'CursorInput', | ||||||||||||||||||||||||||||
| 199 | fields => { | ||||||||||||||||||||||||||||
| 200 | after => { type => $String }, | ||||||||||||||||||||||||||||
| 201 | first => { type => $Int }, | ||||||||||||||||||||||||||||
| 202 | }, | ||||||||||||||||||||||||||||
| 203 | ); | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | return { | ||||||||||||||||||||||||||||
| 206 | 16 | order_direction => $order_direction, | |||||||||||||||||||||||||||
| 207 | order_by_input => $order_by_input, | ||||||||||||||||||||||||||||
| 208 | page_input => $page_input, | ||||||||||||||||||||||||||||
| 209 | cursor_input => $cursor_input, | ||||||||||||||||||||||||||||
| 210 | }; | ||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | # _build_connection_type($moniker, $gql_type) | ||||||||||||||||||||||||||||
| 214 | # | ||||||||||||||||||||||||||||
| 215 | # type BookConnection { | ||||||||||||||||||||||||||||
| 216 | # nodes: [Book] | ||||||||||||||||||||||||||||
| 217 | # total: Int | ||||||||||||||||||||||||||||
| 218 | # nextCursor: String | ||||||||||||||||||||||||||||
| 219 | # hasNextPage: Boolean | ||||||||||||||||||||||||||||
| 220 | # } | ||||||||||||||||||||||||||||
| 221 | 33 | ▶ | sub _build_connection_type { | ||||||||||||||||||||||||||
|
Subroutine
_build_connection_type: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 222 | 33 | my ($moniker, $gql_type) = @_; | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 33 | return GraphQL::Type::Object->new( | |||||||||||||||||||||||||||
| 225 | name => "${moniker}Connection", | ||||||||||||||||||||||||||||
| 226 | fields => { | ||||||||||||||||||||||||||||
| 227 | nodes => { type => GraphQL::Type::List->new(of => $gql_type) }, | ||||||||||||||||||||||||||||
| 228 | total => { type => $Int }, | ||||||||||||||||||||||||||||
| 229 | nextCursor => { type => $String }, | ||||||||||||||||||||||||||||
| 230 | hasNextPage => { type => $Boolean }, | ||||||||||||||||||||||||||||
| 231 | }, | ||||||||||||||||||||||||||||
| 232 | ); | ||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | # _encode_cursor / _decode_cursor | ||||||||||||||||||||||||||||
| 236 | # | ||||||||||||||||||||||||||||
| 237 | # Cursors are base64(val1:val2:...), stateless, no extra dependencies. | ||||||||||||||||||||||||||||
| 238 | # Colons inside values are percent-encoded so the separator is unambiguous. | ||||||||||||||||||||||||||||
| 239 | 2 | ▶ | sub _encode_cursor { | ||||||||||||||||||||||||||
|
Subroutine
_encode_cursor: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 240 | 2 | my (@vals) = @_; | |||||||||||||||||||||||||||
| 241 | 2 | my $raw = join ':', map { (my $v = $_) =~ s/%/%25/g; $v =~ s/:/%3A/g; $v } @vals; | |||||||||||||||||||||||||||
| 242 | 2 | return encode_base64($raw, ''); # no newline | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 2 | ▶ | sub _decode_cursor { | ||||||||||||||||||||||||||
|
Subroutine
_decode_cursor: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 246 | 2 | my ($cursor) = @_; | |||||||||||||||||||||||||||
| 247 | 2 | my $raw = decode_base64($cursor); | |||||||||||||||||||||||||||
| 248 | 2 | my @vals = map { (my $v = $_) =~ s/%3A/:/gi; $v =~ s/%25/%/g; $v } | |||||||||||||||||||||||||||
| 249 | split /:/, $raw; | ||||||||||||||||||||||||||||
| 250 | 2 | return @vals; | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | # _apply_pagination($rs, $args, $source) | ||||||||||||||||||||||||||||
| 254 | # | ||||||||||||||||||||||||||||
| 255 | # Applies ordering, then either offset (page) or cursor (cursor) pagination. | ||||||||||||||||||||||||||||
| 256 | # Returns ($nodes, $next_cursor, $has_next). | ||||||||||||||||||||||||||||
| 257 | # | ||||||||||||||||||||||||||||
| 258 | # Precedence: cursor pagination wins if both are supplied. | ||||||||||||||||||||||||||||
| 259 | 23 | ▶ | sub _apply_pagination { | ||||||||||||||||||||||||||
|
Subroutine
_apply_pagination: called CC=16 SCAR=28.1
| |||||||||||||||||||||||||||||
| 260 | 23 | my ($rs, $args, $source) = @_; | |||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 23 | my @pk_cols = $source->primary_columns; | |||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 23 | my $order_by = $args->{orderBy}; | |||||||||||||||||||||||||||
| 265 | 23 | ▶ | 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"}
| |||||||||||||||||||||||||||||
| 266 | 6 | my $field = $order_by->{field}; | |||||||||||||||||||||||||||
| 267 | 6 | ▶ | unless ($source->has_column($field)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | 3 | die "DBIO::GraphQL: unknown order column '$field' on source '" | |||||||||||||||||||||||||||
| 269 | . $source->source_name . "'\n"; | ||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | 3 | ▶ | my $dir = lc($order_by->{direction} // 'ASC'); | ||||||||||||||||||||||||||
|
Condition50%1/2
$order_by->{"direction"} // "ASC"
MC/DC0%0/1
$order_by->{"direction"} // "ASC"
$order_by->{"direction"}
Truth table50%1/2
A
A= $order_by->{"direction"}
| |||||||||||||||||||||||||||||
| 272 | 3 | $rs = $rs->search(undef, { | |||||||||||||||||||||||||||
| 273 | order_by => { "-$dir" => $field } | ||||||||||||||||||||||||||||
| 274 | }); | ||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | else { | ||||||||||||||||||||||||||||
| 277 | # Default: stable order by PK | ||||||||||||||||||||||||||||
| 278 | 17 | $rs = $rs->search(undef, { order_by => [ map { { -asc => $_ } } @pk_cols ] }); | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 20 | ▶ | if (my $cursor_args = $args->{cursor}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 3 | ▶ | my $first = $cursor_args->{first} // 10; | ||||||||||||||||||||||||||
|
Condition50%1/2
$cursor_args->{"first"} // 10
MC/DC0%0/1
$cursor_args->{"first"} // 10
$cursor_args->{"first"}
Truth table50%1/2
A
A= $cursor_args->{"first"}
| |||||||||||||||||||||||||||||
| 283 | 3 | my $after = $cursor_args->{after}; | |||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 3 | ▶ | if ($after) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 2 | my @decoded = _decode_cursor($after); | |||||||||||||||||||||||||||
| 287 | 2 | ▶ | if (@pk_cols == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | 2 | $rs = $rs->search({ $pk_cols[0] => { '>' => $decoded[0] } }); | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | else { | ||||||||||||||||||||||||||||
| 291 | 0 | my %after_cond = map { $pk_cols[$_] => { '>' => $decoded[$_] } } | |||||||||||||||||||||||||||
| 292 | 0 .. $#pk_cols; | ||||||||||||||||||||||||||||
| 293 | 0 | $rs = $rs->search(\%after_cond); | |||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 3 | my @rows = $rs->search(undef, { rows => $first + 1 })->all; | |||||||||||||||||||||||||||
| 298 | 3 | my $has_next = @rows > $first; | |||||||||||||||||||||||||||
| 299 | 3 | ▶ | @rows = @rows[0 .. $first - 1] if $has_next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 3 | my $next_cursor; | |||||||||||||||||||||||||||
| 302 | 3 | ▶ | 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
| |||||||||||||||||||||||||||||
| 303 | 2 | my $last_row = $rows[-1]; | |||||||||||||||||||||||||||
| 304 | 2 | $next_cursor = _encode_cursor(map { $last_row->get_column($_) } @pk_cols); | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | return ( | ||||||||||||||||||||||||||||
| 308 | 3 | ▶ | [ map { { $_->get_columns } } @rows ], | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 309 | $next_cursor, | ||||||||||||||||||||||||||||
| 310 | $has_next ? 1 : 0, | ||||||||||||||||||||||||||||
| 311 | ); | ||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 17 | ▶ | if (my $page_args = $args->{page}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | 3 | ▶ | my $skip = $page_args->{skip} // 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$page_args->{"skip"} // 0
MC/DC0%0/1
$page_args->{"skip"} // 0
$page_args->{"skip"}
Truth table50%1/2
A
A= $page_args->{"skip"}
| |||||||||||||||||||||||||||||
| 316 | 3 | ▶ | my $take = $page_args->{take} // 10; | ||||||||||||||||||||||||||
|
Condition50%1/2
$page_args->{"take"} // 10
MC/DC0%0/1
$page_args->{"take"} // 10
$page_args->{"take"}
Truth table50%1/2
A
A= $page_args->{"take"}
| |||||||||||||||||||||||||||||
| 317 | 3 | my @rows = $rs->search(undef, { rows => $take, offset => $skip })->all; | |||||||||||||||||||||||||||
| 318 | return ( | ||||||||||||||||||||||||||||
| 319 | 3 | [ map { { $_->get_columns } } @rows ], | |||||||||||||||||||||||||||
| 320 | undef, | ||||||||||||||||||||||||||||
| 321 | 0, | ||||||||||||||||||||||||||||
| 322 | ); | ||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 14 | my @rows = $rs->all; | |||||||||||||||||||||||||||
| 326 | return ( | ||||||||||||||||||||||||||||
| 327 | 14 | [ map { { $_->get_columns } } @rows ], | |||||||||||||||||||||||||||
| 328 | undef, | ||||||||||||||||||||||||||||
| 329 | 0, | ||||||||||||||||||||||||||||
| 330 | ); | ||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 7 | 1; | |||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | __END__ | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | =pod | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | =head1 NAME | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | DBIO::GraphQL - Auto-generate a GraphQL schema from a DBIO schema | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | =head1 VERSION | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | version 0.900001 | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | use DBIO::GraphQL; | ||||||||||||||||||||||||||||
| 353 | use GraphQL::Execution qw(execute); | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | my $db = My::Schema->connect(...); | ||||||||||||||||||||||||||||
| 356 | my $result = DBIO::GraphQL->to_graphql($db); | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | # Simple plural query - always include at least one scalar field | ||||||||||||||||||||||||||||
| 359 | # alongside nodes (e.g. total) - see KNOWN BEHAVIOUR below. | ||||||||||||||||||||||||||||
| 360 | execute($result->{schema}, | ||||||||||||||||||||||||||||
| 361 | '{ allBooks { total nodes { title } } }', | ||||||||||||||||||||||||||||
| 362 | undef, $result->{context}); | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | # Filtered + paginated + ordered (nested-DBIO-style filter) | ||||||||||||||||||||||||||||
| 365 | execute($result->{schema}, ' | ||||||||||||||||||||||||||||
| 366 | query { | ||||||||||||||||||||||||||||
| 367 | allBooks( | ||||||||||||||||||||||||||||
| 368 | filter: { title: { like: "%Perl%" } } | ||||||||||||||||||||||||||||
| 369 | orderBy: { field: "title", direction: ASC } | ||||||||||||||||||||||||||||
| 370 | page: { skip: 0, take: 5 } | ||||||||||||||||||||||||||||
| 371 | ) { | ||||||||||||||||||||||||||||
| 372 | total | ||||||||||||||||||||||||||||
| 373 | hasNextPage | ||||||||||||||||||||||||||||
| 374 | nodes { id title } | ||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | }', undef, $result->{context}); | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | # Cursor pagination | ||||||||||||||||||||||||||||
| 379 | execute($result->{schema}, ' | ||||||||||||||||||||||||||||
| 380 | query($after: String) { | ||||||||||||||||||||||||||||
| 381 | allBooks(cursor: { after: $after, first: 5 }) { | ||||||||||||||||||||||||||||
| 382 | total | ||||||||||||||||||||||||||||
| 383 | nextCursor | ||||||||||||||||||||||||||||
| 384 | hasNextPage | ||||||||||||||||||||||||||||
| 385 | nodes { id title } | ||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | }', undef, $result->{context}, { after => $cursor }); | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | # Mutation | ||||||||||||||||||||||||||||
| 390 | execute($result->{schema}, | ||||||||||||||||||||||||||||
| 391 | 'mutation { createBook(title: "Dune", author_id: 4) { id title } }', | ||||||||||||||||||||||||||||
| 392 | undef, $result->{context}); | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | Introspects every source registered with the supplied L<DBIO::Schema> | ||||||||||||||||||||||||||||
| 397 | and builds a complete, executable L<GraphQL::Schema> with: | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | =over 4 | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | =item * One scalar field per column, typed as C<Int>, C<Float>, | ||||||||||||||||||||||||||||
| 402 | C<Boolean>, or C<String> based on the column's declared C<data_type>. | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | =item * One relationship field per DBIO relationship (C<has_many> | ||||||||||||||||||||||||||||
| 405 | resolves to a List type; C<belongs_to> / C<might_have> resolve to a | ||||||||||||||||||||||||||||
| 406 | single object type). Build-time errors are emitted when the DBIO | ||||||||||||||||||||||||||||
| 407 | relationship contract is incomplete (see L<DBIO::GraphQL::Relationship>). | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | =item * A root C<Query> type with singular lookup and plural | ||||||||||||||||||||||||||||
| 410 | C<allE<lt>SourceE<gt>s> queries supporting filtering, ordering, and both | ||||||||||||||||||||||||||||
| 411 | offset and cursor pagination. Filter arguments use a nested per-column | ||||||||||||||||||||||||||||
| 412 | shape that mirrors the DBIO search-condition format (see | ||||||||||||||||||||||||||||
| 413 | L<DBIO::GraphQL::Filter>). | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | =item * A root C<Mutation> type with C<createX>, C<updateX>, and | ||||||||||||||||||||||||||||
| 416 | C<deleteX> entry points for every source (see L<DBIO::GraphQL::Mutation>). | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | =back | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | Composite primary keys are fully supported throughout. | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | =head1 METHODS | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | =head2 to_graphql | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | my $result = DBIO::GraphQL->to_graphql($db); | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | Class method. Accepts a connected L<DBIO::Schema> instance. Returns a | ||||||||||||||||||||||||||||
| 429 | hashref: | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | { | ||||||||||||||||||||||||||||
| 432 | schema => $graphql_schema, # GraphQL::Schema, pass to execute() | ||||||||||||||||||||||||||||
| 433 | context => $db, # the original schema, for convenience | ||||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | =head1 SCALAR TYPE MAPPING | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | SQL column types are mapped to GraphQL scalars in the following priority order: | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | Boolean: bool, boolean, tinyint(1) | ||||||||||||||||||||||||||||
| 441 | Float : float, double, double precision, real, money, decimal, numeric | ||||||||||||||||||||||||||||
| 442 | Int : int, integer, bigint, smallint, tinyint, mediumint, serial | ||||||||||||||||||||||||||||
| 443 | String : everything else (safe fallback) | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | The mapping is centralised in L<DBIO::GraphQL::ScalarMap> and reused by | ||||||||||||||||||||||||||||
| 446 | the filter, mutation, and relationship modules. | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | =head1 PLURAL QUERIES | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | Every source C<X> gets an C<allXs> query returning an C<XConnection>: | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | type XConnection { | ||||||||||||||||||||||||||||
| 453 | nodes: [X] | ||||||||||||||||||||||||||||
| 454 | total: Int # total rows matching filter, before pagination | ||||||||||||||||||||||||||||
| 455 | nextCursor: String # opaque cursor; set only during cursor pagination | ||||||||||||||||||||||||||||
| 456 | hasNextPage: Boolean # true when more pages follow | ||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | Always request C<total> or another scalar field alongside C<nodes> in your | ||||||||||||||||||||||||||||
| 460 | selection set - see L</KNOWN BEHAVIOUR>. | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | =head2 Filtering | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | Filter arguments use a nested per-column shape that mirrors the DBIO | ||||||||||||||||||||||||||||
| 465 | search-condition format. Each column accepts a typed C<*Filter> input | ||||||||||||||||||||||||||||
| 466 | with operators that depend on the column's scalar type | ||||||||||||||||||||||||||||
| 467 | (C<IntFilter>, C<FloatFilter>, C<StringFilter>, C<BoolFilter>). | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | allBooks(filter: { | ||||||||||||||||||||||||||||
| 470 | title: { like: "%Perl%" } | ||||||||||||||||||||||||||||
| 471 | author_id: { gt: 3 } | ||||||||||||||||||||||||||||
| 472 | active: { eq: true } | ||||||||||||||||||||||||||||
| 473 | AND: [ | ||||||||||||||||||||||||||||
| 474 | { title: { contains: "Hobbit" } } | ||||||||||||||||||||||||||||
| 475 | { OR: [ { title: { contains: "Ring" } }, { author_id: { gt: 5 } } ] } | ||||||||||||||||||||||||||||
| 476 | ] | ||||||||||||||||||||||||||||
| 477 | }) { total nodes { title } } | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | Per-scalar operators: | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | IntFilter / FloatFilter | ||||||||||||||||||||||||||||
| 482 | eq, not, gt, gte, lt, lte, in, isNull | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | StringFilter | ||||||||||||||||||||||||||||
| 485 | eq, not, like, contains, startsWith, endsWith, in, isNull | ||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | BoolFilter | ||||||||||||||||||||||||||||
| 488 | eq, not, isNull | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | Logical combinators (available on every per-source filter): | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | AND: [ Filter, Filter, ... ] | ||||||||||||||||||||||||||||
| 493 | OR: [ Filter, Filter, ... ] | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | =head2 Ordering | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | allBooks(orderBy: { field: "title", direction: ASC }) { | ||||||||||||||||||||||||||||
| 498 | total nodes { title } | ||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | C<direction> is the C<OrderDirection> enum: C<ASC> or C<DESC>. When | ||||||||||||||||||||||||||||
| 502 | omitted, results are ordered by primary key ascending. | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | =head2 Offset pagination | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | allBooks(page: { skip: 10, take: 5 }) { total nodes { title } } | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | C<skip> defaults to 0, C<take> defaults to 10. | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | =head2 Cursor pagination | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | # First page | ||||||||||||||||||||||||||||
| 513 | allBooks(cursor: { first: 5 }) { | ||||||||||||||||||||||||||||
| 514 | total nextCursor hasNextPage nodes { title } | ||||||||||||||||||||||||||||
| 515 | } | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | # Subsequent pages - pass nextCursor from the previous response | ||||||||||||||||||||||||||||
| 518 | allBooks(cursor: { after: "...", first: 5 }) { | ||||||||||||||||||||||||||||
| 519 | total nextCursor hasNextPage nodes { title } | ||||||||||||||||||||||||||||
| 520 | } | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | C<first> defaults to 10. Cursor pagination takes precedence over | ||||||||||||||||||||||||||||
| 523 | offset pagination if both are supplied in the same query. Cursors are | ||||||||||||||||||||||||||||
| 524 | opaque base64-encoded strings derived from the row's primary key and | ||||||||||||||||||||||||||||
| 525 | should be treated as implementation details subject to change. | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | =head1 MUTATIONS | ||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | For every source C<X>, three mutations are generated by | ||||||||||||||||||||||||||||
| 530 | L<DBIO::GraphQL::Mutation>: | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | =head2 createX | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | mutation { createBook(title: "Dune", author_id: 4) { id title } } | ||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||
| 536 | Accepts all column values as arguments. Columns that are non-nullable, | ||||||||||||||||||||||||||||
| 537 | have no declared default, and are not auto-increment are wrapped in | ||||||||||||||||||||||||||||
| 538 | C<NonNull> and must be supplied. On failure, C<die>s - the error | ||||||||||||||||||||||||||||
| 539 | appears in the top-level C<errors> array of the response. | ||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | =head2 updateX | ||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | mutation { updateBook(id: 1, title: "Dune Messiah") { id title } } | ||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | Identifies the target row by its primary key B<or> by a complete set | ||||||||||||||||||||||||||||
| 546 | of columns from any unique constraint declared on the source. All | ||||||||||||||||||||||||||||
| 547 | non-key columns must be supplied (full update). C<die>s if the row | ||||||||||||||||||||||||||||
| 548 | cannot be found or the update fails. | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | =head2 deleteX | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | mutation { deleteBook(id: 1) } | ||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||
| 554 | Identifies the target row by primary key or unique constraint. | ||||||||||||||||||||||||||||
| 555 | Returns C<true> (C<Boolean>) on success, C<false> if the row is not | ||||||||||||||||||||||||||||
| 556 | found. | ||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | =head1 ERROR HANDLING | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | =over 4 | ||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | =item * | ||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | C<createX> and C<updateX> resolver failures C<die>. GraphQL catches | ||||||||||||||||||||||||||||
| 565 | the exception and surfaces it in the top-level C<errors> array; the | ||||||||||||||||||||||||||||
| 566 | C<data> field for the failed mutation will be C<null>. | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | =item * | ||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | C<deleteX> returns C<false> rather than dying when a row is not found. | ||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | =item * | ||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | Relationship resolution C<die>s at build time when the DBIO | ||||||||||||||||||||||||||||
| 575 | relationship contract is incomplete (see | ||||||||||||||||||||||||||||
| 576 | L<DBIO::GraphQL::Relationship>). Set C<< on_error => 'warn' >> on | ||||||||||||||||||||||||||||
| 577 | the resolver to downgrade to a warning + silent skip. | ||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||
| 579 | =back | ||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||
| 581 | =head1 LIMITATIONS | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | =over 4 | ||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||
| 585 | =item * B<Relationship fields are unfiltered.> Relationship fields | ||||||||||||||||||||||||||||
| 586 | within a query (e.g. C<author { books { title } }>) return all | ||||||||||||||||||||||||||||
| 587 | related rows. They do not accept C<filter>, C<orderBy>, or pagination | ||||||||||||||||||||||||||||
| 588 | arguments. | ||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||
| 590 | =item * B<No nested input for mutations.> C<createX> and C<updateX> | ||||||||||||||||||||||||||||
| 591 | accept only scalar column values. Related rows must be created or | ||||||||||||||||||||||||||||
| 592 | linked separately using their own mutations and raw foreign-key | ||||||||||||||||||||||||||||
| 593 | values. | ||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | =item * B<updateX is a full update.> All non-primary-key columns | ||||||||||||||||||||||||||||
| 596 | must be supplied; partial (sparse) updates are not supported. | ||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | =item * B<Cursor pagination assumes primary-key order.> The | ||||||||||||||||||||||||||||
| 599 | C<after> cursor encodes the primary key of the last returned row | ||||||||||||||||||||||||||||
| 600 | and applies a C<pk E<gt> value> condition. If you supply a custom | ||||||||||||||||||||||||||||
| 601 | C<orderBy> using a non-primary-key column, the cursor will not | ||||||||||||||||||||||||||||
| 602 | advance correctly. Use offset pagination (C<page>) when ordering by | ||||||||||||||||||||||||||||
| 603 | non-PK columns. | ||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | =item * B<No custom scalars.> Column types such as C<date>, | ||||||||||||||||||||||||||||
| 606 | C<datetime>, C<json>, and C<uuid> all map to C<String>. Custom | ||||||||||||||||||||||||||||
| 607 | GraphQL scalars are not generated. | ||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | =item * B<No subscriptions.> Only C<Query> and C<Mutation> operation | ||||||||||||||||||||||||||||
| 610 | types are generated. | ||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | =item * B<col_like case sensitivity is database-dependent.> SQLite | ||||||||||||||||||||||||||||
| 613 | C<LIKE> is case-insensitive for ASCII characters but case-sensitive | ||||||||||||||||||||||||||||
| 614 | for Unicode. PostgreSQL C<LIKE> is always case-sensitive. | ||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | =back | ||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | =head1 KNOWN BEHAVIOUR | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | When querying a plural C<allXs> field, always include at least one | ||||||||||||||||||||||||||||
| 621 | scalar field (C<total>, C<hasNextPage>, or C<nextCursor>) alongside | ||||||||||||||||||||||||||||
| 622 | C<nodes> in your selection set: | ||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | # Correct | ||||||||||||||||||||||||||||
| 625 | { allBooks { total nodes { title } } } | ||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||
| 627 | # May silently return empty nodes in some GraphQL executor versions | ||||||||||||||||||||||||||||
| 628 | { allBooks { nodes { title } } } | ||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | This is a quirk of how C<GraphQL::Execution> resolves connection | ||||||||||||||||||||||||||||
| 631 | object types when the selection set contains only a list field. | ||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||
| 633 | =head1 ARCHITECTURE | ||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||
| 635 | L<DBIO::GraphQL> is a thin orchestrator over four focused modules: | ||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||
| 637 | =over 4 | ||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||
| 639 | =item * L<DBIO::GraphQL::ScalarMap> - column data_type → GraphQL scalar | ||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||
| 641 | =item * L<DBIO::GraphQL::Filter> (and L<DBIO::GraphQL::Filter::Search>, | ||||||||||||||||||||||||||||
| 642 | L<DBIO::GraphQL::Filter::Null>) - per-source GraphQL InputObject | ||||||||||||||||||||||||||||
| 643 | translating nested filter args into DBIO search conditions | ||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||
| 645 | =item * L<DBIO::GraphQL::Relationship> - relationship field | ||||||||||||||||||||||||||||
| 646 | resolution with strict contract validation (closes KARR #1) | ||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||
| 648 | =item * L<DBIO::GraphQL::Mutation> - createX / updateX / deleteX | ||||||||||||||||||||||||||||
| 649 | per source | ||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||
| 651 | =back | ||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||
| 653 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||
| 655 | DBIO port of | ||||||||||||||||||||||||||||
| 656 | L<DBIx::Class::Schema::GraphQL|https://metacpan.org/pod/DBIx::Class::Schema::GraphQL> | ||||||||||||||||||||||||||||
| 657 | by Mohammad Sajid Anwar (MANWAR). The original C<DBIx::Class> | ||||||||||||||||||||||||||||
| 658 | implementation, design, and documentation are his work; this | ||||||||||||||||||||||||||||
| 659 | distribution adapts them to the L<DBIO> schema introspection API | ||||||||||||||||||||||||||||
| 660 | and re-architects the filter surface into a nested per-column shape | ||||||||||||||||||||||||||||
| 661 | that mirrors DBIO's native search-condition format. | ||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||
| 663 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||
| 665 | L<DBIx::Class::Schema::GraphQL>, L<GraphQL::Plugin::Convert::DBIC>, | ||||||||||||||||||||||||||||
| 666 | L<DBIO::Schema>, L<GraphQL::Schema> | ||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||
| 668 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||
| 670 | DBIO Authors | ||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||
| 672 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||
| 674 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||
| 676 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 677 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||
| 679 | =cut | ||||||||||||||||||||||||||||