question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A common graphql use-case is to use variables in queries & mutations https://graphql.org/learn/queries/#variables.

Variable support could look like:

Query:

  • Accept a variable type in WithX methods
    • could have a variable name as a constructor parameter or a builder method etc…
var builder =
  new QueryQueryBuilder()
    .WithMe(
      new MeQueryBuilder()
        .WithAllScalarFields()
        .WithHome(
          new HomeQueryBuilder()
            .WithAllScalarFields()
            .WithSubscription(
              new SubscriptionQueryBuilder()
                .WithStatus()
                .WithValidFrom())
            .WithSignupStatus(
              new SignupStatusQueryBuilder().WithAllFields())
            .WithDisaggregation(
              new DisaggregationQueryBuilder().WithAllFields()),
          new Variable<Id>("b420001d-189b-44c0-a3d5-d62452bfdd42", "homeId"))
        .WithEnergyStatements (new Variable<string>("2016-06", "from"), new Variable<string>("2016-10", "to"));

var query = builder.Build(Formatting.Indented);

Results into

{
query: "query ($homeId: Id, $from: string, $to: string) {
  me {
    id
    firstName
    lastName
    fullName
    ssn
    email
    language
    tone
    home (id: $homeId) {
      id
      avatar
      timeZone
      subscription {
        status
        validFrom
      }
      signupStatus {
        registrationStartedTimestamp
        registrationCompleted
        registrationCompletedTimestamp
        checkCurrentSupplierPassed
        supplierSwitchConfirmationPassed
        startDatePassed
        firstReadingReceived
        firstBillingDone
        firstBillingTimestamp
      }
      disaggregation {
        year
        month
        fixedConsumptionKwh
        fixedConsumptionKwhPercent
        heatingConsumptionKwh
        heatingConsumptionKwhPercent
        behaviorConsumptionKwh
        behaviorConsumptionKwhPercent
      }
    }
    energyStatements(from: $from, to: $to) 
  }",
  variables: {
    homeId: "b420001d-189b-44c0-a3d5-d62452bfdd42",
    from: "2016-06",
    to: "2016-10"
  }
}

Mutation

var mutation =
	new RootMutationQueryBuilder()
		.WithUpdateHome(
			new HomeQueryBuilder().WithAllScalarFields(),
			new Variable<UnpdateHomeInput>(new UpdateHomeInput { HomeId = Guid.Empty, AppNickname = "My nickname", Type = HomeType.House, NumberOfResidents = 4, Size = 160, AppAvatar = HomeAvatar.Floorhouse1, PrimaryHeatingSource = HeatingSource.Electricity }, "homeInput")
		)
	.Build(Formatting.Indented, 2);

Results into

{
query: "mutation ($homeInput: UpdateHomeInput!) {
  updateHome (input: $homeInput) {
    id
    timeZone
    appNickname
    appAvatar
    size
    type
    numberOfResidents
    primaryHeatingSource
    hasVentilationSystem
  }
}",
variables: {
  homeInput: {
      homeId: "00000000-0000-0000-0000-000000000000"
      appNickname: "My nickname"
      appAvatar: FLOORHOUSE1
      size: 160
      type: HOUSE
      numberOfResidents: 4
      primaryHeatingSource: ELECTRICITY
    }
}

Could extend it further and generate the query and variables separately which would allow you to statically use the same query and dynamically generate the variables per request

var builder =
	new RootMutationQueryBuilder()
		.WithUpdateHome(
			new HomeQueryBuilder().WithAllScalarFields(),
			new Variable<UnpdateHomeInput>("homeInput")
		);

var mutation = builder.Build(Formatting.Indented, 2);
var variables = builder.BuildVariables(...) // Would have to do something clever with types here...

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
Husqvikcommented, Feb 10, 2020

Ok, finally I think I get it. It’s not possible to set dynamic parameter as field of any input object.

I really don’t want to introduce complex type properties as I would like to have the input objects properties plain data types.

How about to generate With{PropertyName}Parameter(QueryBuilderParameter<T>) where a parameter can be passed?

So the use would be

new InputObject
{
    Property1 = constantValueParameter
}
.WithProperty2Parameter(dynamicQueryParameter);

Or maybe the query parameter after all as you suggest but securing the JSON serialization works both ways. With just plain values.

1reaction
Husqvikcommented, Nov 16, 2019
var input = new UpdateHomeInput { HomeId = Guid.Empty, AppNickname = "My nickname", Type = HomeType.House, NumberOfResidents = 4, Size = 160, AppAvatar = HomeAvatar.Floorhouse1, PrimaryHeatingSource = HeatingSource.Electricity };
var parameter = new GraphQlQueryParameter<UpdateHomeInput>("homeInput", nameof(UpdateHomeInput), input);

var mutation =
	new TibberMutationQueryBuilder()
		.WithUpdateHome(
			new HomeQueryBuilder().WithAllScalarFields(),
			parameter
		)
	.WithParameter(parameter)
	.Build(Formatting.Indented, 2)

I’ll probably remove the type parameter as it also can be generated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Support of a random variable
The support of a random variable is the set of values that the random variable can take.
Read more >
Support (mathematics)
In probability theory, the support of a probability distribution can be loosely thought of as the closure of the set of possible values...
Read more >
Precise definition of the support of a random variable
Random Variables – A random variable is a real valued function defined on the sample space of an experiment. Associated with each random ......
Read more >
Variable Spring Supports - Products | Piping Tech
Variable springs utilize coiled springs to allow movement and provide load support. PT&P is highly experienced in the design and supply of variables....
Read more >
CSS Variables (Custom Properties) | Can I use... Support ...
"Can I use" provides up-to-date browser support tables for support of front-end ... Permits the declaration and usage of cascading variables in stylesheets....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found