Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose FragmentParameters to correctly build requests with DispatchInput #29

Merged
merged 1 commit into from
Apr 25, 2024

Conversation

CliffCawley
Copy link
Contributor

@CliffCawley CliffCawley commented Apr 25, 2024

This allows you to get the FragmentParameters and add things like WithId() and WithEncodedData()

I.e. it allows the following extension method:

public static DispatchInputType SetupFromRequestObject<TRequest, TFragment>(this DispatchInputType dispatchInputType, GraphQlRequest<TRequest, TFragment> request, DispatchCall? call, out JsonDocument disposeDocument)
    where TRequest : GraphQlRequest<TRequest, TFragment>
    where TFragment : IGraphQlFragment

{
    if (request.FragmentParameters != null && request.FragmentParameters is TransactionFragment transactionFragment)
    {
        // Ensure that Id and EncodedData are included, because they're a requirement of the DispatchInput call
        transactionFragment
            .WithId()
            .WithEncodedData();
    }

    var options = new JsonSerializerOptions
    {
        Converters = { new BigIntegerJsonConverter() }
    };

    var jsonString = JsonSerializer.Serialize(request.VariablesWithoutTypes, options);
    disposeDocument = JsonDocument.Parse(jsonString);
    var jsonElement = disposeDocument.RootElement;
    return dispatchInputType.SetCall(call)
        .SetQuery(request.Compile())
        .SetVariables(jsonElement);
}

Which can be used to call DispatchAndTouch with the relevant request, such as:

 var reqDispatch = new DispatchAndTouch()
     .SetTankId(_settings.Value.EnjinPlatformFuelTankId)
     .SetRuleSetId(BigInteger.Parse(_settings.Value.EnjinPlatformFuelTankRuleSetId))
     .SetDispatch(new DispatchInputType()
         .SetupFromRequestObject(reqTransferBalance, DispatchCall.MultiTokens, out var disposeDocument)
     );

 reqDispatch.Fragment(new TransactionFragment().WithTransactionHash());
 var response = await _enjinPlatformService.Client.SendDispatchAndTouch(reqDispatch);
 disposeDocument.Dispose();

Perhaps something like this could be integrated into the SDK in the future, if it's desirable.

(It would also be nice to remove this dependency on a JsonDocument, but I haven't got the time to work on it)

@leonardocustodio leonardocustodio force-pushed the ExposeFragmentParameters branch from 02429e8 to ed78031 Compare April 25, 2024 16:51
@leonardocustodio leonardocustodio merged commit 7bba209 into enjin:master Apr 25, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants