Giter Club home page Giter Club logo

perl6-graphql's Introduction

Perl 6 GraphQL

Build Status

A Perl 6 implementation of the GraphQL standard. GraphQL is a query language for APIs originally created by Facebook.

Intro

Before we get into all the details, here's the Perl 6 GraphQL "Hello World" hello.pl

use GraphQL;
use GraphQL::Server;

class Query
{
    method hello(--> Str) { 'Hello World' }
}

my $schema = GraphQL::Schema.new(Query);

GraphQL-Server($schema);

You can run this with a GraphQL query on the command line:

$ perl6 hello.pl --help
Usage:
  hello.pl <query> 
  hello.pl [--filename=<Str>] 
  hello.pl [--port=<Int>] 

$ perl6 hello.pl '{hello}'
{
  "data": {
    "hello": "Hello World"
  }
}

You can even ask for information about the schema and types:

$ perl6 hello.pl '{ __schema { queryType { name } } }'
{
  "data": {
    "__schema": {
      "queryType": {
        "name": "Query"
      }
    }
  }
}

$ perl6 hello.pl '{ __type(name: "Query") { fields { name type { name }}}}'
{
  "data": {
    "__type": {
      "fields": [
        {
          "name": "hello",
          "type": {
            "name": "String"
          }
        }
      ]
    }
  }
}

Running as a server

That's fine for the command line, but you can also easily wrap GraphQL into a web server to expose that API to external clients. GraphQL::Server uses the Perl 6 web framework Bailador to do that:

$ ./hello.pl
Entering the development dance floor: http://0.0.0.0:3000
[2016-12-21T13:02:38Z] Started HTTP server.

The server takes any GraphQL query sent with HTTP POST to /graphql, executes it against the GraphQL Schema, and returns the result in JSON.

There is one additional feature. If it receives a GET request to "/graphql", it sends back the GraphiQL graphical interactive in-browser GraphQL IDE.

You can use that to explore the schema (though the Hello World schema is very simple, that won't take long), and interactively construct and execute GraphQL queries.

More documentation

See eg/usersexample.md for a more complicated example.

See slides from a presentation about Perl 6 GraphQL at the Philadelphia Perl Mongers.

GraphQL Documentation

Copyright © 2017 United States Government as represented by the Administrator of the National Aeronautics and Space Administration. No copyright is claimed in the United States under Title 17, U.S.Code. All Other Rights Reserved.

perl6-graphql's People

Contributors

curttilmes avatar ugexe avatar

Watchers

Fernando Correa de Oliveira avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.