zino-hofmann / graphql-flutter

A GraphQL client for Flutter, bringing all the features from a modern GraphQL client to one easy to use package.
https://zino-hofmann.github.io/graphql-flutter
MIT License
3.25k stars 620 forks source link

Graphql_flutter query result not coming in desired format extra __typename is coming from server #1087

Closed manjeety87 closed 2 years ago

manjeety87 commented 2 years ago

{typename: DummyStockData, title: nifty, subtitleData: 0.86(1.25%), subtitle: nse, titleData: 98.65, graphData: [{typename: GraphData, srno: 0, data: 50}, {typename: GraphData, srno: 1, data: 80}, {typename: GraphData, srno: 2, data: 90}, {typename: GraphData, srno: 3, data: 70}, {typename: GraphData, srno: 4, data: 100}, {typename: GraphData, srno: 5, data: 80}, {typename: GraphData, srno: 6, data: 70}, {typename: GraphData, srno: 7, data: 120}, {typename: GraphData, srno: 8, data: 100}, {__typename: GraphData, srno: 9, data: 140}]}

The output I want is in json format

{ DummyStockData: title: nifty, subtitleData: 0.86(1.25%), subtitle: nse, titleData: 98.65 graphData: [ {srno: 0, data: 50},{srno: 2, data: 90}, { srno: 3, data: 70}, { srno: 4, data: 100}, {srno: 5, data: 80}, { srno: 6, data: 70}, {srno: 7, data: 120}, {srno: 8, data: 100}, {srno: 9, data: 140} ] }

why there extra data like __typename is coming ? DummyStockData is a object in which graphData is array of objects

My Code : `class QueryExample extends StatelessWidget { final String getMission = r""" query MyQuery { getDummyStockData { title subtitleData subtitle titleData graphData { srno
data } } } """; const QueryExample({Key? key}) : super(key: key);

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text("GraphlQL Client"), ), body: Query( options: QueryOptions( document: gql(getMission), ), builder: (QueryResult result, {VoidCallback? refetch, FetchMore? fetchMore}) { if (result.hasException) { return Text(result.exception.toString()); }

      if (result.isLoading) {
        return const Text('Loading');
      }

      if (result.data != null) {
        const Text("Sucess");
      }
      // ignore: unused_local_variable
      var counters = result.data!['getDummyStockData'];
      print('counters $counters');
      return Text(counters.toString());
    },
  ),
);

} } `

How can I change the query result to type dynamic to - `class SharesCardModel { final String title; final String subtitle; final String titleData; final String subtitleData; final List graphData; SharesCardModel({ required this.title, required this.subtitle, required this.titleData, required this.subtitleData, required this.graphData, }); } class LineGraphData { final int time; final int shares;

LineGraphData(this.time, this.shares); }`

budde377 commented 2 years ago

This is expected behaviour. The gql adds the typename which is used for normalisation.

manjeety87 commented 2 years ago

This is expected behaviour. The gql adds the typename which is used for normalisation.

In postman when we fire query request it comes in json format which I can use but How would I get the data in proper json format so that I can map it down

budde377 commented 2 years ago

What do you mean? The data is a map parsed from JSON.

budde377 commented 2 years ago

If you need a json string, you can serialise it back as you'd normally do.