FSharp.Data.GraphQL is a client and server implementation of Facebook'sGraphQL query language.
It's a standard created for building web service APIs and a runtime for defining those APIs in statically typed, wellformed 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 theserver or aclient:
1:2: | PM>Install-PackageFSharp.Data.GraphQL.Server-PrePM>Install-PackageFSharp.Data.GraphQL.Client-Pre
|
To use FSharp.Data.GraphQL on the server side, first define some data working as a source:
1:2:3:4: | typePerson= {FirstName:string;LastName:string }letpeople= [ {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: | openFSharp.Data.GraphQLopenFSharp.Data.GraphQL.Types// GraphQL type definition for Person typeletPerson=Define.Object("Person", [Define.Field("firstName",String,functxp->p.FirstName)Define.Field("lastName",String,functxp->p.LastName) ])// each schema must define so-called root queryletQueryRoot=Define.Object("Query", [Define.Field("people",ListOfPerson,functx ()->people)])// then initialize everything as part of schemaletschema=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: | openFSharp.Data.GraphQL.Executionletquery=""" query Example { people { firstName } } """async {let!response=schema.AsyncExecute(query)printf"%A"response}
|
For more examples, cloneFSharp.Data.GraphQL github repository and see thesamples folder. There, your can find:
- A mandatory Star Wars schema introduction usingGraphiQL client.
- An example usingRelayJS data structures (which this library supports).
- A client example, using type providers to operate on any GraphQL schema available - worth noticing: it's compatbile withFable compiler!
- A fully fledged ToDo application, whereFSharp.Data.GraphQL is used for both server and client.
The project is hosted onGitHub where you canreport issues, forkthe project and submit pull requests. If you're adding a new public API, please alsoconsider addingsamples that can be turned into a documentation. You mightalso want to read thelibrary design notes to understand how it works.
The library is available under Public Domain license, which allows modification andredistribution for both commercial and non-commercial purposes. For more information see theLicense 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