GraphQL

 view release on metacpan or  search on metacpan

lib/GraphQL.pm  view on Meta::CPAN

GraphQL - Perl implementation of GraphQL

=cut

our $VERSION = '0.54';

=begin markdown

# PROJECT STATUS

| OS      |  Build status |
|:-------:|--------------:|
| Linux   | [![Build Status](https://travis-ci.org/graphql-perl/graphql-perl.svg?branch=master)](https://travis-ci.org/graphql-perl/graphql-perl) |

[![CPAN version](https://badge.fury.io/pl/GraphQL.svg)](https://metacpan.org/pod/GraphQL) [![Coverage Status](https://coveralls.io/repos/github/graphql-perl/graphql-perl/badge.svg?branch=master)](https://coveralls.io/github/graphql-perl/graphql-perl?...

=end markdown

=head1 SYNOPSIS

  use GraphQL::Schema;
  use GraphQL::Type::Object;
  use GraphQL::Type::Scalar qw($String);
  use GraphQL::Execution qw(execute);

  my $schema = GraphQL::Schema->from_doc(<<'EOF');
  type Query {
    helloWorld: String
  }
  EOF
  post '/graphql' => sub {
    send_as JSON => execute(
      $schema,
      body_parameters->{query},
      { helloWorld => 'Hello world!' },
      undef,
      body_parameters->{variables},
      body_parameters->{operationName},
      undef,
    );
  };

The above is from L<the sample Dancer 2 applet|https://github.com/graphql-perl/sample-dancer2>.

=head1 DESCRIPTION

This module is a port of the GraphQL reference implementation,
L<graphql-js|https://github.com/graphql-js/graphql-js>, to Perl 5.

It now supports Promises, allowing asynchronous operation. See
L<Mojolicious::Plugin::GraphQL> for an example of how to take advantage
of this.

As of 0.39, supports GraphQL subscriptions.

See L<GraphQL::Type> for description of how to create GraphQL types.

=head2 Introduction to GraphQL

GraphQL is a technology that lets clients talk to APIs via a single
endpoint, which acts as a single "source of the truth". This means clients
do not need to seek the whole picture from several APIs. Additionally,
it makes this efficient in network traffic, time, and programming effort:

=over

=item Network traffic

The request asks for exactly what it wants, which it gets, and no
more. No wasted traffic.

=item Time

It gets all the things it needs in one go, including any connected
resources, so it does not need to make several requests to fill its
information requirement.

=item Programming effort

With "fragments" that can be attached to user-interface components,
keeping track of what information a whole page needs to request can be
automated. See L<Relay|https://facebook.github.io/relay/> or
L<Apollo|http://dev.apollodata.com/> for more on this.

=back

=head2 Basic concepts

GraphQL implements a system featuring a L<schema|GraphQL::Schema>,
which features various classes of L<types|GraphQL::Type>, some of which
are L<objects|GraphQL::Type::Object>. Special objects provide the roots
of queries (mandatory), and mutations and subscriptions (both optional).

Objects have fields, each of which can be specified to take arguments,
and which have a return type. These are effectively the properties and/or
methods on the type. If they return an object, then a query can specify
subfields of that object, and so on - as alluded to in the "time-saving"
point above.

For more, see the JavaScript tutorial in L</"SEE ALSO">.

=head2 Hooking your system up to GraphQL

You will need to decide how to model your system in GraphQL terms. This
will involve deciding on what L<output object types|GraphQL::Type::Object>
you have, what fields they have, and what arguments and return-types
those fields have.

Additionally, you will need to design mutations if you want to be able
to update/create/delete data. This requires some thought for return types,
to ensure you can get all the information you need to proceed to avoid
extra round-trips.

The easiest way to achieve these things is to make a
L<GraphQL::Plugin::Convert> subclass, to encapsulate the specifics of
your system. See the documentation for further information.

Finally, you should consider whether you need "subscriptions". These
are designed to hook into WebSockets. Apollo has a L<JavaScript
module|https://github.com/apollographql/graphql-subscriptions> for this.



( run in 0.569 second using v1.01-cache-2.11-cpan-39bf76dae61 )