1

I have an GraphQL api implemented using HotChocolate 11. The schema changed recently and I would like to use our stitching layer to keep it the same for clients. I have an mutation that looked like this:

type Mutation {
  generate(apertures: [ApertureInput!]! templates: [TemplateInput!]!): [GeneratedApertures!]!
}

I had to add one more argument, also we added a query to get the additional data

type Mutation { 
  generate(apertures: [ApertureInput!]! templates: [TemplateInput!]! algorithm: AlgorithmInput!): [GeneratedApertures!]!
}

type Query {
 standardAlgorithm: StandardAlgorithm
}

type StandardAlgorithm {
 shrink: Int!
}

What I'm trying to do is to use our stitching layer to add the old signature of the mutation and call a remote schema in the resolver to get the additional data and pass it to the new mutation. I have renamed the new signature on the stitch and created a mutation that looks like the old one:

extend type Mutation {
   product_generate(apertures: [ApertureInput!]! templates: [TemplateInput!]! algorithm: AlgorithmInput!): GeneratedApertures!
     @source(name: "generate" schema: "productdefinition") 
     @delegate(schema: "productdefinition")
   generate(apertures: [ApertureInput!]! templates: [TemplateInput!]!): GeneratedApertures!
}

But I am lost using the stitching context to build the sub queries.

    public class GenerationMutationTypeExtension : ObjectTypeExtension
    {
        protected override void Configure(IObjectTypeDescriptor descriptor)
        {
            descriptor.Name("Mutation");
            descriptor.Field("generate")
                      .Resolve(async ctx => {
                          var context = ctx.Service<IStitchingContext>();
                          var executor = context.GetRemoteRequestExecutor("productdefinition");
                          var request =
                              QueryRequestBuilder.New()
                                                 .SetQuery("{ standardAlgorithm { shrink } }")
                                                 .SetServices(executor.Services)
                                                 .Create();

                          var result = (QueryResult)await executor.ExecuteAsync(request);
                          var template = result.Data?["standardTemplate"];
                          if (template is NullValueNode || template is null) throw new GraphQLRequestException("Missing configuration!!!");

                          var shrink = (string)((IValueNode)((Dictionary<string, object>)template)["shrinkPercent"]).Value;

                          var apertures = ctx.ArgumentLiteral<IValueNode>("apertures");
                          var templates = ctx.ArgumentLiteral<IValueNode>("templates");
                          var selection = ctx.Selection.SyntaxNode.SelectionSet;

                          var query =
                              $"{{ product_generateDeposits(apertures: {apertures} templates: {templates} algorithm: {{ standardAlgorithm: {{ shrink: {shrink} }} }}) {selection} }}";
                          request =
                              QueryRequestBuilder.New()
                                                 .SetQuery(query)
                                                 .SetServices(executor.Services)
                                                 .Create();
                          result = (QueryResult)await executor.ExecuteAsync(request);
                          return result;
                      });
        }
    }

The first query for the shrink is working fine, I get the data. But I struggle with putting the other mutation together. Is there maybe a better way how to achieve this? Documentation is very vague when it comes to stitching.

0 Answers0