- Notifications
You must be signed in to change notification settings - Fork27
A dotnet CLI to generate type-safe GraphQL clients for F# and Fable with automatic deserialization, static query verification and type checking
License
Zaid-Ajaj/Snowflaqe
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A dotnet CLI tool for generating type-safe GraphQL clients for F#.
- Static queryanalysis,verification andtype checking against a remote or local GraphQL schema.
- Generating idiomatic F# types from GraphQL queries
- Generating type-safeFable or F# GraphQL client project in a single command. This project can be then referenced by your application and will be ready to go.
- Supports GraphQL interfaces and unions
- Resolves type name collisions when re-using types in a single query
- Generates a HTTP client with functions that correspond to the query or mutation names. These functions handle exact JSON deserialization for the returned data.
- Tested with different GraphQL backends generated fromgraphql-dotnet,hasura,postgraphile andabsinthe.
Install as a global dotnet CLI tool
dotnet tool install snowflaqe -g
Create a JSON file calledsnowflaqe.json
with the following shape:
{ "schema": "<schema>", "queries": "<queries>", "project": "<project>", "output": "<output>" ["target"]: "<target>", ["errorType"]: <custom error type>, ["overrideClientName"]: "<clientName>", ["copyLocalLockFileAssemblies"]: <true | false>, ["emitMetadata"]: <true | false>, ["createProjectFile"]: <true | false>, ["normalizeEnumCases"]: <true | false>, ["asyncReturnType"]: <"async" | "task"> ["serializer"]: <"newtonsoft" | "system">}
Where
<schema>
can be one of:- A URL to the GraphQL backend
- A relative path to another JSON file containing the output of thestandard Introspection query which you can execute against the backend yourself (this allows for offline verification and type-checking)
- A relative path to a file with extension
.gql
or.graphql
containing the schema definitions and types
<queries>
is an absolute or relative path to a directory that contains*.gql
or*.graphql
files that contain individual GraphQL queries thatsnowflaqe
will run the verification against.<project>
is the name of the project will be generated.<output>
is an absolute or relative path to a directory where the project will be generated. It is recommended to use an empty directory relative to configuration file because each time you generate and regenarate the project, this directory is cleaned.<errorType>
optional custom error type to be generated. See below to learn more.<clientName>
optional name for theGraphqlClient
class which is{project}GraphqlClient
by default when you don't provide this property.<copyLocalLockFileAssemblies>
Adds the attribute to the generated F# project for scenarios where embedding the dependencies is required<emitMetadata>
when set totrue
, emits code generation metadata as comments into the output modules. The option is set tofalse
by default.<normalizeEnumCases>
determines whether enum case names should be normalized (true by default when omitted)createProjectFile
determines whether snowflaqe should output a.fsproj
file when set totrue
(default value) or output a.props
file instead when set tofalse
asyncReturnType
when targetting F# on dotnet, determines the output type of the client functions, whether they returnAsync<'T>
when this option is set to "async" or returnTask<'T>
when set to "task". This option is not compatible for Fable projects since Fable doesn't support tasks.serializer
: forfsharp
targets, determines whether to use Newtonsoft.Json (default) as the JSON serializer or System.Text.Json when set to "system".<target>
optional the code-generation target which can either befable
(default),fsharp
orshared
.
Using
shared
as a code generation target actually builds 3 projects! One contains just the types and can beshared across platforms. The other two reference this shared projects and implement Fable specific client and dotnet specific client, respectively.
After creating the configuration file. You cancd
your way to where you have the config file and run:
snowflaqe
which will by default only do static query verification and static type-checking against the<schema>
. You can alsoreference the configuration file in another directory via a relative path:
snowflaqe --config ./src/snowflaqe.json
In this case, the file doesn't necessarily have to be called
snowflaqe.json
.
snowflaqe --generatesnowflaqe --config ./path/to/snowflaqe.json --generate
Will generate a full project in the designatedoutput
directory. You can start using the generated project byreferencing it from another project using a reference as follows:
<ProjectReferenceInclude=".\path\to\generated\Project.fsproj" />
You can either do this manually or using an IDE such as Visual Studio or Rider that allow you toRight Click -> Add Existing Project
.
You don't need to reference extra packages or anything, once youdotnet restore
the generated project will pull in the packages that it requires.
It is worth mentioning that the generated project will targetnetstandard2.0
which means you can use anywhere in .NET Core or even in full .NET Framework v4.7.1+
Here are screenshots of how such generated project looks like. In this case, we have a project generated from the Github GraphQL API (Fable target):
Given the query
The corresponding query types are generated
All queries and mutations can be called from a dedicatedGraphqlClient
By default, the error type that is generated in the global types looks like this:
typeErrorType={ message:string}
This type is important because every request you make to the GraphQL backend returnsResult<Query, ErrorType list>
but the errors that come back are usually determined by the backend and not exposed through the schema. That is why you can customize this error type using theerrorType
configuration element:
{ "schema": "<schema>", "queries: "<queries>", "project": "<project>", "output": "<output>", "errorType": { "CustomErrorType": { "Message": "string" "Path": "string list" "RequestId": "string" } }}
which will generate:
typeCustomErrorType={ Message:string Path:string list RequestId:string}
There are a couple of features of the GraphQL specs whichsnowflaqe
doesn't (yet) know how to work with:
- Subscriptions
About
A dotnet CLI to generate type-safe GraphQL clients for F# and Fable with automatic deserialization, static query verification and type checking