GraphQlClientGenerator icon indicating copy to clipboard operation
GraphQlClientGenerator copied to clipboard

Build Graphql variable

Open thheg opened this issue 10 months ago • 1 comments

I can't figure out how to build graphql variable object properly, the error is mainly that the graphQL server wants everything in camel case, but json serilizing for some properties is captilized and follows C# naming conventions.

` var filterV2 = new ObjectValueFilter { DefinitionId= 37};

   var dic = new Dictionary<string, object>
   {
       { nameof(filterV2), filterV2 }
   };

   var filterParameter2 = new GraphQlQueryParameter<ObjectValueFilter>(nameof(filterV2));

   var queryBuilder = new QueryQueryBuilder()
       .WithObjectValues(new ObjectValueConnectionQueryBuilder()
               .WithPageInfo(new PageInfoQueryBuilder()
                   .WithHasNextPage())
               .WithEdges(new ObjectValueEdgeQueryBuilder()
                   .WithCursor()
                   .WithNode(new ObjectValueQueryBuilder()
                       .WithDbId()
                       .WithOwnerDbId()
                       .WithCode()
                       .WithDescription()
                   ))
           , first: 100, after: "1", filter: filterParameter2).WithParameter(filterParameter2);


   var gql = new GraphQLRequest
   {
       Query = queryBuilder.Build(Formatting.Indented),
       Variables = dic
   };
   Console.WriteLine($"Query:{JsonConvert.SerializeObject(gql, Settings)}");`

the serilized GraphQLRequest is: { "query": "query (\r\n $filterV2: ObjectValue_Filter) {\r\n objectValues(first: 100, after: \"1\", filter: $filterV2) {\r\n pageInfo {\r\n hasNextPage\r\n }\r\n edges {\r\n cursor\r\n node {\r\n dbId\r\n ownerDbId\r\n code\r\n description\r\n }\r\n }\r\n }\r\n}", "variables": { "filterV2": { "DefinitionId": 37 } } }

DefinitionId should have been definitionId

If I try var queryBuilder = new QueryQueryBuilder() .WithObjectValues(new ObjectValueConnectionQueryBuilder() .WithPageInfo(new PageInfoQueryBuilder() .WithHasNextPage()) .WithEdges(new ObjectValueEdgeQueryBuilder() .WithCursor() .WithNode(new ObjectValueQueryBuilder() .WithDbId() .WithOwnerDbId() .WithCode() .WithDescription() )) , first: 100, after: "1", filter: filterV2);

then filter is correct with definitionId as shown bellow. query{objectValues(first:100,after:"1",filter:{definitionId:37}){pageInfo{hasNextPage},edges{cursor,node{dbId,ownerDbId,code,description}}}}

Is there anything I do wrong or that I miss? I realy like the query builder and want to start
to use this in our code.

Other observation: From the definition of ObjectValueFilter we have this: `#if !GRAPHQL_GENERATOR_DISABLE_NEWTONSOFT_JSON [JsonConverter(typeof(QueryBuilderParameterConverter<int?>))] #endif public QueryBuilderParameter<int?> DefinitionId { get { return (QueryBuilderParameter<int?>)_definitionId.Value; } set { _definitionId = new InputPropertyInfo { Name = "definitionId", Value = value }; } }

    #if !GRAPHQL_GENERATOR_DISABLE_NEWTONSOFT_JSON
    [JsonProperty("definitionId_not")]
    [JsonConverter(typeof(QueryBuilderParameterConverter<int?>))]
    #endif
    public QueryBuilderParameter<int?> DefinitionIdNot
    {
        get { return (QueryBuilderParameter<int?>)_definitionIdNot.Value; }
        set { _definitionIdNot = new InputPropertyInfo { Name = "definitionId_not", Value = value }; }
    }`

DefinitionId don't have a JsonProperty, but e.g. definitionId_not has one. I belive this could also solve the proble

PS: The exemple above s a simple and it could be solved easily without variables directly in the query, so this is more a test of functionality. We do mutation we sometimes send a list of 100 input objects in one request. We then think it is easier to read and better practice when the input is in the varable object and not a part of the query string.

thheg avatar Mar 27 '24 11:03 thheg