Hotchocolate Strawberryshake how to use a mutation?

Viewed 358

There seem to be no examples at all on how to do a mutation by a graphql client using Strawberryshake (C#). Using Strawberryshake Version11.0.0-preview.138, Abstractions and Http v. 10.3.0-preview.12 and CodeGeneration.CSharp.Analyzers v. 0.0.30, I can run the query from the example just fine. Now I added a mutation, but I can't figure how to use it?

I got the following from the schema import (Alarm.graphql):

schema {
  query: Query
  mutation: Mutation
}

type Query {
  alarms: [Alarm]
}

type Mutation {
  addAlarm(input: AlarmInput): AlarmPayload
}

type AlarmPayload {
  alarm: Alarm
}

type Alarm {
  id: Int!
  handle: Int!
  messageId: Int!
  message: String
}

input AlarmInput {
  id: Int!
  handle: Int!
  messageId: Int!
  message: String
}

The query using

query getAlarms {
    alarms {
        message
    }
}

is just fine. For the mutation I added

mutation writeAlarm {
    addAlarm {
       alarm {
            id
            handle
            messageId
            message
        }
    }
}

Which successfully generates quite a lot of sensible looking classes on build. Now I create a connection to the server:

var serviceCollection = new ServiceCollection();
serviceCollection.AddHttpClient(
"ConnectorLogGraphQL",
    c => c.BaseAddress = new Uri("http://localhost:5000/graphql"));
serviceCollection.AddAlarmClient();
var services = serviceCollection.BuildServiceProvider();
IAlarmClient client = services.GetRequiredService<IAlarmClient>();

The client is what I need to use. For the query, I can use:

var result = await client.GetAlarmsAsync();

similarly

var result = await client.WriteAlarmAsync();

compiles just fine, but of course it doesn't do what I'd like to. There's some WriteAlarmOperation defined, which I could pass as a parameter to WriteAlarmAsync, but this operation doesn't have fields for the input either. Something's missing.. ?

0 Answers
Related