Guidance for executing GraphQL query using .NET SDK

I have…

I’m submitting a…

  • [ ] Regression (a behavior that stopped working in a new release)
  • [ ] Bug report
  • [ ] Performance issue
  • [X] Documentation issue or request

Current behavior

Per the SDK documentation: https://docs.squidex.io/02-documentation/software-development-kits/.net-standard

We are able to use the SDK and query contents by following examples. We’d like to use GraphQL with the SDK, but it is not clear how this is done.

I also looked at this git page:

Line 478 starts a test example.

I can create a query and make this call:

var result = await client.GraphQlGetAsync<QueryResult>(query, context: QueryContext.Default.WithLanguages("en"));

And receive this error:

An GraphQl error occurred.

  • Error parsing query: Unexpected Name “querySchemaContents”

I’m looking for guidance on what the QueryResult class should look like to handle a flat data response (single language).

Minimal reproduction of the problem

Here is an example GraphQL query:

{
  querySchemaContents(
filter: "id in ('0bc6eb0e-eb6f-4497-81f0-61df1bfc11ee','c79a827e-f836-4d06-bd1d-53c8de54edaa')"
  ) {
flatData {
  sectionName
  pageType
  pageDescription
  pageHeading
  headerImage {
    url
  }
  videoUrl {
    url
  }
  contentGroups {
    insertDividerAbove
    layoutType
    groupHeading
    groupParagraph
    groupImage {
      url
    }
    bulletedList {
      listHeader
      listItems {
        item
      }
    }
  }
}
  }
}

The response in the GraphiQL tool shows this response structure:

{
  "data": {
    "querySchemaContents": [
      {
        "flatData": {
          "sectionName": "value1",
          "pageType": "value2"
...

Environment

App Name:

  • [X] Self hosted with docker
  • [ ] Self hosted with IIS
  • [ ] Self hosted with other version
  • [ ] Cloud version

Version: v.3.5.2.0

Others:

Hi, can you also post your QueryResult class? it has to match to the response structure of your query.

Well, I got it. The issue was more to do with how to write the query.

It would be great if there was documentation on how to make GraphQL requests with the SDK. I was able to follow the REST request examples just fine and that works.

Two items in particular that would be helpful in the documentation:

  • For GraphQL use:
    clientManager.CreateDynamicContentsClient()
    and not
    clientManager.CreateContentsClient<TBase ,T>()

  • An example of a query string. (For a GraphQL expert, not necessary, but Squidex has been our team’s motivation to using GraphQL so we’re definitely newer to it.)

For others looking to use GraphQL with the .NET SDK, here’s my example information:

var client =
    clientManager.CreateDynamicContentsClient("demo-schema");

var query = new
{
    query = @"
            query ContentsQuery($filter: String!) {
                queryDemoSchemaContents(filter: $filter) {
                    flatData {
                        sectionName
                        pageType
                        pageDescription
                        pageHeading
                        headerImage {
                            url
                        }
                        videoUrl {
                            url
                        }
                        contentGroups {
                            insertDividerAbove
                            layoutType
                            groupHeading
                        groupParagraph
                            groupImage {
                                url
                        }
                            bulletedList {
                                listHeader
                                listItems {
                                    item
                                }
                            }
                        }
                    }
                }
            }",
    variables = new
    {
        filter = @"id in ('bb0f4cdd-7f67-4c6a-9e7b-9f78c9fcca4a','a432a1e4-b03f-405e-bcdf-03a198e8157e')"
    }
};

var result = await client.GraphQlGetAsync<DemoPageGqlResult>(query, context: QueryContext.Default.WithLanguages("en"));

And the classes to hold the response:

 public sealed class DemoPageGqlResult
    {
        [JsonProperty("queryDemoSchemaContents")]
        public QueryItem[] Items { get; set; }
    }

    public sealed class QueryItem
    {
        public SquidexDemoPage FlatData { get; set; }
    }

    public class SquidexDemoPage
    {

        public string SectionName { get; set; }
        public string PageType { get; set; }
        public string PageDescription { get; set; }

        public string PageHeading { get; set; }

        public List<SquidexDemo_MediaObject> HeaderImage { get; set; }

        public List<SquidexDemo_MediaObject> VideoUrl { get; set; }

        public List<SquidexDemoPage_ContentGroup> ContentGroups { get; set; }
    }

    public class SquidexDemo_MediaObject
    {
        public string Url { get; set; }
    }


    public class SquidexDemoPage_ContentGroup
    {

        public bool? InsertDividerAbove { get; set; }
        public string LayoutType { get; set; }

        public string GroupHeading { get; set; }

        public string GroupParagraph { get; set; }

        public List<SquidexDemo_MediaObject> GroupImage { get; set; }

        public SquidexDemoPage_BulletedList BulletedList { get; set; }


    }

    public class SquidexDemoPage_BulletedList
    {
        public string ListHeader { get; set; }

        public List<SquidexDemoPage_ListItem> ListItems { get; set; }
    }

    public class SquidexDemoPage_ListItem
    {
        public string Item { get; set; }
    }
1 Like

You can use that if you are not using the other endpoints or pure GraphQL. But it is not a must. The only benefit is that you do not have to define the model class.

1 Like