From 5d541d3263496638433052515b9dea5b271f9352 Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Sun, 15 Mar 2020 18:10:12 -0600 Subject: [PATCH 1/1] Version 0.600 --- Changes | 5 + LICENSE | 379 ++++++++++++++++++++++ MANIFEST | 31 ++ META.json | 150 +++++++++ META.yml | 71 +++++ Makefile.PL | 92 ++++++ README | 202 ++++++++++++ bin/graphql | 536 ++++++++++++++++++++++++++++++++ lib/GraphQL/Client.pm | 370 ++++++++++++++++++++++ lib/GraphQL/Client/http.pm | 326 +++++++++++++++++++ lib/GraphQL/Client/https.pm | 60 ++++ t/00-compile.t | 99 ++++++ t/00-report-prereqs.dd | 91 ++++++ t/00-report-prereqs.t | 193 ++++++++++++ t/client.t | 165 ++++++++++ t/http.t | 217 +++++++++++++ t/https.t | 13 + t/lib/MockTransport.pm | 38 +++ t/lib/MockUA.pm | 48 +++ xt/author/clean-namespaces.t | 11 + xt/author/critic.t | 7 + xt/author/eol.t | 36 +++ xt/author/minimum-version.t | 6 + xt/author/no-tabs.t | 36 +++ xt/author/pod-coverage.t | 7 + xt/author/pod-syntax.t | 7 + xt/author/portability.t | 10 + xt/release/consistent-version.t | 10 + xt/release/cpan-changes.t | 10 + xt/release/distmeta.t | 6 + 30 files changed, 3232 insertions(+) create mode 100644 Changes create mode 100644 LICENSE create mode 100644 MANIFEST create mode 100644 META.json create mode 100644 META.yml create mode 100644 Makefile.PL create mode 100644 README create mode 100755 bin/graphql create mode 100644 lib/GraphQL/Client.pm create mode 100644 lib/GraphQL/Client/http.pm create mode 100644 lib/GraphQL/Client/https.pm create mode 100644 t/00-compile.t create mode 100644 t/00-report-prereqs.dd create mode 100644 t/00-report-prereqs.t create mode 100755 t/client.t create mode 100755 t/http.t create mode 100755 t/https.t create mode 100644 t/lib/MockTransport.pm create mode 100644 t/lib/MockUA.pm create mode 100644 xt/author/clean-namespaces.t create mode 100644 xt/author/critic.t create mode 100644 xt/author/eol.t create mode 100644 xt/author/minimum-version.t create mode 100644 xt/author/no-tabs.t create mode 100644 xt/author/pod-coverage.t create mode 100644 xt/author/pod-syntax.t create mode 100644 xt/author/portability.t create mode 100644 xt/release/consistent-version.t create mode 100644 xt/release/cpan-changes.t create mode 100644 xt/release/distmeta.t diff --git a/Changes b/Changes new file mode 100644 index 0000000..c54d2b5 --- /dev/null +++ b/Changes @@ -0,0 +1,5 @@ +Revision history for GraphQL-Client. + +0.600 2020-03-15 18:08:57-06:00 MST7MDT + * Initial public release + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f87adb0 --- /dev/null +++ b/LICENSE @@ -0,0 +1,379 @@ +This software is copyright (c) 2020 by Charles McGarvey. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +Terms of the Perl programming language system itself + +a) the GNU General Public License as published by the Free + Software Foundation; either version 1, or (at your option) any + later version, or +b) the "Artistic License" + +--- The GNU General Public License, Version 1, February 1989 --- + +This software is Copyright (c) 2020 by Charles McGarvey. + +This is free software, licensed under: + + The GNU General Public License, Version 1, February 1989 + + GNU GENERAL PUBLIC LICENSE + Version 1, February 1989 + + Copyright (C) 1989 Free Software Foundation, Inc. + 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The license agreements of most software companies try to keep users +at the mercy of those companies. By contrast, our General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. The +General Public License applies to the Free Software Foundation's +software and to any other program whose authors commit to using it. +You can use it for your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Specifically, the General Public License is designed to make +sure that you have the freedom to give away or sell copies of free +software, that you receive source code or can get it if you want it, +that you can change the software or use pieces of it in new free +programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of a such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must tell them their rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any program or other work which +contains a notice placed by the copyright holder saying it may be +distributed under the terms of this General Public License. The +"Program", below, refers to any such program or work, and a "work based +on the Program" means either the Program or any work containing the +Program or a portion of it, either verbatim or with modifications. Each +licensee is addressed as "you". + + 1. You may copy and distribute verbatim copies of the Program's source +code as you receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice and +disclaimer of warranty; keep intact all the notices that refer to this +General Public License and to the absence of any warranty; and give any +other recipients of the Program a copy of this General Public License +along with the Program. You may charge a fee for the physical act of +transferring a copy. + + 2. You may modify your copy or copies of the Program or any portion of +it, and copy and distribute such modifications under the terms of Paragraph +1 above, provided that you also do the following: + + a) cause the modified files to carry prominent notices stating that + you changed the files and the date of any change; and + + b) cause the whole of any work that you distribute or publish, that + in whole or in part contains the Program or any part thereof, either + with or without modifications, to be licensed at no charge to all + third parties under the terms of this General Public License (except + that you may choose to grant warranty protection to some or all + third parties, at your option). + + c) If the modified program normally reads commands interactively when + run, you must cause it, when started running for such interactive use + in the simplest and most usual way, to print or display an + announcement including an appropriate copyright notice and a notice + that there is no warranty (or else, saying that you provide a + warranty) and that users may redistribute the program under these + conditions, and telling the user how to view a copy of this General + Public License. + + d) You may charge a fee for the physical act of transferring a + copy, and you may at your option offer warranty protection in + exchange for a fee. + +Mere aggregation of another independent work with the Program (or its +derivative) on a volume of a storage or distribution medium does not bring +the other work under the scope of these terms. + + 3. You may copy and distribute the Program (or a portion or derivative of +it, under Paragraph 2) in object code or executable form under the terms of +Paragraphs 1 and 2 above provided that you also do one of the following: + + a) accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of + Paragraphs 1 and 2 above; or, + + b) accompany it with a written offer, valid for at least three + years, to give any third party free (except for a nominal charge + for the cost of distribution) a complete machine-readable copy of the + corresponding source code, to be distributed under the terms of + Paragraphs 1 and 2 above; or, + + c) accompany it with the information you received as to where the + corresponding source code may be obtained. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form alone.) + +Source code for a work means the preferred form of the work for making +modifications to it. For an executable file, complete source code means +all the source code for all modules it contains; but, as a special +exception, it need not include source code for modules which are standard +libraries that accompany the operating system on which the executable +file runs, or for standard header files or definitions files that +accompany that operating system. + + 4. You may not copy, modify, sublicense, distribute or transfer the +Program except as expressly provided under this General Public License. +Any attempt otherwise to copy, modify, sublicense, distribute or transfer +the Program is void, and will automatically terminate your rights to use +the Program under this License. However, parties who have received +copies, or rights to use copies, from you under this General Public +License will not have their licenses terminated so long as such parties +remain in full compliance. + + 5. By copying, distributing or modifying the Program (or any work based +on the Program) you indicate your acceptance of this license to do so, +and all its terms and conditions. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the original +licensor to copy, distribute or modify the Program subject to these +terms and conditions. You may not impose any further restrictions on the +recipients' exercise of the rights granted herein. + + 7. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of the license which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +the license, you may choose any version ever published by the Free Software +Foundation. + + 8. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + Appendix: How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to humanity, the best way to achieve this is to make it +free software which everyone can redistribute and change under these +terms. + + To do so, attach the following notices to the program. It is safest to +attach them to the start of each source file to most effectively convey +the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) 19yy + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 1, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19xx name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the +appropriate parts of the General Public License. Of course, the +commands you use may be called something other than `show w' and `show +c'; they could even be mouse-clicks or menu items--whatever suits your +program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + program `Gnomovision' (a program to direct compilers to make passes + at assemblers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +That's all there is to it! + + +--- The Artistic License 1.0 --- + +This software is Copyright (c) 2020 by Charles McGarvey. + +This is free software, licensed under: + + The Artistic License 1.0 + +The Artistic License + +Preamble + +The intent of this document is to state the conditions under which a Package +may be copied, such that the Copyright Holder maintains some semblance of +artistic control over the development of the package, while giving the users of +the package the right to use and distribute the Package in a more-or-less +customary fashion, plus the right to make reasonable modifications. + +Definitions: + + - "Package" refers to the collection of files distributed by the Copyright + Holder, and derivatives of that collection of files created through + textual modification. + - "Standard Version" refers to such a Package if it has not been modified, + or has been modified in accordance with the wishes of the Copyright + Holder. + - "Copyright Holder" is whoever is named in the copyright or copyrights for + the package. + - "You" is you, if you're thinking about copying or distributing this Package. + - "Reasonable copying fee" is whatever you can justify on the basis of media + cost, duplication charges, time of people involved, and so on. (You will + not be required to justify it to the Copyright Holder, but only to the + computing community at large as a market that must bear the fee.) + - "Freely Available" means that no fee is charged for the item itself, though + there may be fees involved in handling the item. It also means that + recipients of the item may redistribute it under the same conditions they + received it. + +1. You may make and give away verbatim copies of the source form of the +Standard Version of this Package without restriction, provided that you +duplicate all of the original copyright notices and associated disclaimers. + +2. You may apply bug fixes, portability fixes and other modifications derived +from the Public Domain or from the Copyright Holder. A Package modified in such +a way shall still be considered the Standard Version. + +3. You may otherwise modify your copy of this Package in any way, provided that +you insert a prominent notice in each changed file stating how and when you +changed that file, and provided that you do at least ONE of the following: + + a) place your modifications in the Public Domain or otherwise make them + Freely Available, such as by posting said modifications to Usenet or an + equivalent medium, or placing the modifications on a major archive site + such as ftp.uu.net, or by allowing the Copyright Holder to include your + modifications in the Standard Version of the Package. + + b) use the modified Package only within your corporation or organization. + + c) rename any non-standard executables so the names do not conflict with + standard executables, which must also be provided, and provide a separate + manual page for each non-standard executable that clearly documents how it + differs from the Standard Version. + + d) make other distribution arrangements with the Copyright Holder. + +4. You may distribute the programs of this Package in object code or executable +form, provided that you do at least ONE of the following: + + a) distribute a Standard Version of the executables and library files, + together with instructions (in the manual page or equivalent) on where to + get the Standard Version. + + b) accompany the distribution with the machine-readable source of the Package + with your modifications. + + c) accompany any non-standard executables with their corresponding Standard + Version executables, giving the non-standard executables non-standard + names, and clearly documenting the differences in manual pages (or + equivalent), together with instructions on where to get the Standard + Version. + + d) make other distribution arrangements with the Copyright Holder. + +5. You may charge a reasonable copying fee for any distribution of this +Package. You may charge any fee you choose for support of this Package. You +may not charge a fee for this Package itself. However, you may distribute this +Package in aggregate with other (possibly commercial) programs as part of a +larger (possibly commercial) software distribution provided that you do not +advertise this Package as a product of your own. + +6. The scripts and library files supplied as input to or produced as output +from the programs of this Package do not automatically fall under the copyright +of this Package, but belong to whomever generated them, and may be sold +commercially, and may be aggregated with this Package. + +7. C or perl subroutines supplied by you and linked into this Package shall not +be considered part of this Package. + +8. The name of the Copyright Holder may not be used to endorse or promote +products derived from this software without specific prior written permission. + +9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF +MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + +The End + diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..8c517a2 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,31 @@ +# This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.014. +Changes +LICENSE +MANIFEST +META.json +META.yml +Makefile.PL +README +bin/graphql +lib/GraphQL/Client.pm +lib/GraphQL/Client/http.pm +lib/GraphQL/Client/https.pm +t/00-compile.t +t/00-report-prereqs.dd +t/00-report-prereqs.t +t/client.t +t/http.t +t/https.t +t/lib/MockTransport.pm +t/lib/MockUA.pm +xt/author/clean-namespaces.t +xt/author/critic.t +xt/author/eol.t +xt/author/minimum-version.t +xt/author/no-tabs.t +xt/author/pod-coverage.t +xt/author/pod-syntax.t +xt/author/portability.t +xt/release/consistent-version.t +xt/release/cpan-changes.t +xt/release/distmeta.t diff --git a/META.json b/META.json new file mode 100644 index 0000000..737e3ed --- /dev/null +++ b/META.json @@ -0,0 +1,150 @@ +{ + "abstract" : "A GraphQL client", + "author" : [ + "Charles McGarvey " + ], + "dynamic_config" : 0, + "generated_by" : "Dist::Zilla version 6.014, CPAN::Meta::Converter version 2.150010", + "license" : [ + "perl_5" + ], + "meta-spec" : { + "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", + "version" : 2 + }, + "name" : "GraphQL-Client", + "no_index" : { + "directory" : [ + "eg", + "share", + "shares", + "t", + "xt" + ] + }, + "prereqs" : { + "configure" : { + "requires" : { + "ExtUtils::MakeMaker" : "0" + } + }, + "develop" : { + "recommends" : { + "App::FatPacker" : "0", + "CPAN::Meta" : "0", + "Capture::Tiny" : "0", + "Config" : "0", + "File::pushd" : "0", + "Getopt::Long" : "0", + "MetaCPAN::API" : "0", + "Module::CoreList" : "0", + "Path::Tiny" : "0", + "Perl::Strip" : "0" + }, + "requires" : { + "Dist::Zilla" : "5", + "Dist::Zilla::Plugin::ConsistentVersionTest" : "0", + "Dist::Zilla::Plugin::Prereqs" : "0", + "Dist::Zilla::Plugin::RemovePhasedPrereqs" : "0", + "Dist::Zilla::Plugin::Run::Release" : "0", + "Dist::Zilla::PluginBundle::Author::CCM" : "0", + "Dist::Zilla::PluginBundle::Filter" : "0", + "Pod::Coverage::TrustPod" : "0", + "Software::License::Perl_5" : "0", + "Test::CPAN::Changes" : "0.19", + "Test::CPAN::Meta" : "0", + "Test::CleanNamespaces" : "0.15", + "Test::ConsistentVersion" : "0", + "Test::EOL" : "0", + "Test::MinimumVersion" : "0", + "Test::More" : "0.96", + "Test::NoTabs" : "0", + "Test::Perl::Critic" : "0", + "Test::Pod" : "1.41", + "Test::Pod::Coverage" : "1.08", + "Test::Portability::Files" : "0" + } + }, + "runtime" : { + "recommends" : { + "HTTP::Tiny" : "0", + "Pod::Usage" : "0" + }, + "requires" : { + "Carp" : "0", + "Getopt::Long" : "0", + "HTTP::AnyUA" : "0", + "HTTP::AnyUA::Util" : "0", + "JSON::MaybeXS" : "0", + "Module::Load" : "0", + "Scalar::Util" : "0", + "namespace::clean" : "0", + "overload" : "0", + "parent" : "0", + "perl" : "5.010", + "strict" : "0", + "warnings" : "0" + }, + "suggests" : { + "Data::Dumper" : "0", + "Text::CSV" : "0", + "Text::Table::Any" : "0", + "YAML" : "0" + } + }, + "test" : { + "recommends" : { + "CPAN::Meta" : "2.120900" + }, + "requires" : { + "ExtUtils::MakeMaker" : "0", + "File::Spec" : "0", + "FindBin" : "0", + "Future" : "0", + "HTTP::AnyUA::Backend" : "0", + "IO::Handle" : "0", + "IPC::Open3" : "0", + "Test::Deep" : "0", + "Test::Exception" : "0", + "Test::More" : "0", + "lib" : "0" + } + } + }, + "provides" : { + "GraphQL::Client" : { + "file" : "lib/GraphQL/Client.pm", + "version" : "0.600" + }, + "GraphQL::Client::Error" : { + "file" : "lib/GraphQL/Client.pm", + "version" : "0.600" + }, + "GraphQL::Client::http" : { + "file" : "lib/GraphQL/Client/http.pm", + "version" : "0.600" + }, + "GraphQL::Client::https" : { + "file" : "lib/GraphQL/Client/https.pm", + "version" : "0.600" + } + }, + "release_status" : "stable", + "resources" : { + "bugtracker" : { + "web" : "https://github.com/chazmcgarvey/graphql-client/issues" + }, + "homepage" : "https://github.com/chazmcgarvey/graphql-client", + "repository" : { + "type" : "git", + "url" : "https://github.com/chazmcgarvey/graphql-client.git", + "web" : "https://github.com/chazmcgarvey/graphql-client" + } + }, + "version" : "0.600", + "x_authority" : "cpan:CCM", + "x_generated_by_perl" : "v5.28.0", + "x_serialization_backend" : "Cpanel::JSON::XS version 4.15", + "x_spdx_expression" : "Artistic-1.0-Perl OR GPL-1.0-or-later" +} + diff --git a/META.yml b/META.yml new file mode 100644 index 0000000..ef5ab68 --- /dev/null +++ b/META.yml @@ -0,0 +1,71 @@ +--- +abstract: 'A GraphQL client' +author: + - 'Charles McGarvey ' +build_requires: + ExtUtils::MakeMaker: '0' + File::Spec: '0' + FindBin: '0' + Future: '0' + HTTP::AnyUA::Backend: '0' + IO::Handle: '0' + IPC::Open3: '0' + Test::Deep: '0' + Test::Exception: '0' + Test::More: '0' + lib: '0' +configure_requires: + ExtUtils::MakeMaker: '0' +dynamic_config: 0 +generated_by: 'Dist::Zilla version 6.014, CPAN::Meta::Converter version 2.150010' +license: perl +meta-spec: + url: http://module-build.sourceforge.net/META-spec-v1.4.html + version: '1.4' +name: GraphQL-Client +no_index: + directory: + - eg + - share + - shares + - t + - xt +provides: + GraphQL::Client: + file: lib/GraphQL/Client.pm + version: '0.600' + GraphQL::Client::Error: + file: lib/GraphQL/Client.pm + version: '0.600' + GraphQL::Client::http: + file: lib/GraphQL/Client/http.pm + version: '0.600' + GraphQL::Client::https: + file: lib/GraphQL/Client/https.pm + version: '0.600' +recommends: + HTTP::Tiny: '0' + Pod::Usage: '0' +requires: + Carp: '0' + Getopt::Long: '0' + HTTP::AnyUA: '0' + HTTP::AnyUA::Util: '0' + JSON::MaybeXS: '0' + Module::Load: '0' + Scalar::Util: '0' + namespace::clean: '0' + overload: '0' + parent: '0' + perl: '5.010' + strict: '0' + warnings: '0' +resources: + bugtracker: https://github.com/chazmcgarvey/graphql-client/issues + homepage: https://github.com/chazmcgarvey/graphql-client + repository: https://github.com/chazmcgarvey/graphql-client.git +version: '0.600' +x_authority: cpan:CCM +x_generated_by_perl: v5.28.0 +x_serialization_backend: 'YAML::Tiny version 1.73' +x_spdx_expression: 'Artistic-1.0-Perl OR GPL-1.0-or-later' diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..491a896 --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,92 @@ +# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v6.014. +use strict; +use warnings; + +use 5.010; + +use ExtUtils::MakeMaker; + +my %WriteMakefileArgs = ( + "ABSTRACT" => "A GraphQL client", + "AUTHOR" => "Charles McGarvey ", + "CONFIGURE_REQUIRES" => { + "ExtUtils::MakeMaker" => 0 + }, + "DISTNAME" => "GraphQL-Client", + "EXE_FILES" => [ + "bin/graphql" + ], + "LICENSE" => "perl", + "MIN_PERL_VERSION" => "5.010", + "NAME" => "GraphQL::Client", + "PREREQ_PM" => { + "Carp" => 0, + "Getopt::Long" => 0, + "HTTP::AnyUA" => 0, + "HTTP::AnyUA::Util" => 0, + "JSON::MaybeXS" => 0, + "Module::Load" => 0, + "Scalar::Util" => 0, + "namespace::clean" => 0, + "overload" => 0, + "parent" => 0, + "strict" => 0, + "warnings" => 0 + }, + "TEST_REQUIRES" => { + "ExtUtils::MakeMaker" => 0, + "File::Spec" => 0, + "FindBin" => 0, + "Future" => 0, + "HTTP::AnyUA::Backend" => 0, + "IO::Handle" => 0, + "IPC::Open3" => 0, + "Test::Deep" => 0, + "Test::Exception" => 0, + "Test::More" => 0, + "lib" => 0 + }, + "VERSION" => "0.600", + "test" => { + "TESTS" => "t/*.t" + } +); + + +my %FallbackPrereqs = ( + "Carp" => 0, + "ExtUtils::MakeMaker" => 0, + "File::Spec" => 0, + "FindBin" => 0, + "Future" => 0, + "Getopt::Long" => 0, + "HTTP::AnyUA" => 0, + "HTTP::AnyUA::Backend" => 0, + "HTTP::AnyUA::Util" => 0, + "IO::Handle" => 0, + "IPC::Open3" => 0, + "JSON::MaybeXS" => 0, + "Module::Load" => 0, + "Scalar::Util" => 0, + "Test::Deep" => 0, + "Test::Exception" => 0, + "Test::More" => 0, + "lib" => 0, + "namespace::clean" => 0, + "overload" => 0, + "parent" => 0, + "strict" => 0, + "warnings" => 0 +); + + +unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { + delete $WriteMakefileArgs{TEST_REQUIRES}; + delete $WriteMakefileArgs{BUILD_REQUIRES}; + $WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs; +} + +delete $WriteMakefileArgs{CONFIGURE_REQUIRES} + unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; + +WriteMakefile(%WriteMakefileArgs); diff --git a/README b/README new file mode 100644 index 0000000..ba5570e --- /dev/null +++ b/README @@ -0,0 +1,202 @@ +NAME + + GraphQL::Client - A GraphQL client + +VERSION + + version 0.600 + +SYNOPSIS + + my $graphql = GraphQL::Client->new(url => 'http://localhost:4000/graphql'); + + # Example: Hello world! + + my $response = $graphql->execute('{hello}'); + + # Example: Kitchen sink + + my $query = q[ + query GetHuman { + human(id: $human_id) { + name + height + } + } + ]; + my $variables = { + human_id => 1000, + }; + my $operation_name = 'GetHuman'; + my $transport_options = { + headers => { + authorization => 'Bearer s3cr3t', + }, + }; + my $response = $graphql->execute($query, $variables, $operation_name, $transport_options); + + # Example: Asynchronous with Mojo::UserAgent (promisify requires Future::Mojo) + + my $ua = Mojo::UserAgent->new; + my $graphql = GraphQL::Client->new(ua => $ua, url => 'http://localhost:4000/graphql'); + + my $future = $graphql->execute('{hello}'); + + $future->promisify->then(sub { + my $response = shift; + ... + }); + +DESCRIPTION + + GraphQL::Client provides a simple way to execute GraphQL + queries and mutations on a server. + + This module is the programmatic interface. There is also a graphql. + + GraphQL servers are usually served over HTTP. The provided transport, + GraphQL::Client::http, lets you plug in your own user agent, so this + client works naturally with HTTP::Tiny, Mojo::UserAgent, and more. You + can also use HTTP::AnyUA middleware. + +ATTRIBUTES + + url + + The URL of a GraphQL endpoint, e.g. "http://myapiserver/graphql". + + class + + The package name of a transport. + + By default this is automatically determined from the protocol portion + of the "url". + + transport + + The transport object. + + By default this is automatically constructed based on the "class". + + unpack + + Whether or not to "unpack" the response, which enables a different + style for error-handling. + + Default is 0. + + See "ERROR HANDLING". + +METHODS + + new + + $graphql = GraphQL::Client->new(%attributes); + + Construct a new client. + + execute + + $response = $graphql->execute($query); + $response = $graphql->execute($query, \%variables); + $response = $graphql->execute($query, \%variables, $operation_name); + $response = $graphql->execute($query, \%variables, $operation_name, \%transport_options); + $response = $graphql->execute($query, \%variables, \%transport_options); + + Execute a request on a GraphQL server, and get a response. + + By default, the response will either be a hashref with the following + structure or a Future that resolves to such a hashref, depending on the + transport and how it is configured. + + { + data => { + field1 => {...}, # or [...] + ... + }, + errors => [ + { message => 'some error message blah blah blah' }, + ... + ], + } + + Note: Setting the "unpack" attribute affects the response shape. + +ERROR HANDLING + + There are two different styles for handling errors. + + If "unpack" is 0 (off), every response -- whether success or failure -- + is enveloped like this: + + { + data => {...}, + errors => [...], + } + + where data might be missing or undef if errors occurred (though not + necessarily) and errors will be missing if the response completed + without error. + + It is up to you to check for errors in the response, so your code might + look like this: + + my $response = $graphql->execute(...); + if (my $errors = $response->{errors}) { + # handle $errors + } + else { + my $data = $response->{data}; + # do something with $data + } + + If unpack is 1 (on), then "execute" will return just the data if there + were no errors, otherwise it will throw an exception. So your code + would instead look like this: + + my $data = eval { $graphql->execute(...) }; + if (my $error = $@) { + # handle errors + } + else { + # do something with $data + } + + Or if you want to handle errors in a different stack frame, your code + is simply this: + + my $data = $graphql->execute(...); + # do something with $data + + Both styles map to Future responses intuitively. If unpack is 0, the + response always resolves to the envelope structure. If unpack is 1, + successful responses will resolve to just the data and errors will + fail/reject. + +SEE ALSO + + * graphql - CLI program + + * GraphQL - Perl implementation of a GraphQL server + + * https://graphql.org/ - GraphQL project website + +BUGS + + Please report any bugs or feature requests on the bugtracker website + https://github.com/chazmcgarvey/graphql-client/issues + + When submitting a bug or request, please include a test-file or a patch + to an existing test-file that illustrates the bug or desired feature. + +AUTHOR + + Charles McGarvey + +COPYRIGHT AND LICENSE + + This software is copyright (c) 2020 by Charles McGarvey. + + This is free software; you can redistribute it and/or modify it under + the same terms as the Perl 5 programming language system itself. + diff --git a/bin/graphql b/bin/graphql new file mode 100755 index 0000000..31c48ae --- /dev/null +++ b/bin/graphql @@ -0,0 +1,536 @@ +#! perl +# PODNAME: graphql +# ABSTRACT: Command-line GraphQL client + +# FATPACK - Do not remove this line. + +use warnings; +use strict; + +use Getopt::Long; +use GraphQL::Client; +use JSON::MaybeXS; + +our $VERSION = '0.600'; # VERSION + +my $version; +my $help; +my $manual; +my $url; +my $transport = {}; +my $query = '-'; +my $variables = {}; +my $operation_name; +my $format = 'json:pretty'; +my $unpack = 0; +my $outfile; +GetOptions( + 'version' => \$version, + 'help|h|?' => \$help, + 'manual|man' => \$manual, + 'url|u=s' => \$url, + 'query|mutation=s' => \$query, + 'variables|vars|V=s' => \$variables, + 'variable|var|d=s%' => \$variables, + 'operation-name|n=s' => \$operation_name, + 'transport|t=s%' => \$transport, + 'format|f=s' => \$format, + 'unpack!' => \$unpack, + 'output|o=s' => \$outfile, +) or pod2usage(2); + +if ($version) { + print "graphql $VERSION\n"; + exit 0; +} +if ($help) { + pod2usage(-exitval => 0, -verbose => 99, -sections => [qw(NAME SYNOPSIS OPTIONS)]); +} +if ($manual) { + pod2usage(-exitval => 0, -verbose => 2); +} + +$url = shift if !$url; +$query = shift if !$query || $query eq '-'; + +if (!$url) { + print STDERR "The or --url option argument is required.\n"; + pod2usage(2); +} + +$transport = expand_vars($transport); + +if (ref $variables) { + $variables = expand_vars($variables); +} +else { + $variables = JSON::MaybeXS->new->decode($variables); +} + +my $client = GraphQL::Client->new(url => $url); + +eval { $client->transport }; +if (my $err = $@) { + warn $err if $ENV{GRAPHQL_CLIENT_DEBUG}; + print STDERR "Could not construct a transport for URL: $url\n"; + print STDERR "Is this URL correct?\n"; + pod2usage(2); +} + +if (!$query || $query eq '-') { + print STDERR "Interactive mode engaged! Waiting for a query on ...\n" + if -t STDIN; ## no critic (InputOutput::ProhibitInteractiveTest) + $query = do { local $/; <> }; +} + +my $resp = $client->execute($query, $variables, $operation_name, $transport); +my $err = $resp->{errors}; +$unpack = 0 if $err; +my $data = $unpack ? $resp->{data} : $resp; + +if ($outfile) { + open(my $out, '>', $outfile) or die "Open $outfile failed: $!"; + *STDOUT = $out; +} + +print_data($data, $format); + +exit($unpack && $err ? 1 : 0); + +sub print_data { + my ($data, $format) = @_; + $format = lc($format || 'json:pretty'); + if ($format eq 'json' || $format eq 'json:pretty') { + my %opts = (canonical => 1, utf8 => 1); + $opts{pretty} = 1 if $format eq 'json:pretty'; + print JSON::MaybeXS->new(%opts)->encode($data); + } + elsif ($format eq 'yaml') { + eval { require YAML } or die "Missing dependency: YAML\n"; + print YAML::Dump($data); + } + elsif ($format eq 'csv' || $format eq 'tsv' || $format eq 'table') { + my $sep = $format eq 'tsv' ? "\t" : ','; + + my $unpacked = $data; + $unpacked = $data->{data} if !$unpack && !$err; + + # check the response to see if it can be formatted + my @columns; + my $rows = []; + if (keys %$unpacked == 1) { + my ($val) = values %$unpacked; + if (ref $val eq 'ARRAY') { + my $first = $val->[0]; + if ($first && ref $first eq 'HASH') { + @columns = sort keys %$first; + $rows = [ + map { [@{$_}{@columns}] } @$val + ]; + } + elsif ($first) { + @columns = keys %$unpacked; + $rows = [map { [$_] } @$val]; + } + } + } + + if (@columns) { + if ($format eq 'table') { + eval { require Text::Table::Any } or die "Missing dependency: Text::Table::Any\n"; + my $table = Text::Table::Any::table( + header_row => 1, + rows => [[@columns], @$rows], + backend => $ENV{PERL_TEXT_TABLE}, + ); + print $table; + } + else { + eval { require Text::CSV } or die "Missing dependency: Text::CSV\n"; + my $csv = Text::CSV->new({binary => 1, sep => $sep, eol => $/}); + $csv->print(*STDOUT, [@columns]); + for my $row (@$rows) { + $csv->print(*STDOUT, $row); + } + } + } + else { + print_data($data); + print STDERR sprintf("Error: Response could not be formatted as %s.\n", uc($format)); + exit 3; + } + } + elsif ($format eq 'perl') { + eval { require Data::Dumper } or die "Missing dependency: Data::Dumper\n"; + print Data::Dumper::Dumper($data); + } + else { + print STDERR "Error: Format not supported: $format\n"; + print_data($data); + exit 3; + } +} + +sub expand_vars { + my $vars = shift; + + my %out; + while (my ($key, $value) = each %$vars) { + my $var = $value; + my @segments = split(/\./, $key); + for my $segment (reverse @segments) { + my $saved = $var; + if ($segment =~ /^(\d+)$/) { + $var = []; + $var->[$segment] = $saved; + } + else { + $var = {}; + $var->{$segment} = $saved; + } + } + %out = (%out, %$var); + } + + return \%out; +} + +sub pod2usage { + eval { require Pod::Usage }; + if ($@) { + my $ref = $VERSION eq '999.999' ? 'master' : "v$VERSION"; + my $exit = (@_ == 1 && $_[0] =~ /^\d+$/ && $_[0]) // + (@_ % 2 == 0 && {@_}->{'-exitval'}) // 2; + print STDERR < [ [--variables JSON] | [--variable KEY=VALUE]... ] + [--operation-name NAME] [--transport KEY=VALUE]... + [--[no-]unpack] [--format json|json:pretty|yaml|perl|csv|tsv|table] + [--output FILE] + + graphql --version|--help|--manual + +=head1 DESCRIPTION + +C is a command-line program for executing queries and mutations on +a L server. + +=head1 INSTALL + +There are several ways to install F to your system. + +=head2 from CPAN + +You can install F using L: + + cpanm GraphQL::Client + +=head2 from GitHub + +You can also choose to download F as a self-contained executable: + + curl -OL https://raw.githubusercontent.com/chazmcgarvey/graphql-client/solo/graphql + chmod +x graphql + +To hack on the code, clone the repo instead: + + git clone https://github.com/chazmcgarvey/graphql-client.git + cd graphql-client + make bootstrap # installs dependencies; requires cpanm + +=head1 OPTIONS + +=head2 C<--url URL> + +The URL of the GraphQL server endpoint. + +If no C<--url> option is given, the first argument is assumed to be the URL. + +This option is required. + +Alias: C<-u> + +=head2 C<--query STR> + +The query or mutation to execute. + +If no C<--query> option is given, the next argument (after URL) is assumed to be the query. + +If the value is "-" (which is the default), the query will be read from C. + +See: L + +Alias: C<--mutation> + +=head2 C<--variables JSON> + +Provide the variables as a JSON object. + +Aliases: C<--vars>, C<-V> + +=head2 C<--variable KEY=VALUE> + +An alternative way to provide variables. Repeat this option to provide multiple variables. + +If used in combination with L, this option is silently ignored. + +See: L + +Aliases: C<--var>, C<-d> + +=head2 C<--operation-name NAME> + +Inform the server which query/mutation to execute. + +Alias: C<-n> + +=head2 C<--output FILE> + +Write the response to a file instead of STDOUT. + +Alias: C<-o> + +=head2 C<--transport KEY=VALUE> + +Key-value pairs for configuring the transport (usually HTTP). + +Alias: C<-t> + +=head2 C<--format STR> + +Specify the output format to use. See L. + +Alias: C<-f> + +=head2 C<--unpack> + +Enables unpack mode. + +By default, the response structure is printed as-is from the server, and the program exits 0. + +When unpack mode is enabled, if the response completes with no errors, only the data section of +the response is printed and the program exits 0. If the response has errors, the whole response +structure is printed as-is and the program exits 1. + +See L. + +=head1 FORMAT + +The argument for L can be one of: + +=over 4 + +=item * + +C - Comma-separated values (requires L) + +=item * + +C - Human-readable JSON (default) + +=item * + +C - JSON + +=item * + +C - Perl code (requires L) + +=item * + +C - Table (requires L) + +=item * + +C - Tab-separated values (requires L) + +=item * + +C - YAML (requires L) + +=back + +The C, C, and C
formats will only work if the response has a particular shape: + + { + "data" : { + "onefield" : [ + { + "key" : "value", + ... + }, + ... + ] + } + } + +or + + { + "data" : { + "onefield" : [ + "value", + ... + ] + } + } + +If the response cannot be formatted, the default format will be used instead, an error message will +be printed to STDERR, and the program will exit 3. + +Table formatting can be done by one of several different modules, each with its own features and +bugs. The default module is L, but this can be overridden using the +C environment variable if desired, like this: + + PERL_TEXT_TABLE=Text::Table::HTML graphql ... -f table + +The list of supported modules is at L. + +=head1 EXAMPLES + +Different ways to provide the query/mutation to execute: + + graphql http://myserver/graphql {hello} + + echo {hello} | graphql http://myserver/graphql + + graphql http://myserver/graphql < {hello} + > END + + graphql http://myserver/graphql + Interactive mode engaged! Waiting for a query on ... + {hello} + ^D + +Execute a query with variables: + + graphql http://myserver/graphql < query HeroNameAndFriends($episode: Episode) { + > hero(episode: $episode) { + > name + > friends { + > name + > } + > } + > } + > END + + graphql http://myserver/graphql --vars '{"episode":"JEDI"}' + +Configure the transport: + + graphql http://myserver/graphql {hello} -t headers.authorization='Basic s3cr3t' + +This example shows the effect of L: + + graphql http://myserver/graphql {hello} + + # Output: + { + "data" : { + "hello" : "Hello world!" + } + } + + graphql http://myserver/graphql {hello} --unpack + + # Output: + { + "hello" : "Hello world!" + } + +=head1 ENVIRONMENT + +Some environment variables affect the way C behaves: + +=over 4 + +=item * + +C - Set to 1 to print diagnostic messages to STDERR. + +=item * + +C - Set the HTTP user agent string. + +=item * + +C - Set table format backend; see L. + +=back + +=head1 EXIT STATUS + +Here is a consolidated summary of what exit statuses mean: + +=over 4 + +=item * + +C<0> - Success + +=item * + +C<1> - Client or server errors + +=item * + +C<2> - Option usage is wrong + +=item * + +C<3> - Could not format the response as requested + +=back + +=head1 BUGS + +Please report any bugs or feature requests on the bugtracker website +L + +When submitting a bug or request, please include a test-file or a +patch to an existing test-file that illustrates the bug or desired +feature. + +=head1 AUTHOR + +Charles McGarvey + +=head1 COPYRIGHT AND LICENSE + +This software is copyright (c) 2020 by Charles McGarvey. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=cut diff --git a/lib/GraphQL/Client.pm b/lib/GraphQL/Client.pm new file mode 100644 index 0000000..2f844fe --- /dev/null +++ b/lib/GraphQL/Client.pm @@ -0,0 +1,370 @@ +package GraphQL::Client; +# ABSTRACT: A GraphQL client + +use warnings; +use strict; + +use Module::Load qw(load); +use Scalar::Util qw(reftype); +use namespace::clean; + +our $VERSION = '0.600'; # VERSION + +sub _croak { require Carp; goto &Carp::croak } +sub _throw { GraphQL::Client::Error->throw(@_) } + +sub new { + my $class = shift; + bless {@_}, $class; +} + +sub execute { + my $self = shift; + my ($query, $variables, $operation_name, $options) = @_; + + if ((reftype($operation_name) || '') eq 'HASH') { + $options = $operation_name; + $operation_name = undef; + } + + my $request = { + query => $query, + ($variables && %$variables) ? (variables => $variables) : (), + $operation_name ? (operationName => $operation_name) : (), + }; + + return $self->_handle_result($self->transport->execute($request, $options)); +} + +sub _handle_result { + my $self = shift; + my ($result) = @_; + + my $handle_result = sub { + my $result = shift; + my $resp = $result->{response}; + if (my $exception = $result->{error}) { + unshift @{$resp->{errors}}, { + message => "$exception", + }; + } + if ($self->unpack) { + if ($resp->{errors}) { + _throw $resp->{errors}[0]{message}, { + type => 'graphql', + response => $resp, + details => $result->{details}, + }; + } + return $resp->{data}; + } + return $resp; + }; + + if (eval { $result->isa('Future') }) { + return $result->transform( + done => sub { + my $result = shift; + my $resp = eval { $handle_result->($result) }; + if (my $err = $@) { + Future::Exception->throw("$err", $err->{type}, $err->{response}, $err->{details}); + } + return $resp; + }, + ); + } + else { + return $handle_result->($result); + } +} + +sub url { + my $self = shift; + $self->{url}; +} + +sub class { + my $self = shift; + $self->{class}; +} + +sub transport { + my $self = shift; + $self->{transport} //= do { + my $class = $self->_transport_class; + eval { load $class }; + if ((my $err = $@) || !$class->can('execute')) { + $err ||= "Loaded $class, but it doesn't look like a proper transport.\n"; + warn $err if $ENV{GRAPHQL_CLIENT_DEBUG}; + _croak "Failed to load transport for \"${class}\""; + } + $class->new(%$self); + }; +} + +sub unpack { + my $self = shift; + $self->{unpack} //= 0; +} + +sub _url_protocol { + my $self = shift; + + my $url = $self->url; + my ($protocol) = $url =~ /^([^+:]+)/; + + return $protocol; +} + +sub _transport_class { + my $self = shift; + + return _expand_class($self->{class}) if $self->{class}; + + my $protocol = $self->_url_protocol; + _croak 'Failed to determine transport from URL' if !$protocol; + + my $class = lc($protocol); + $class =~ s/[^a-z]/_/g; + + return _expand_class($class); +} + +sub _expand_class { + my $class = shift; + $class = "GraphQL::Client::$class" unless $class =~ s/^\+//; + $class; +} + +{ + package GraphQL::Client::Error; + + use warnings; + use strict; + + use overload '""' => \&error, fallback => 1; + + sub new { bless {%{$_[2] || {}}, error => $_[1] || 'Something happened'}, $_[0] } + + sub error { "$_[0]->{error}" } + sub type { "$_[0]->{type}" } + + sub throw { + my $self = shift; + die $self if ref $self; + die $self->new(@_); + } +} + +1; + +__END__ + +=pod + +=encoding UTF-8 + +=head1 NAME + +GraphQL::Client - A GraphQL client + +=head1 VERSION + +version 0.600 + +=head1 SYNOPSIS + + my $graphql = GraphQL::Client->new(url => 'http://localhost:4000/graphql'); + + # Example: Hello world! + + my $response = $graphql->execute('{hello}'); + + # Example: Kitchen sink + + my $query = q[ + query GetHuman { + human(id: $human_id) { + name + height + } + } + ]; + my $variables = { + human_id => 1000, + }; + my $operation_name = 'GetHuman'; + my $transport_options = { + headers => { + authorization => 'Bearer s3cr3t', + }, + }; + my $response = $graphql->execute($query, $variables, $operation_name, $transport_options); + + # Example: Asynchronous with Mojo::UserAgent (promisify requires Future::Mojo) + + my $ua = Mojo::UserAgent->new; + my $graphql = GraphQL::Client->new(ua => $ua, url => 'http://localhost:4000/graphql'); + + my $future = $graphql->execute('{hello}'); + + $future->promisify->then(sub { + my $response = shift; + ... + }); + +=head1 DESCRIPTION + +C provides a simple way to execute L queries and +mutations on a server. + +This module is the programmatic interface. There is also a L. + +GraphQL servers are usually served over HTTP. The provided transport, L, lets +you plug in your own user agent, so this client works naturally with L, +L, and more. You can also use L middleware. + +=head1 ATTRIBUTES + +=head2 url + +The URL of a GraphQL endpoint, e.g. C<"http://myapiserver/graphql">. + +=head2 class + +The package name of a transport. + +By default this is automatically determined from the protocol portion of the L. + +=head2 transport + +The transport object. + +By default this is automatically constructed based on the L. + +=head2 unpack + +Whether or not to "unpack" the response, which enables a different style for error-handling. + +Default is 0. + +See L. + +=head1 METHODS + +=head2 new + + $graphql = GraphQL::Client->new(%attributes); + +Construct a new client. + +=head2 execute + + $response = $graphql->execute($query); + $response = $graphql->execute($query, \%variables); + $response = $graphql->execute($query, \%variables, $operation_name); + $response = $graphql->execute($query, \%variables, $operation_name, \%transport_options); + $response = $graphql->execute($query, \%variables, \%transport_options); + +Execute a request on a GraphQL server, and get a response. + +By default, the response will either be a hashref with the following structure or a L that +resolves to such a hashref, depending on the transport and how it is configured. + + { + data => { + field1 => {...}, # or [...] + ... + }, + errors => [ + { message => 'some error message blah blah blah' }, + ... + ], + } + +Note: Setting the L attribute affects the response shape. + +=head1 ERROR HANDLING + +There are two different styles for handling errors. + +If L is 0 (off), every response -- whether success or failure -- is enveloped like this: + + { + data => {...}, + errors => [...], + } + +where C might be missing or undef if errors occurred (though not necessarily) and C +will be missing if the response completed without error. + +It is up to you to check for errors in the response, so your code might look like this: + + my $response = $graphql->execute(...); + if (my $errors = $response->{errors}) { + # handle $errors + } + else { + my $data = $response->{data}; + # do something with $data + } + +If C is 1 (on), then L will return just the data if there were no errors, +otherwise it will throw an exception. So your code would instead look like this: + + my $data = eval { $graphql->execute(...) }; + if (my $error = $@) { + # handle errors + } + else { + # do something with $data + } + +Or if you want to handle errors in a different stack frame, your code is simply this: + + my $data = $graphql->execute(...); + # do something with $data + +Both styles map to L responses intuitively. If C is 0, the response always resolves +to the envelope structure. If C is 1, successful responses will resolve to just the data and +errors will fail/reject. + +=head1 SEE ALSO + +=over 4 + +=item * + +L - CLI program + +=item * + +L - Perl implementation of a GraphQL server + +=item * + +L - GraphQL project website + +=back + +=head1 BUGS + +Please report any bugs or feature requests on the bugtracker website +L + +When submitting a bug or request, please include a test-file or a +patch to an existing test-file that illustrates the bug or desired +feature. + +=head1 AUTHOR + +Charles McGarvey + +=head1 COPYRIGHT AND LICENSE + +This software is copyright (c) 2020 by Charles McGarvey. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=cut diff --git a/lib/GraphQL/Client/http.pm b/lib/GraphQL/Client/http.pm new file mode 100644 index 0000000..7adf4a4 --- /dev/null +++ b/lib/GraphQL/Client/http.pm @@ -0,0 +1,326 @@ +package GraphQL::Client::http; +# ABSTRACT: GraphQL over HTTP + +use 5.010; +use warnings; +use strict; + +use HTTP::AnyUA::Util qw(www_form_urlencode); +use HTTP::AnyUA; +use namespace::clean; + +our $VERSION = '0.600'; # VERSION + +sub _croak { require Carp; goto &Carp::croak } + +sub new { + my $class = shift; + my $self = @_ % 2 == 0 ? {@_} : $_[0]; + bless $self, $class; +} + +sub execute { + my $self = shift; + my ($request, $options) = @_; + + my $url = delete $options->{url} || $self->url; + my $method = delete $options->{method} || $self->method; + + $request && ref($request) eq 'HASH' or _croak q{Usage: $http->execute(\%request)}; + $request->{query} or _croak q{Request must have a query}; + $url or _croak q{URL must be provided}; + + my $data = {%$request}; + + if ($method eq 'GET' || $method eq 'HEAD') { + $data->{variables} = $self->json->encode($data->{variables}) if $data->{variables}; + my $params = www_form_urlencode($data); + my $sep = $url =~ /^[^#]+\?/ ? '&' : '?'; + $url =~ s/#/${sep}${params}#/ or $url .= "${sep}${params}"; + } + else { + my $encoded_data = $self->json->encode($data); + $options->{content} = $encoded_data; + $options->{headers}{'content-length'} = length $encoded_data; + $options->{headers}{'content-type'} = 'application/json'; + } + + return $self->_handle_response($self->any_ua->request($method, $url, $options)); +} + +sub _handle_response { + my $self = shift; + my ($resp) = @_; + + if (eval { $resp->isa('Future') }) { + return $resp->followed_by(sub { + my $f = shift; + + if (my ($exception, $category, @other) = $f->failure) { + if (ref $exception eq 'HASH') { + my $resp = $exception; + return Future->done($self->_handle_error($resp)); + } + + return Future->done({ + error => $exception, + response => undef, + details => { + exception_details => [$category, @other], + }, + }); + } + + my $resp = $f->get; + return Future->done($self->_handle_success($resp)); + }); + } + else { + return $self->_handle_error($resp) if !$resp->{success}; + return $self->_handle_success($resp); + } +} + +sub _handle_error { + my $self = shift; + my ($resp) = @_; + + my $data = eval { $self->json->decode($resp->{content}) }; + my $content = $resp->{content} // 'No content'; + my $reason = $resp->{reason} // ''; + my $message = "HTTP transport returned $resp->{status} ($reason): $content"; + + return { + error => $message, + response => $data, + details => { + http_response => $resp, + }, + }; +} + +sub _handle_success { + my $self = shift; + my ($resp) = @_; + + my $data = eval { $self->json->decode($resp->{content}) }; + if (my $exception = $@) { + return { + error => "HTTP transport failed to decode response: $exception", + response => undef, + details => { + http_response => $resp, + }, + }; + } + + return { + response => $data, + details => { + http_response => $resp, + }, + }; +} + +sub ua { + my $self = shift; + $self->{ua} //= do { + require HTTP::Tiny; + HTTP::Tiny->new( + agent => $ENV{GRAPHQL_CLIENT_HTTP_USER_AGENT} // "perl-graphql-client/$VERSION", + ); + }; +} + +sub any_ua { + my $self = shift; + $self->{any_ua} //= HTTP::AnyUA->new(ua => $self->ua); +} + +sub url { + my $self = shift; + $self->{url}; +} + +sub method { + my $self = shift; + $self->{method} // 'POST'; +} + +sub json { + my $self = shift; + $self->{json} //= do { + require JSON::MaybeXS; + JSON::MaybeXS->new(utf8 => 1); + }; +} + +1; + +__END__ + +=pod + +=encoding UTF-8 + +=head1 NAME + +GraphQL::Client::http - GraphQL over HTTP + +=head1 VERSION + +version 0.600 + +=head1 SYNOPSIS + + my $transport = GraphQL::Client::http->new( + url => 'http://localhost:5000/graphql', + method => 'POST', + ); + + my $request = { + query => 'query Greet($name: String) { hello(name: $name) }', + operationName => 'Greet', + variables => { name => 'Bob' }, + }; + my $options = { + headers => { + authorization => 'Bearer s3cr3t', + }, + }; + my $response = $client->execute($request, $options); + +=head1 DESCRIPTION + +You probably shouldn't use this directly. Instead use L. + +C is a GraphQL transport for HTTP. GraphQL is not required to be transported +via HTTP, but this is definitely the most common way. + +This also serves as a reference implementation for C transports. + +=head1 ATTRIBUTES + +=head2 ua + +A user agent, such as: + +=over 4 + +=item * + +instance of a L (this is the default if no user agent is provided) + +=item * + +instance of a L + +=item * + +the string C<"AnyEvent::HTTP"> + +=item * + +and more... + +=back + +See L. + +=head2 any_ua + +The L instance. Can be used to apply middleware if desired. + +=head2 url + +The http URL of a GraphQL endpoint, e.g. C<"http://myapiserver/graphql">. + +=head2 method + +The HTTP method to use when querying the GraphQL server. Can be one of: + +=over 4 + +=item * + +C + +=item * + +C (default) + +=back + +GraphQL servers should be able to handle both, but you can set this explicitly to one or the other +if you're dealing with a server that is opinionated. You can also provide a different HTTP method, +but anything other than C and C are less likely to work. + +=head2 json + +The L (or compatible) object used for encoding and decoding data structures to and from +the GraphQL server. + +Defaults to a L. + +=head1 METHODS + +=head2 new + + $transport = GraphQL::Client::http->new(%attributes); + +Construct a new GraphQL HTTP transport. + +See L. + +=head2 execute + + $response = $client->execute(\%request); + $response = $client->execute(\%request, \%options); + +Get a response from the GraphQL server. + +The C<%data> structure must have a C key whose value is the query or mutation string. It may +optionally have a C hashref and an C string. + +The C<%options> structure is optional and may contain options passed through to the user agent. The +only useful options are C (which should have a hashref value) and C and C to +override the attributes of the same names. + +The response will either be a hashref with the following structure or a L that resolves to +such a hashref: + + { + response => { # decoded response (may be undef if an error occurred) + data => {...}, + errors => [...], + }, + error => 'Something happened', # may be ommitted if no error occurred + details => { # optional information which may aide troubleshooting + }, + } + +=head1 SEE ALSO + +L + +=head1 BUGS + +Please report any bugs or feature requests on the bugtracker website +L + +When submitting a bug or request, please include a test-file or a +patch to an existing test-file that illustrates the bug or desired +feature. + +=head1 AUTHOR + +Charles McGarvey + +=head1 COPYRIGHT AND LICENSE + +This software is copyright (c) 2020 by Charles McGarvey. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=cut diff --git a/lib/GraphQL/Client/https.pm b/lib/GraphQL/Client/https.pm new file mode 100644 index 0000000..6467664 --- /dev/null +++ b/lib/GraphQL/Client/https.pm @@ -0,0 +1,60 @@ +package GraphQL::Client::https; +# ABSTRACT: GraphQL over HTTPS + +use warnings; +use strict; + +use parent 'GraphQL::Client::http'; + +our $VERSION = '0.600'; # VERSION + +sub new { + my $class = shift; + GraphQL::Client::http->new(@_); +} + +1; + +__END__ + +=pod + +=encoding UTF-8 + +=head1 NAME + +GraphQL::Client::https - GraphQL over HTTPS + +=head1 VERSION + +version 0.600 + +=head1 DESCRIPTION + +This is the same as L. + +=head1 SEE ALSO + +L + +=head1 BUGS + +Please report any bugs or feature requests on the bugtracker website +L + +When submitting a bug or request, please include a test-file or a +patch to an existing test-file that illustrates the bug or desired +feature. + +=head1 AUTHOR + +Charles McGarvey + +=head1 COPYRIGHT AND LICENSE + +This software is copyright (c) 2020 by Charles McGarvey. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=cut diff --git a/t/00-compile.t b/t/00-compile.t new file mode 100644 index 0000000..676ed70 --- /dev/null +++ b/t/00-compile.t @@ -0,0 +1,99 @@ +use 5.006; +use strict; +use warnings; + +# this test was generated with Dist::Zilla::Plugin::Test::Compile 2.058 + +use Test::More; + +plan tests => 4 + ($ENV{AUTHOR_TESTING} ? 1 : 0); + +my @module_files = ( + 'GraphQL/Client.pm', + 'GraphQL/Client/http.pm', + 'GraphQL/Client/https.pm' +); + +my @scripts = ( + 'bin/graphql' +); + +# no fake home requested + +my @switches = ( + -d 'blib' ? '-Mblib' : '-Ilib', +); + +use File::Spec; +use IPC::Open3; +use IO::Handle; + +open my $stdin, '<', File::Spec->devnull or die "can't open devnull: $!"; + +my @warnings; +for my $lib (@module_files) +{ + # see L + my $stderr = IO::Handle->new; + + diag('Running: ', join(', ', map { my $str = $_; $str =~ s/'/\\'/g; q{'} . $str . q{'} } + $^X, @switches, '-e', "require q[$lib]")) + if $ENV{PERL_COMPILE_TEST_DEBUG}; + + my $pid = open3($stdin, '>&STDERR', $stderr, $^X, @switches, '-e', "require q[$lib]"); + binmode $stderr, ':crlf' if $^O eq 'MSWin32'; + my @_warnings = <$stderr>; + waitpid($pid, 0); + is($?, 0, "$lib loaded ok"); + + shift @_warnings if @_warnings and $_warnings[0] =~ /^Using .*\bblib/ + and not eval { +require blib; blib->VERSION('1.01') }; + + if (@_warnings) + { + warn @_warnings; + push @warnings, @_warnings; + } +} + +foreach my $file (@scripts) +{ SKIP: { + open my $fh, '<', $file or warn("Unable to open $file: $!"), next; + my $line = <$fh>; + + close $fh and skip("$file isn't perl", 1) unless $line =~ /^#!\s*(?:\S*perl\S*)((?:\s+-\w*)*)(?:\s*#.*)?$/; + @switches = (@switches, split(' ', $1)) if $1; + + close $fh and skip("$file uses -T; not testable with PERL5LIB", 1) + if grep { $_ eq '-T' } @switches and $ENV{PERL5LIB}; + + my $stderr = IO::Handle->new; + + diag('Running: ', join(', ', map { my $str = $_; $str =~ s/'/\\'/g; q{'} . $str . q{'} } + $^X, @switches, '-c', $file)) + if $ENV{PERL_COMPILE_TEST_DEBUG}; + + my $pid = open3($stdin, '>&STDERR', $stderr, $^X, @switches, '-c', $file); + binmode $stderr, ':crlf' if $^O eq 'MSWin32'; + my @_warnings = <$stderr>; + waitpid($pid, 0); + is($?, 0, "$file compiled ok"); + + shift @_warnings if @_warnings and $_warnings[0] =~ /^Using .*\bblib/ + and not eval { +require blib; blib->VERSION('1.01') }; + + # in older perls, -c output is simply the file portion of the path being tested + if (@_warnings = grep { !/\bsyntax OK$/ } + grep { chomp; $_ ne (File::Spec->splitpath($file))[2] } @_warnings) + { + warn @_warnings; + push @warnings, @_warnings; + } +} } + + + +is(scalar(@warnings), 0, 'no warnings found') + or diag 'got warnings: ', ( Test::More->can('explain') ? Test::More::explain(\@warnings) : join("\n", '', @warnings) ) if $ENV{AUTHOR_TESTING}; + + diff --git a/t/00-report-prereqs.dd b/t/00-report-prereqs.dd new file mode 100644 index 0000000..8ec21f8 --- /dev/null +++ b/t/00-report-prereqs.dd @@ -0,0 +1,91 @@ +do { my $x = { + 'configure' => { + 'requires' => { + 'ExtUtils::MakeMaker' => '0' + } + }, + 'develop' => { + 'recommends' => { + 'App::FatPacker' => '0', + 'CPAN::Meta' => '0', + 'Capture::Tiny' => '0', + 'Config' => '0', + 'File::pushd' => '0', + 'Getopt::Long' => '0', + 'MetaCPAN::API' => '0', + 'Module::CoreList' => '0', + 'Path::Tiny' => '0', + 'Perl::Strip' => '0' + }, + 'requires' => { + 'Dist::Zilla' => '5', + 'Dist::Zilla::Plugin::ConsistentVersionTest' => '0', + 'Dist::Zilla::Plugin::Prereqs' => '0', + 'Dist::Zilla::Plugin::RemovePhasedPrereqs' => '0', + 'Dist::Zilla::Plugin::Run::Release' => '0', + 'Dist::Zilla::PluginBundle::Author::CCM' => '0', + 'Dist::Zilla::PluginBundle::Filter' => '0', + 'Pod::Coverage::TrustPod' => '0', + 'Software::License::Perl_5' => '0', + 'Test::CPAN::Changes' => '0.19', + 'Test::CPAN::Meta' => '0', + 'Test::CleanNamespaces' => '0.15', + 'Test::ConsistentVersion' => '0', + 'Test::EOL' => '0', + 'Test::MinimumVersion' => '0', + 'Test::More' => '0.96', + 'Test::NoTabs' => '0', + 'Test::Perl::Critic' => '0', + 'Test::Pod' => '1.41', + 'Test::Pod::Coverage' => '1.08', + 'Test::Portability::Files' => '0' + } + }, + 'runtime' => { + 'recommends' => { + 'HTTP::Tiny' => '0', + 'Pod::Usage' => '0' + }, + 'requires' => { + 'Carp' => '0', + 'Getopt::Long' => '0', + 'HTTP::AnyUA' => '0', + 'HTTP::AnyUA::Util' => '0', + 'JSON::MaybeXS' => '0', + 'Module::Load' => '0', + 'Scalar::Util' => '0', + 'namespace::clean' => '0', + 'overload' => '0', + 'parent' => '0', + 'perl' => '5.010', + 'strict' => '0', + 'warnings' => '0' + }, + 'suggests' => { + 'Data::Dumper' => '0', + 'Text::CSV' => '0', + 'Text::Table::Any' => '0', + 'YAML' => '0' + } + }, + 'test' => { + 'recommends' => { + 'CPAN::Meta' => '2.120900' + }, + 'requires' => { + 'ExtUtils::MakeMaker' => '0', + 'File::Spec' => '0', + 'FindBin' => '0', + 'Future' => '0', + 'HTTP::AnyUA::Backend' => '0', + 'IO::Handle' => '0', + 'IPC::Open3' => '0', + 'Test::Deep' => '0', + 'Test::Exception' => '0', + 'Test::More' => '0', + 'lib' => '0' + } + } + }; + $x; + } \ No newline at end of file diff --git a/t/00-report-prereqs.t b/t/00-report-prereqs.t new file mode 100644 index 0000000..c72183a --- /dev/null +++ b/t/00-report-prereqs.t @@ -0,0 +1,193 @@ +#!perl + +use strict; +use warnings; + +# This test was generated by Dist::Zilla::Plugin::Test::ReportPrereqs 0.027 + +use Test::More tests => 1; + +use ExtUtils::MakeMaker; +use File::Spec; + +# from $version::LAX +my $lax_version_re = + qr/(?: undef | (?: (?:[0-9]+) (?: \. | (?:\.[0-9]+) (?:_[0-9]+)? )? + | + (?:\.[0-9]+) (?:_[0-9]+)? + ) | (?: + v (?:[0-9]+) (?: (?:\.[0-9]+)+ (?:_[0-9]+)? )? + | + (?:[0-9]+)? (?:\.[0-9]+){2,} (?:_[0-9]+)? + ) + )/x; + +# hide optional CPAN::Meta modules from prereq scanner +# and check if they are available +my $cpan_meta = "CPAN::Meta"; +my $cpan_meta_pre = "CPAN::Meta::Prereqs"; +my $HAS_CPAN_META = eval "require $cpan_meta; $cpan_meta->VERSION('2.120900')" && eval "require $cpan_meta_pre"; ## no critic + +# Verify requirements? +my $DO_VERIFY_PREREQS = 1; + +sub _max { + my $max = shift; + $max = ( $_ > $max ) ? $_ : $max for @_; + return $max; +} + +sub _merge_prereqs { + my ($collector, $prereqs) = @_; + + # CPAN::Meta::Prereqs object + if (ref $collector eq $cpan_meta_pre) { + return $collector->with_merged_prereqs( + CPAN::Meta::Prereqs->new( $prereqs ) + ); + } + + # Raw hashrefs + for my $phase ( keys %$prereqs ) { + for my $type ( keys %{ $prereqs->{$phase} } ) { + for my $module ( keys %{ $prereqs->{$phase}{$type} } ) { + $collector->{$phase}{$type}{$module} = $prereqs->{$phase}{$type}{$module}; + } + } + } + + return $collector; +} + +my @include = qw( + +); + +my @exclude = qw( + +); + +# Add static prereqs to the included modules list +my $static_prereqs = do './t/00-report-prereqs.dd'; + +# Merge all prereqs (either with ::Prereqs or a hashref) +my $full_prereqs = _merge_prereqs( + ( $HAS_CPAN_META ? $cpan_meta_pre->new : {} ), + $static_prereqs +); + +# Add dynamic prereqs to the included modules list (if we can) +my ($source) = grep { -f } 'MYMETA.json', 'MYMETA.yml'; +my $cpan_meta_error; +if ( $source && $HAS_CPAN_META + && (my $meta = eval { CPAN::Meta->load_file($source) } ) +) { + $full_prereqs = _merge_prereqs($full_prereqs, $meta->prereqs); +} +else { + $cpan_meta_error = $@; # capture error from CPAN::Meta->load_file($source) + $source = 'static metadata'; +} + +my @full_reports; +my @dep_errors; +my $req_hash = $HAS_CPAN_META ? $full_prereqs->as_string_hash : $full_prereqs; + +# Add static includes into a fake section +for my $mod (@include) { + $req_hash->{other}{modules}{$mod} = 0; +} + +for my $phase ( qw(configure build test runtime develop other) ) { + next unless $req_hash->{$phase}; + next if ($phase eq 'develop' and not $ENV{AUTHOR_TESTING}); + + for my $type ( qw(requires recommends suggests conflicts modules) ) { + next unless $req_hash->{$phase}{$type}; + + my $title = ucfirst($phase).' '.ucfirst($type); + my @reports = [qw/Module Want Have/]; + + for my $mod ( sort keys %{ $req_hash->{$phase}{$type} } ) { + next if $mod eq 'perl'; + next if grep { $_ eq $mod } @exclude; + + my $file = $mod; + $file =~ s{::}{/}g; + $file .= ".pm"; + my ($prefix) = grep { -e File::Spec->catfile($_, $file) } @INC; + + my $want = $req_hash->{$phase}{$type}{$mod}; + $want = "undef" unless defined $want; + $want = "any" if !$want && $want == 0; + + my $req_string = $want eq 'any' ? 'any version required' : "version '$want' required"; + + if ($prefix) { + my $have = MM->parse_version( File::Spec->catfile($prefix, $file) ); + $have = "undef" unless defined $have; + push @reports, [$mod, $want, $have]; + + if ( $DO_VERIFY_PREREQS && $HAS_CPAN_META && $type eq 'requires' ) { + if ( $have !~ /\A$lax_version_re\z/ ) { + push @dep_errors, "$mod version '$have' cannot be parsed ($req_string)"; + } + elsif ( ! $full_prereqs->requirements_for( $phase, $type )->accepts_module( $mod => $have ) ) { + push @dep_errors, "$mod version '$have' is not in required range '$want'"; + } + } + } + else { + push @reports, [$mod, $want, "missing"]; + + if ( $DO_VERIFY_PREREQS && $type eq 'requires' ) { + push @dep_errors, "$mod is not installed ($req_string)"; + } + } + } + + if ( @reports ) { + push @full_reports, "=== $title ===\n\n"; + + my $ml = _max( map { length $_->[0] } @reports ); + my $wl = _max( map { length $_->[1] } @reports ); + my $hl = _max( map { length $_->[2] } @reports ); + + if ($type eq 'modules') { + splice @reports, 1, 0, ["-" x $ml, "", "-" x $hl]; + push @full_reports, map { sprintf(" %*s %*s\n", -$ml, $_->[0], $hl, $_->[2]) } @reports; + } + else { + splice @reports, 1, 0, ["-" x $ml, "-" x $wl, "-" x $hl]; + push @full_reports, map { sprintf(" %*s %*s %*s\n", -$ml, $_->[0], $wl, $_->[1], $hl, $_->[2]) } @reports; + } + + push @full_reports, "\n"; + } + } +} + +if ( @full_reports ) { + diag "\nVersions for all modules listed in $source (including optional ones):\n\n", @full_reports; +} + +if ( $cpan_meta_error || @dep_errors ) { + diag "\n*** WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING ***\n"; +} + +if ( $cpan_meta_error ) { + my ($orig_source) = grep { -f } 'MYMETA.json', 'MYMETA.yml'; + diag "\nCPAN::Meta->load_file('$orig_source') failed with: $cpan_meta_error\n"; +} + +if ( @dep_errors ) { + diag join("\n", + "\nThe following REQUIRED prerequisites were not satisfied:\n", + @dep_errors, + "\n" + ); +} + +pass; + +# vim: ts=4 sts=4 sw=4 et: diff --git a/t/client.t b/t/client.t new file mode 100755 index 0000000..4775b70 --- /dev/null +++ b/t/client.t @@ -0,0 +1,165 @@ +#!/usr/bin/env perl + +use warnings; +use strict; + +use FindBin '$Bin'; +use lib "$Bin/lib"; + +use Test::Exception; +use Test::More; + +use Future; +use GraphQL::Client; +use MockTransport; + +subtest 'transport' => sub { + my $client = GraphQL::Client->new(class => 'http'); + isa_ok($client->transport, 'GraphQL::Client::http', 'decide transport from class'); + + $client = GraphQL::Client->new(url => 'https://localhost:4000/graphql'); + isa_ok($client->transport, 'GraphQL::Client::http', 'decide transport from url'); + + $client = GraphQL::Client->new(class => 'not a real class'); + is($client->class, 'not a real class', 'class constructor works'); + throws_ok { $client->transport } qr/^Failed to load transport/, 'throws if invalid transport'; +}; + +subtest 'request to transport' => sub { + my $mock = MockTransport->new; + my $client = GraphQL::Client->new(transport => $mock); + + $client->execute('{hello}'); + my $req = ($mock->requests)[-1]; + is_deeply($req->[0], { + query => '{hello}', + }, 'query is passed to transport'); + + $client->execute('{hello}', {foo => 'bar'}); + $req = ($mock->requests)[-1]; + is_deeply($req->[0], { + query => '{hello}', + variables => {foo => 'bar'}, + }, 'vars passed to transport'); + + $client->execute('{hello}', {foo => 'bar'}, 'opname'); + $req = ($mock->requests)[-1]; + is_deeply($req->[0], { + query => '{hello}', + variables => {foo => 'bar'}, + operationName => 'opname', + }, 'operationName passed to transport'); + + $client->execute('{hello}', {foo => 'bar'}, 'opname', {baz => 'qux'}); + $req = ($mock->requests)[-1]; + is_deeply($req->[1], { + baz => 'qux', + }, 'transport options passed to transport'); + + $client->execute('{hello}', {foo => 'bar'}, {baz => 'qux'}); + $req = ($mock->requests)[-1]; + is_deeply($req->[1], { + baz => 'qux', + }, 'operation name can be ommitted with transport options'); +}; + +subtest 'success response' => sub { + my $mock = MockTransport->new; + my $client = GraphQL::Client->new(transport => $mock); + + $mock->response({ + response => { + data => { + hello => 'Hello world!', + }, + }, + }); + my $resp = $client->execute('{hello}'); + is_deeply($resp, { + data => {hello => 'Hello world!'}, + }, 'response is packed') or diag explain $resp; + { + local $client->{unpack} = 1; + my $resp = $client->execute('{hello}'); + is_deeply($resp, { + hello => 'Hello world!', + }, 'success response is unpacked') or diag explain $resp; + }; + + $mock->response(Future->done({ + response => { + data => { + hello => 'Hello world!', + }, + }, + })); + my $f = $client->execute('{hello}'); + is_deeply($f->get, { + data => {hello => 'Hello world!'}, + }, 'future response is packed') or diag explain $f->get; + { + local $client->{unpack} = 1; + my $f = $client->execute('{hello}'); + is_deeply($f->get, { + hello => 'Hello world!', + }, 'future success response is unpacked') or diag explain $f->get; + }; +}; + +subtest 'response with errors' => sub { + my $mock = MockTransport->new; + my $client = GraphQL::Client->new(transport => $mock); + + $mock->response({ + response => { + data => { + hello => 'Hello world!', + }, + errors => [ + { + message => 'Uh oh', + }, + ], + }, + }); + my $resp = $client->execute('{hello}'); + is_deeply($resp, { + data => {hello => 'Hello world!'}, + errors => [{message => 'Uh oh'}], + }, 'response is packed') or diag explain $resp; + { + local $client->{unpack} = 1; + throws_ok { $client->execute('{hello}') } qr/^Uh oh$/, 'error response thrown'; + my $err = $@; + is($err->error, 'Uh oh', 'error message is from first error'); + is($err->type, 'graphql', 'error type is "graphql"'); + my $resp = $err->{response}; + is_deeply($resp, { + data => {hello => 'Hello world!'}, + errors => [{message => 'Uh oh'}], + }, 'error includes the response') or diag explain $resp; + }; + + $mock->response({ + response => undef, + error => 'Transport error', + details => { + foo => 'bar', + }, + }); + $resp = $client->execute('{hello}'); + is_deeply($resp, { + errors => [{message => 'Transport error'}], + }, 'unpacked response munges error into response') or diag explain $resp; + { + local $client->{unpack} = 1; + throws_ok { $client->execute('{hello}') } qr/^Transport error$/, 'error response thrown'; + my $err = $@; + my $resp = $err->{response}; + is_deeply($resp, { + errors => [{message => 'Transport error'}], + }, 'error includes the constructed response') or diag explain $resp; + }; +}; + +done_testing; diff --git a/t/http.t b/t/http.t new file mode 100755 index 0000000..61a9ccf --- /dev/null +++ b/t/http.t @@ -0,0 +1,217 @@ +#!/usr/bin/env perl + +use warnings; +use strict; + +use FindBin '$Bin'; +use lib "$Bin/lib"; + +use Test::Deep; +use Test::Exception; +use Test::More; + +use Future; +use GraphQL::Client::http; + +HTTP::AnyUA->register_backend(MockUA => '+MockUA'); + +my $URL = 'http://localhost:4000/graphql'; + +subtest 'attributes' => sub { + my $http = GraphQL::Client::http->new; + + is($http->method, 'POST', 'default method is POST'); + is($http->url, undef, 'default url is undefined'); + + $http = GraphQL::Client::http->new(method => 'HEAD', url => $URL); + + is($http->method, 'HEAD', 'method getter returns correctly'); + is($http->url, $URL, 'url getter returns correctly'); +}; + +subtest 'bad arguments to execute' => sub { + my $http = GraphQL::Client::http->new(ua => 'MockUA'); + my $mock = $http->any_ua->backend; + + throws_ok { + $http->execute('blah'); + } qr/^Usage:/, 'first argument must be a hashref'; + + throws_ok { + $http->execute({}); + } qr/^Request must have a query/, 'request must have a query'; + + throws_ok { + $http->execute({query => '{hello}'}); + } qr/^URL must be provided/, 'request must have a URL'; +}; + +subtest 'POST request' => sub { + my $http = GraphQL::Client::http->new(ua => 'MockUA', url => $URL); + my $mock = $http->any_ua->backend; + + my $resp = $http->execute({ + query => '{hello}', + }); + my $req = ($mock->requests)[-1]; + + is($req->[0], 'POST', 'method is POST'); + is($req->[2]{content}, '{"query":"{hello}"}', 'encoded body as JSON'); + is($req->[2]{headers}{'content-type'}, 'application/json', 'set content-type to json'); +}; + +subtest 'GET request' => sub { + my $http = GraphQL::Client::http->new(ua => 'MockUA', url => $URL); + my $mock = $http->any_ua->backend; + + $http->execute({ + query => '{hello}', + }, { + method => 'GET', + }); + my $req = ($mock->requests)[-1]; + + is($req->[0], 'GET', 'method is GET'); + is($req->[1], "$URL?query=%7Bhello%7D", 'encoded query in params'); + is($req->[2]{content}, undef, 'no content'); + + $http->execute({ + query => '{hello}', + }, { + method => 'GET', + url => "$URL?foo=bar", + }); + $req = ($mock->requests)[-1]; + + is($req->[1], "$URL?foo=bar&query=%7Bhello%7D", 'encoded query in params with existing param'); +}; + +subtest 'plain response' => sub { + my $http = GraphQL::Client::http->new(ua => 'MockUA', url => $URL); + my $mock = $http->any_ua->backend; + + $mock->response({ + content => '{"data":{"foo":"bar"}}', + reason => 'OK', + status => 200, + success => 1, + }); + my $r = $http->execute({query => '{hello}'}); + my $expected = { + response => { + data => {foo => 'bar'}, + }, + details => { + http_response => $mock->response, + }, + }; + is_deeply($r, $expected, 'success response') or diag explain $r; + + $mock->response({ + content => '{"data":{"foo":"bar"},"errors":[{"message":"uh oh"}]}', + reason => 'OK', + status => 200, + success => 1, + }); + $r = $http->execute({query => '{hello}'}); + $expected = { + response => { + data => {foo => 'bar'}, + errors => [{message => 'uh oh'}], + }, + details => { + http_response => $mock->response, + }, + }; + is_deeply($r, $expected, 'response with graphql errors') or diag explain $r; + + $mock->response({ + content => 'The agent failed', + reason => 'Internal Exception', + status => 599, + success => '', + }); + my $resp = $http->execute({query => '{hello}'}); + $expected = { + error => 'HTTP transport returned 599 (Internal Exception): The agent failed', + response => undef, + details => { + http_response => $mock->response, + }, + }; + is_deeply($resp, $expected, 'response with http error') or diag explain $resp; + + $mock->response({ + content => 'not json', + reason => 'OK', + status => 200, + success => 1, + }); + $r = $http->execute({query => '{hello}'}); + $expected = { + error => re('^HTTP transport failed to decode response:'), + response => undef, + details => { + http_response => $mock->response, + }, + }; + cmp_deeply($r, $expected, 'response with invalid response') or diag explain $r; +}; + +subtest 'future response' => sub { + my $http = GraphQL::Client::http->new(ua => 'MockUA', url => $URL); + my $mock = $http->any_ua->backend; + + $mock->response(Future->done({ + content => '{"data":{"foo":"bar"}}', + reason => 'OK', + status => 200, + success => 1, + })); + my $f = $http->execute({query => '{hello}'}); + my $expected = { + response => { + data => {foo => 'bar'}, + }, + details => { + http_response => $mock->response->get, + }, + }; + is_deeply($f->get, $expected, 'success response') or diag explain $f->get; + + $mock->response(Future->done({ + content => '{"data":{"foo":"bar"},"errors":[{"message":"uh oh"}]}', + reason => 'OK', + status => 200, + success => 1, + })); + $f = $http->execute({query => '{hello}'}); + $expected = { + response => { + data => {foo => 'bar'}, + errors => [{message => 'uh oh'}], + }, + details => { + http_response => $mock->response->get, + }, + }; + is_deeply($f->get, $expected, 'response with graphql errors') or diag explain $f->get; + + $mock->response(Future->fail({ + content => 'The agent failed', + reason => 'Internal Exception', + status => 599, + success => '', + })); + $expected = { + error => 'HTTP transport returned 599 (Internal Exception): The agent failed', + response => undef, + details => { + http_response => ($mock->response->failure)[0], + }, + }; + $f = $http->execute({query => '{hello}'}); + is_deeply($f->get, $expected, 'response with http error') or diag explain $f->get; +}; + +done_testing; diff --git a/t/https.t b/t/https.t new file mode 100755 index 0000000..c125a50 --- /dev/null +++ b/t/https.t @@ -0,0 +1,13 @@ +#!/usr/bin/env perl + +use warnings; +use strict; + +use Test::More; + +use GraphQL::Client::https; + +isa_ok('GraphQL::Client::https', 'GraphQL::Client::http'); +can_ok('GraphQL::Client::https', qw(new execute)); + +done_testing; diff --git a/t/lib/MockTransport.pm b/t/lib/MockTransport.pm new file mode 100644 index 0000000..7e4159f --- /dev/null +++ b/t/lib/MockTransport.pm @@ -0,0 +1,38 @@ +package MockTransport; +# ABSTRACT: A backend for testing HTTP::AnyUA + +use warnings; +use strict; + +sub new { bless {}, shift } + +=method response + + $response = $backend->response; + $response = $backend->response($response); + +Get and set the response hashref or L that this backend will always respond with. + +=cut + +sub response { @_ == 2 ? $_[0]->{response} = pop : $_[0]->{response} } + +=method requests + + @requests = $backend->requests; + +Get the requests the backend has handled so far. + +=cut + +sub requests { @{$_[0]->{requests} || []} } + +sub execute { + my $self = shift; + + push @{$self->{requests} ||= []}, [@_]; + + return $self->response; +} + +1; diff --git a/t/lib/MockUA.pm b/t/lib/MockUA.pm new file mode 100644 index 0000000..3ee2d70 --- /dev/null +++ b/t/lib/MockUA.pm @@ -0,0 +1,48 @@ +package MockUA; +# ABSTRACT: HTTP::AnyUA backend for testing GraphQL::Client::http + +use warnings; +use strict; + +use Scalar::Util qw(blessed); +use namespace::clean; + +use parent 'HTTP::AnyUA::Backend'; + +=method response + + $response = $backend->response; + $response = $backend->response($response); + +Get and set the response hashref or L that this backend will always respond with. + +=cut + +sub response { @_ == 2 ? $_[0]->{response} = pop : $_[0]->{response} } + +=method requests + + @requests = $backend->requests; + +Get the requests the backend has handled so far. + +=cut + +sub requests { @{$_[0]->{requests} || []} } + +sub response_is_future { blessed($_[0]->{response}) && $_[0]->{response}->isa('Future') } + +sub request { + my $self = shift; + + push @{$self->{requests} ||= []}, [@_]; + + return $self->response || { + success => '', + status => 599, + reason => 'Internal Exception', + content => "No response mocked.\n", + }; +} + +1; diff --git a/xt/author/clean-namespaces.t b/xt/author/clean-namespaces.t new file mode 100644 index 0000000..36387da --- /dev/null +++ b/xt/author/clean-namespaces.t @@ -0,0 +1,11 @@ +use strict; +use warnings; + +# this test was generated with Dist::Zilla::Plugin::Test::CleanNamespaces 0.006 + +use Test::More 0.94; +use Test::CleanNamespaces 0.15; + +subtest all_namespaces_clean => sub { all_namespaces_clean() }; + +done_testing; diff --git a/xt/author/critic.t b/xt/author/critic.t new file mode 100644 index 0000000..80ccdad --- /dev/null +++ b/xt/author/critic.t @@ -0,0 +1,7 @@ +#!perl + +use strict; +use warnings; + +use Test::Perl::Critic (-profile => "perlcritic.rc") x!! -e "perlcritic.rc"; +all_critic_ok(); diff --git a/xt/author/eol.t b/xt/author/eol.t new file mode 100644 index 0000000..aae18ba --- /dev/null +++ b/xt/author/eol.t @@ -0,0 +1,36 @@ +use strict; +use warnings; + +# this test was generated with Dist::Zilla::Plugin::Test::EOL 0.19 + +use Test::More 0.88; +use Test::EOL; + +my @files = ( + 'bin/graphql', + 'lib/GraphQL/Client.pm', + 'lib/GraphQL/Client/http.pm', + 'lib/GraphQL/Client/https.pm', + 't/00-compile.t', + 't/00-report-prereqs.dd', + 't/00-report-prereqs.t', + 't/client.t', + 't/http.t', + 't/https.t', + 't/lib/MockTransport.pm', + 't/lib/MockUA.pm', + 'xt/author/clean-namespaces.t', + 'xt/author/critic.t', + 'xt/author/eol.t', + 'xt/author/minimum-version.t', + 'xt/author/no-tabs.t', + 'xt/author/pod-coverage.t', + 'xt/author/pod-syntax.t', + 'xt/author/portability.t', + 'xt/release/consistent-version.t', + 'xt/release/cpan-changes.t', + 'xt/release/distmeta.t' +); + +eol_unix_ok($_, { trailing_whitespace => 1 }) foreach @files; +done_testing; diff --git a/xt/author/minimum-version.t b/xt/author/minimum-version.t new file mode 100644 index 0000000..277e084 --- /dev/null +++ b/xt/author/minimum-version.t @@ -0,0 +1,6 @@ +use strict; +use warnings; + +use Test::More; +use Test::MinimumVersion; +all_minimum_version_ok( qq{5.10.1} ); diff --git a/xt/author/no-tabs.t b/xt/author/no-tabs.t new file mode 100644 index 0000000..f70f55c --- /dev/null +++ b/xt/author/no-tabs.t @@ -0,0 +1,36 @@ +use strict; +use warnings; + +# this test was generated with Dist::Zilla::Plugin::Test::NoTabs 0.15 + +use Test::More 0.88; +use Test::NoTabs; + +my @files = ( + 'bin/graphql', + 'lib/GraphQL/Client.pm', + 'lib/GraphQL/Client/http.pm', + 'lib/GraphQL/Client/https.pm', + 't/00-compile.t', + 't/00-report-prereqs.dd', + 't/00-report-prereqs.t', + 't/client.t', + 't/http.t', + 't/https.t', + 't/lib/MockTransport.pm', + 't/lib/MockUA.pm', + 'xt/author/clean-namespaces.t', + 'xt/author/critic.t', + 'xt/author/eol.t', + 'xt/author/minimum-version.t', + 'xt/author/no-tabs.t', + 'xt/author/pod-coverage.t', + 'xt/author/pod-syntax.t', + 'xt/author/portability.t', + 'xt/release/consistent-version.t', + 'xt/release/cpan-changes.t', + 'xt/release/distmeta.t' +); + +notabs_ok($_) foreach @files; +done_testing; diff --git a/xt/author/pod-coverage.t b/xt/author/pod-coverage.t new file mode 100644 index 0000000..66b3b64 --- /dev/null +++ b/xt/author/pod-coverage.t @@ -0,0 +1,7 @@ +#!perl +# This file was automatically generated by Dist::Zilla::Plugin::PodCoverageTests. + +use Test::Pod::Coverage 1.08; +use Pod::Coverage::TrustPod; + +all_pod_coverage_ok({ coverage_class => 'Pod::Coverage::TrustPod' }); diff --git a/xt/author/pod-syntax.t b/xt/author/pod-syntax.t new file mode 100644 index 0000000..e563e5d --- /dev/null +++ b/xt/author/pod-syntax.t @@ -0,0 +1,7 @@ +#!perl +# This file was automatically generated by Dist::Zilla::Plugin::PodSyntaxTests. +use strict; use warnings; +use Test::More; +use Test::Pod 1.41; + +all_pod_files_ok(); diff --git a/xt/author/portability.t b/xt/author/portability.t new file mode 100644 index 0000000..c531252 --- /dev/null +++ b/xt/author/portability.t @@ -0,0 +1,10 @@ +use strict; +use warnings; + +use Test::More; + +eval 'use Test::Portability::Files'; +plan skip_all => 'Test::Portability::Files required for testing portability' + if $@; + +run_tests(); diff --git a/xt/release/consistent-version.t b/xt/release/consistent-version.t new file mode 100644 index 0000000..7f200c5 --- /dev/null +++ b/xt/release/consistent-version.t @@ -0,0 +1,10 @@ +use strict; +use warnings; + +use Test::More; + +eval "use Test::ConsistentVersion"; +plan skip_all => "Test::ConsistentVersion required for this test" + if $@; + +Test::ConsistentVersion::check_consistent_versions(); diff --git a/xt/release/cpan-changes.t b/xt/release/cpan-changes.t new file mode 100644 index 0000000..286005a --- /dev/null +++ b/xt/release/cpan-changes.t @@ -0,0 +1,10 @@ +use strict; +use warnings; + +# this test was generated with Dist::Zilla::Plugin::Test::CPAN::Changes 0.012 + +use Test::More 0.96 tests => 1; +use Test::CPAN::Changes; +subtest 'changes_ok' => sub { + changes_file_ok('Changes'); +}; diff --git a/xt/release/distmeta.t b/xt/release/distmeta.t new file mode 100644 index 0000000..c2280dc --- /dev/null +++ b/xt/release/distmeta.t @@ -0,0 +1,6 @@ +#!perl +# This file was automatically generated by Dist::Zilla::Plugin::MetaTests. + +use Test::CPAN::Meta; + +meta_yaml_ok(); -- 2.45.2