FSharp.Data.GraphQL


FSharp.Data.GraphQL

FSharp.Data.GraphQL is a client and server implementation of Facebook's GraphQL query language.

It's a standard created for building web service APIs and a runtime for defining those APIs in statically typed, well formed way. The core idea is to define web service in context of its capabilities in oposition to routees known from existing RESTful APIs. Capabilities are defined in form of GraphQL schema and describe all operations and data allowed to be requested by the client, without fragmenting it into particular routes.

The FSharp.Data.GraphQL library can be installed from NuGet on the server or a client:

1: 
2: 
PM> Install-Package FSharp.Data.GraphQL.Server -Pre
PM> Install-Package FSharp.Data.GraphQL.Client -Pre

Quick start

To use FSharp.Data.GraphQL on the server side, first define some data working as a source:

1: 
2: 
3: 
4: 
type Person = { FirstName: string; LastName: string }
let people = [ 
    { FirstName = "Jane"; LastName = "Milton" }
    { FirstName = "Travis"; LastName = "Smith" } ]

Then expose it through the schema - GraphQL language defines it's own type system, that can be integrated with any other programming language:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
open FSharp.Data.GraphQL
open FSharp.Data.GraphQL.Types

// GraphQL type definition for Person type
let Person = Define.Object("Person", [
    Define.Field("firstName", String, fun ctx p -> p.FirstName)
    Define.Field("lastName", String, fun ctx p -> p.LastName)  
])

// each schema must define so-called root query
let QueryRoot = Define.Object("Query", [
    Define.Field("people", ListOf Person, fun ctx () -> people)
])

// then initialize everything as part of schema
let schema = Schema(QueryRoot)

With schema create we are now able to respond to any incoming GraphQL queries:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
open FSharp.Data.GraphQL.Execution
let query = """
    query Example {
        people {
            firstName
        }
    }
    """
async {
    let! response = schema.AsyncExecute(query)
    printf "%A" response
}

More examples

For more examples, clone FSharp.Data.GraphQL github repository and see the samples folder. There, your can find:

  • A mandatory Star Wars schema introduction using GraphiQL client.
  • An example using RelayJS data structures (which this library supports).
  • A client example, using type providers to operate on any GraphQL schema available - worth noticing: it's compatbile with Fable compiler!
  • A fully fledged ToDo application, where FSharp.Data.GraphQL is used for both server and client.

Contributing and copyright

The project is hosted on GitHub where you can report issues, fork the project and submit pull requests. If you're adding a new public API, please also consider adding samples that can be turned into a documentation. You might also want to read the library design notes to understand how it works.

The library is available under Public Domain license, which allows modification and redistribution for both commercial and non-commercial purposes. For more information see the License file in the GitHub repository.

namespace Microsoft.FSharp
namespace Microsoft.FSharp.Data
type Person =
  {FirstName: string;
   LastName: string;}

Full name: index.Person
Person.FirstName: string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
Person.LastName: string
val people : Person list

Full name: index.people
Multiple items
val Person : obj

Full name: index.Person

--------------------
type Person =
  {FirstName: string;
   LastName: string;}

Full name: index.Person
module String

from Microsoft.FSharp.Core
val QueryRoot : obj

Full name: index.QueryRoot
val schema : obj

Full name: index.schema
val query : string

Full name: index.query
val async : AsyncBuilder

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
val response : obj
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
Fork me on GitHub