Hotchocolate: Strawberry Shake IOperation.

Created on 26 Jan 2020  ยท  8Comments  ยท  Source: ChilliCream/hotchocolate

Quick question. After generating a StrawberryShake client from a schema with a mutation defined as:

type Mutation {
  mergeFoo(data: FooInput): Foo
}

A method expecting a MergeFooOperation is provided. It's created a MergeFoo1 type derived from a MergeFoo interface but it's unclear how these are pushed to the client.

[GeneratedCode("StrawberryShake", "11.0.0")]
public interface IFooClient {
        Task<IOperationResult<IMergeFoo>> MergeFooAsync(
            MergeFooOperation operation,
            CancellationToken cancellationToken = default);
}

How does a populated FooInput instance get passed to the client via the MergeFooOperation?

โ“ question

All 8 comments

Hi @grounzero
You can but do not need to create the MergeFooOperation. There are other methods. One should expect only the inputs values:

[GeneratedCode("StrawberryShake", "11.0.0")]
public interface IFooClient {
        Task<IOperationResult<IMergeFoo>> MergeFooAsync(
            Optional<FooInput> data = default,
            CancellationToken cancellationToken = default);


        Task<IOperationResult<IMergeFoo>> MergeFooAsync(
            MergeFooOperation operation,
            CancellationToken cancellationToken = default);
}

In the generated FooClient.cs that implements this interface you will see how the Operation is created

[GeneratedCode("StrawberryShake", "11.0.0")]
public class FooClient {
        public Task<IOperationResult<IMergeFoo>> MergeFooAsync(
            Optional<FooInput> data= default,
            CancellationToken cancellationToken = default)
        {

            return _executor.ExecuteAsync(
                new MergeFooOperation { Data= data},
                cancellationToken);
        }

        public Task<IOperationResult<IMergeFoo>> MergeFooAsync(
            MergeFooOperation operation,
            CancellationToken cancellationToken = default)
        {
            if (operation is null)
            {
                throw new ArgumentNullException(nameof(operation));
            }

            return _executor.ExecuteAsync(operation, cancellationToken);
        }
}

Hi, My client seems to be missing the Optional<FooInput> data= default

My client looks like this:

        public Task<IOperationResult<IMergeFoo>> MergeFooAsync(
            CancellationToken cancellationToken = default)
        {

            return _executor.ExecuteAsync(
                new MergeFooOperation(),
                cancellationToken);
        }

        public Task<IOperationResult<IMergeFoo>> MergeFooAsync(
            MergeFooOperation operation,
            CancellationToken cancellationToken = default)
        {
            if (operation is null)
            {
                throw new ArgumentNullException(nameof(operation));
            }

            return _executor.ExecuteAsync(operation, cancellationToken);
        }

Also the operation seems to be missing the Data property.

  [System.CodeDom.Compiler.GeneratedCode("StrawberryShake", "11.0.0")]
    public class MergeFooOperation
        : IOperation<IMergeFoo>
    {
        public string Name => "mergeFoo";

        public IDocument Document => Queries.Default;

        public OperationKind Kind => OperationKind.Mutation;

        public Type ResultType => typeof(IMergeFoo);

        public IReadOnlyList<VariableValue> GetVariableValues()
        {
            return Array.Empty<VariableValue>();
        }
    }

@grounzero ok. the problem could be the mutation query. What did you define in the Queries.graphql file? :)

It's defined as:

mutation mergeFoo {
  mergeFoo(
    data:
      {
        baz: 0
      }
  ) {
    baz
  }
}

ah I see
So the thing is this mutation does not really has any variables.
i basically is send like this to the backend.
if you would like to use it with variables you have to declare them in the mutation header.
so like this:

mutation mergeFoo($data: FooInput) {
  mergeFoo( data: $data  ) {
    baz
  }
}

Ahh ok I wasn't specifying the variable type. I just regenerated and all is as expected. Thank you so much!

Cool :+1:
Glad it worked. I'm gonna close this one :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hognevevle picture hognevevle  ยท  3Comments

benmccallum picture benmccallum  ยท  5Comments

mortzi picture mortzi  ยท  4Comments

jbray1982 picture jbray1982  ยท  5Comments

IKolosynskyi picture IKolosynskyi  ยท  3Comments