This is a small application to generate GraphQL client C# code. It leverages GraphQLClientGenerator libraray to achive this. Following are the process associated with generating GraphQL client,
- Fetch the backend schema,
- Deserilize the schema to GraphQLClientGenerator's schema format and,
- Generate client out of the schema object.
Even though GraphQLClientGenerator library can fetch and serialize the schema in one go, it did not work with the APIM GraphQL endpoint, as it by default added charset as UTF-8, which resulted APIM endpoint prompting bad request error. The way around it was to use http client to directly fetch the schema leveraging Introspection. This way we make sure that the content header is set to "application/json", with no specification of charset to use.
For more info on Introspection follow these links,
By deserialization, we are just converting the json schema to GraphQLClientGenerator's Schema object, which can be used to generator the client class.
The deserilized schema is plugged into GraphQLClientGenerator's helper method to generate the client class.
Following are instruction on using the applicaiton,
Replace the endpoint placeholder in program.cs with your actual endpoint.
Go to folder ClientGenerator\GraphQLClientGenerator\GraphQLClientGeneratorConsole and run command,
dotnet run
This will result in generation of file named GraphQLClient.cs, which is the GraphQL client for your endpoint. This will help you in construction of your GraphQL query and mutation, rather than hardcoding them in query or mutation string.
Refer to the GraphQLClientGenerator GitHub page.
Internal reference title GraphQL Client Class Generator.