heftapp / graphql_codegen

MIT License
137 stars 52 forks source link

How to set variable in mutation? #353

Open muslimmuda15 opened 1 week ago

muslimmuda15 commented 1 week ago

I follow step by step the instruction until generate my graphql

dart run build_runner build

I tried to request my query from my widget

# port.graphql
query FindPortAndCity($options: ListOptions, $keyword: String, $zone_id: ID) {
  findPortAndCity(options: $options, keyword: $keyword, zone_id: $zone_id) {
    id
    type
    name
    location {
      latitude
      longitude
    }
    photos
  }
}

// port widget
Query_FindPortAndCity_Widget(
      options: Options_Query_FindPortAndCity(
        variables: Variables_Query_FindPortAndCity(
          keyword: searchText,
          zone_id: null,
          options: Input_ListOptions(
            active: null,
            limit: null,
            offset: null,
            order: Enum_SearchOrder.desc,
            orderBy: "created_at",
          ),
        ),
        fetchPolicy: FetchPolicy.networkOnly,
      ),
      builder: (result, {fetchMore, refetch}) {
      },
    );

everything working well.

But how to impelementation mutation? I have graphql mutation

# create port
mutation CreateRfqTestMutation($data: InQuoteProjectLocation!) {
  createRfqTest(data: $data) {
    all_found {
      id
    }
    unmatch {
      id
    }
    match {
      id
    }
    pol_zone
  }
}

But I still confuse how to implement that? Because it seem very diffeent with the query.

I implementation like this

useMutation_CreateRfqTestMutation(
    WidgetOptions_Mutation_CreateRfqTestMutation(
          variables: // i don't see variable here
    )
)

the useMutation required WidgetOptions_Mutation_CreateRfqTestMutation but, i cannot set variable inside there.

image

Please help me.

github-actions[bot] commented 1 week ago

👋 @muslimmuda15 Thank you for raising an issue. I will investigate the issue and get back to you as soon as possible. Please make sure you have provided enough context.

This library is created and maintained by me, @budde377. Please consider supporting my work and ensure our survival by donating here.

budde377 commented 6 days ago

Thanks for reaching out @muslimmuda15 - The useMutation* hook returns an object with a runMutation method on it. When you call this, you need to provide the variables.

muslimmuda15 commented 7 hours ago

can you give me some example the full code, because i getting error like this, and i already using StatefulHookWidget image

budde377 commented 4 hours ago

Because the code-generator is just a wrapper around graphql and graphql_flutter, try and have a look at their documentation: https://github.com/zino-hofmann/graphql-flutter/tree/main/packages/graphql_flutter#mutations