2. GraphQL - Xablu/Xablu.WebApiClient GitHub Wiki
In here we will be taking a in-depth look at the possibilities with our GraphQL.Client. Our version has the following implemented:
- Based on GraphQL.Client
- Implemented Retry and Timeout from Polly
- Implemented Fusillade’s Priorities
- Implemented a Custom, Object-Oriented Query Builder which includes a builder for Mutations!
Unique in this package is the addition of a Custom, Object-Oriented Query Builder. This builder will translate your model into a query. Which then can be used to get result object back from the server.
When using this package you have the ability to either do a query call or a mutation call. Each call has a different builder and request class.
There are also 3 different kind of attributes and we will be going over their use-cases one by one.
When querying a result you have to initialize and use the Request class. This class supports the custom query builder for GraphQL query calls.
Query calls are used to get data from the server.
When initializing the Request class you have the option to supply your own query or when you provide a model it will translate this into a working query.
Example:
var request = new Request<BooksResponseModel>()
or when you want to provide your own query
var queryString = "{books {id: iSBN title authors publishDate genre}}";
var result = new Request(queryString);
When making a mutation call you have to initialize and use the MutationRequest class. This class supports the custom query builder for GraphQL mutation calls.
Mutation calls are used to add/update/delete data on the server.
NOTE: Currently we only support an operation with single variable input when using the query builder!
When initializing the MutationRequest class you have the option to supply your own query or when you provide a model it will translate this into a working mutation query. Be sure when creating a mutation call to always specify the variables you want to target.
Example:
// Variables
BookReview = new BookReview
{
BookISBN = "0544272994",
Review = "This is a mutation test"
};
var detail = new MutationDetail("createReview", "review");
// Initialization of mutation class
var mutationRequest = new MutationRequest<BookReview>(detail, BookReview)
or when you want to provide your own query
var queryString = "mutation($variables: reviewInput!){createReview(review: $variables){bookISBN review}}";
var result = new MutationRequest(queryString, BookReview)
This class currently accepts two arguments which are as following:
- The mutation name this should always be specified
- The mutation argument input name.
In the feature we will be accepting multiple parameters.
In this package we supply 3 different kind of attributes:
- GraphQlEndPointAttribute
- NameOfFieldAttribute
- VariableInputTypeAttribute
Each attribute has their use-case and should be explicitly be used in either mutation calls or query calls.
NOTE: This attribute is mandatory!
Be sure when implementing the WebApiClient to mark your GraphQL endpoint inside the interface of your Api.
Example:
[GraphQLEndpoint("/graphql")]
public interface IGitHubApi
{
}
NOTE: Use this explicitly for query calls only!
This attribute should be used on a property in which you want to query a specific field inside that object.
example:
public class Status
{
[JsonProperty("message")]
public string Message { get; set; }
}
[NameOfField("message")]
public Status Status { get; set; }
When done correctly the query should try and fetch the specified field.
NOTE: Use this explicitly for mutations calls only!
The following attribute is the variable input type name. This should be placed on the model with the associated type input value.
example:
[VariableInputType("reviewInput")]
public class BookReview
{
public string BookISBN { get; set; }
public string Review { get; set; }
}
[VariableInputType("ChangeUserStatusInput")]
public class ChangeUserStatus
{
[JsonProperty("clientMutationId")]
public string ClientMutationId { get; set; }
[JsonProperty("status")]
public Status Status { get; set; }
}