C# Library and ms build tasks for managing .graphql files within a c# project. This project will create c# files using quicktype for any .graphql files in your project which will use the GraphQL.Client library to request the results
Note: This package only works .net versions which support .net standard. You will also need to use dependency injection.
- Acquire the schema.json file for the graphql server you want to request data from
- Add that file to your project
- Add the GraphQL.Typed.Client nuget package
- In your csproj file add a
<GraphQLSchema>
tag inside the first property group, inside that tag add the path to your schema file - Optional: If using JetBrains Rider add
<GraphQLSetupRiderFileWatcher>True</GraphQLSetupRiderFileWatcher>
inside the first property group to auto generate a file watcher - Optional: If using a plugin which supports .graphqlconfig files add
<GraphQLSetupConfig>True</GraphQLSetupConfig>
inside the first property group to auto generate one - Create a class which implements
IGraphQlClientBuilder
. This class should configure the builder to correctly hit the server that your schema belongs to - Add that class using the
IGraphQlClientBuilder
interface to your injected services - Create a .graphql file in your project with a graph query or mutation in it.
- This file should be named {DescriptiveName}Query.graphql or {DescriptiveName}Mutation.graphql
- The query or mutation should be named the same as the filename
- Hit build on your project (or hit save if you are in Rider and added the element on step 6)
- You will now have a cs file containing a class/interface matching the graphql file name, it will need to be injected into your services
- Add the new class or interface to the consuming class and call the
Fetch
method
This package can also be used to produce a nuget package which will generate types for an included schema.
- Create a new .net standard library project
- Add the GraphQL.Typed.Client nuget package
- Edit the
PackageReference
tag addingPrivateAssets="none"
- Add a build folder
- Add a {Package.Name}.targets file. This file MUST have the same file name as the resulting package name
- Add the following to the targets file
<Project> <PropertyGroup> <GraphQLSchema>$(MSBuildThisFileDirectory)\..\schema.json</GraphQLSchema> </PropertyGroup> <PropertyGroup> <RelativeSchemaPath>$([MSBuild]::MakeRelative('$(MSBuildProjectDirectory)', '$(GraphQLSchema)'))</RelativeSchemaPath> </PropertyGroup> <ItemGroup> <None Include="$(RelativeSchemaPath)" Visible="false"/> </ItemGroup> </Project>
- Add your schma.json file to your project
- Add the following to your csproj file, replacing
{Package.Name}
with the name of your packageNote: If you see either file appear twice in the tree remove any other elements which refer to those files<ItemGroup> <None Include="build\{Package.Name}.targets"> <Pack>true</Pack> <PackagePath>build</PackagePath> </None> </ItemGroup> <ItemGroup> <None Include="schema.json"> <Pack>true</Pack> <PackagePath>.</PackagePath> </None> </ItemGroup>
- Add the Nuget attributes to your csproj and hit build. You should now have an installable package which will configure projects to build cs files off graphql files
All Pull Requests should be available as a prerelease on nuget.org. To create an official release create a release in github with the new version number, after the build completes it will be uploaded to nuget.org