FSharp.Data.GraphQL
F# implementation of Facebook GraphQL query language specification.

Installing project templates
Type the following commands to install the template for creating web applications:
From GitHub:
dotnet new -i "FSharp.Data.GraphQL::2.0.0-ci-*" --nuget-source "https://nuget.pkg.github.com/fsprojects/index.json"
From NuGet:
dotnet new -i "FSharp.Data.GraphQL::2.0.0"
Quick start
open FSharp.Data.GraphQL
open FSharp.Data.GraphQL.Types
type Person =
{ FirstName: string
LastName: string }
// Define GraphQL type
let PersonType = Define.Object(
name = "Person",
fields = [
// Property resolver will be auto-generated
Define.AutoField("firstName", StringType)
// Asynchronous explicit member resolver
Define.AsyncField("lastName", StringType, resolve = fun context person -> async { return person.LastName })
])
// Include person as a root query of a schema
let schema = Schema(query = PersonType)
// Create an Exector for the schema
let executor = Executor(schema)
// Retrieve person data
let johnSnow = { FirstName = "John"; LastName = "Snow" }
let reply = executor.AsyncExecute(Parser.parse "{ firstName, lastName }", johnSnow) |> Async.RunSynchronously
// #> { data: { "firstName", "John", "lastName", "Snow" } }
It's type safe. Things like invalid fields or invalid return types will be checked at compile time.
ASP.NET / Giraffe / WebSocket (for GraphQL subscriptions) usage
See the AspNetCore/README.md
Demos
GraphiQL client
Go to the GraphiQL sample directory. In order to run it, build and run the Star Wars API sample project with Debug settings - this will create a Giraffe server compatible with the GraphQL spec, running on port 8086. Then what you need is to run node.js graphiql frontend. To do so, run npm i to get all dependencies, and then run npm run serve | npm run dev - this will start a webpack server running on http://localhost:8090/ . Visit this link, and GraphiQL editor should appear. You may try it by applying following query:
{
hero(id:"1000") {
id,
name,
appearsIn,
homePlanet,
friends {
... on Human {
name
}
... on Droid {
name
}
}
}
}
Stream features
The stream directive now has additional features, like batching (buffering) by interval and/or batch size. To make it work, a custom stream directive must be placed inside the SchemaConfig.Directives list, this custom directive containing two optional arguments called interval and preferredBatchSize:
let customStreamDirective =
let args = [|
Define.Input(
"interval",
Nullable IntType,
defaultValue = Some 2000,
description = "An optional argument used to buffer stream results. ")
Define.Input(
"preferredBatchSize",
Nullable IntType,
defaultValue = None,
description = "An optional argument used to buffer stream results. ") |]
{ StreamDirective with Args = args }
let schemaConfig =
{ SchemaConfig.Default with
Directives = [
IncludeDirective
SkipDirective
DeferDirective
customStreamDirective
LiveDirective ] }
This boilerplate code can be easily reduced with a built-in implementation:
let streamOptions =
{ Interval = ValueSome 2000; PreferredBatchSize = ValueNone }
let schemaConfig =
SchemaConfig.DefaultWithBufferedStream(streamOptions)
Live queries
The live directive is now supported by the server component. To support live queries, each field of each type of the schema needs to be configured as a live field. This is done by using ILiveFieldSubscription and ILiveQuerySubscriptionProvider, which can be configured in the SchemaConfig:
type ILiveFieldSubscription =
interface
abstract member Identity : obj -> obj
abstract member TypeName : string
abstract member FieldName : string
end
and ILiveFieldSubscription<'Object, 'Identity> =
interface
inherit ILiveFieldSubscription
abstract member Identity : 'Object -> 'Identity
end
and ILiveFieldSubscriptionProvider =
interface
abstract member HasSubscribers : string -> string -> bool
abstract member IsRegistered : string -> string -> bool
abstract member AsyncRegister : ILiveFieldSubscription -> Async<unit>
abstract member TryFind : string -> string -> ILiveFieldSubscription option
abstract member Add : obj -> string -> string -> IObservable<obj>
abstract member AsyncPublish<'T> : string -> string -> 'T -> Async<unit>
end
To set a field as a live field, call the Register extension method. Each subscription needs to know an object identity, so it must be configured on the Identity function of the ILiveFieldSubscription. Also, the name of the Type and the field inside the ObjectDef needs to be passed along:
let schemaConfig = SchemaConfig.Default
let schema = Schema(root, config = schemaConfig)
let subscription =
{ Identity = fun (x : Human) -> x.Id
TypeName = "Hero"
FieldName = "name" }
schemaConfig.LiveFieldSubscriptionProvider.Register subscription
With that, the field name of the hero is now able to go live, being updated to clients whenever it is queried with the live directive. To push updates to subscribers, just call Publish method, passing along the type name, the field name and the updated object:
let updatedHero = { hero with Name = "Han Solo - Test" }
schemaConfig.LiveFieldSubscriptionProvider.Publish "Hero" "name" updatedHero
Client Provider
Our client library now has a completely redesigned type provider. To start using it, you will first need access to the introspection schema for the server you are trying to connect. This can be done with the provider in one of two ways:
- Provide the URL to the desired GraphQL server (without any custom HTTP headers required). The provider will access the server, send an Introspection Query, and use the schema to provide the types used to make queries.
type MyProvider = GraphQLProvider<"http://some.graphqlserver.development.org">
- Provide an introspection json file to be used by the provider. Beware though that the introspection json should have all fields required by the provider. You can get the correct fields by running our standard introspection query on the desired server and saving it into a file on the same path as the project using the provider:
type MyProvider = GraphQLProvider<"swapi_schema.json">
From now on, you can start running queries and mutations:
let operation =
MyProvider.Operation<"""query q {
hero (id: "1001") {
name
appearsIn
homePlanet
friends {
edges {
node {
... on Human {
name
homePlanet
}
... on Droid {
name
primaryFunction
}
}
}
}
}
}""">()
// This is a instance of GraphQLProviderRuntimeContext.
// You can use it to provider a runtime URL to access your server,
// and optionally additional HTTP headers (auth headers, for example).
// If you use a local introspection file to parse the schema,
// The runtime context is mandatory.
let runtimeContext =
{ ServerUrl = "http://some.graphqlserver.production.org"
CustomHttpHeaders = None }
let result = operation.Run(runtimeContext)
// Query result objects have pretty-printing and structural equality.
printfn "Data: %A\n" result.Data
printfn "Errors: %A\n" result.Errors
printfn "Custom data: %A\n" result.CustomData
// Response from the server:
// Data: Some
// {Hero = Some
// {Name = Some "Darth Vader";
// AppearsIn = [|NewHope; Empire; Jedi|];
// HomePlanet = Some "Tatooine";
// Friends = {Edges = [|{Node = {Name = Some "Wilhuff Tarkin";
// HomePlanet = <null>;};}|];};};}
// Errors: <null>
// Custom data: map [("documentId", 1221427401)]
For more information about how to use the client provider, see the examples folder.
Middleware
You can create and use middleware on top of the Executor<'Root> object.
The query execution process through the use of the Executor involves three phases:
Schema compile phase: this phase happens when the Executor<'Root> class is instantiated. In this phase, the Schema map of types is used to build a field execute map, which contains all field definitions alongside their field resolution functions. This map is used later on in the planning and execution phases to retrieve the values of the queried fields of the schema.
Operation planning phase: this phase happens before running a query that has no execution plan. This phase is responsible for analyzing the AST document generated by the query, and building an ExecutionPlan to execute it.
Operation execution phase: this phase is the phase that executes the query. It needs an execution plan, so, it commonly happens after the operation planning phase.
All the phases wrap the needed data to do the phase job inside a context object. They are expressed internally by functions:
let internal compileSchema (ctx : SchemaCompileContext) : unit =
// ...
let internal planOperation (ctx: PlanningContext) : ExecutionPlan =
// ...
let internal executeOperation (ctx : ExecutionContext) : AsyncVal<GQLResponse> =
// ...
That way, in the compile schema phase, the schema is modified and execution maps are generated inside the SchemaCompileContext object. During the operation planning phase, values of the PlanningContext object are used to generate an execution plan, and finally, this plan is passed alongside other values in the ExecutionContext object to the operation execution phase, which finally uses them to execute the query and generate a GQLResponse.
With that being said, a middleware can be used to intercept each phase and customize them as necessary. Each middleware must be implemented as a function with a specific signature, and wrapped inside an IExecutorMiddleware interface:
type SchemaCompileMiddleware =
SchemaCompileContext -> (SchemaCompileContext -> unit) -> unit
type OperationPlanningMiddleware =
PlanningContext -> (PlanningContext -> ExecutionPlan) -> ExecutionPlan
type OperationExecutionMiddleware =
ExecutionContext -> (ExecutionContext -> AsyncVal<GQLResponse>) -> AsyncVal<GQLResponse>
type IExecutorMiddleware =
abstract CompileSchema : SchemaCompileMiddleware voption
abstract PostCompileSchema : SchemaPostCompileMiddleware voption
abstract PlanOperation : OperationPlanningMiddleware voption
abstract ExecuteOperationAsync : OperationExecutionMiddleware voption
Optionally, for ease of implementation, concrete class to derive from can be used, receiving only the optional sub-middleware functions in the constructor:
type ExecutorMiddleware([<Struct>] ?compile, [<Struct>] ?postCompile, [<Struct>] ?plan, [<Struct>] ?execute) =
interface IExecutorMiddleware with
member _.CompileSchema = compile
member _.PostCompileSchema = postCompile
member _.PlanOperation = plan
member _.ExecuteOperationAsync = execute
Each of the middleware functions act like an intercept function, with two parameters: the context of the phase, the function of the next middleware (or the actual phase itself, which is the last to run), and the return value. Those functions can be passed as an argument to the constructor of the Executor<'Root> object:
let middleware = [ ExecutorMiddleware(compile = compileFn, plan = planningFn, execute = executionFn) ]
let executor = Executor(schema, middleware)
A simple example of a practical middleware can be one that measures the time needed to plan a query. The results of which get returned as part of the Metadata of the planning context. The Metadata object is a Map<string, obj> implementation that acts like a bag of information to be passed through each phase, until it is returned inside the GQLResponse object. You can use it to thread custom information through middleware:
let planningMiddleware (ctx : PlanningContext) (next : PlanningContext -> ExecutionPlan) =
let watch = Stopwatch()
watch.Start()
let result = next ctx
watch.Stop()
let metadata = result.Metadata.Add("planningTime", watch.ElapsedMilliseconds)
{ result with Metadata = metadata }
Built-in middleware
There are some built-in middleware inside FSharp.Data.GraphQL.Server.Middleware package:
QueryWeightMiddleware
This middleware can be used to place weights on fields of the schema. Those weighted fields can now be used to protect the server from complex queries that could otherwise be used in DDOS attacks.
When defining a field, we use the extension method WithQueryWeight to place a weight on it:
let resolveFn (h : Human) =
h.Friends |> List.map getCharacter |> List.toSeq
let field =
Define.Field("friends", ListOf (Nullable CharacterType),
resolve = resolveFn).WithQueryWeight(0.5)