Getting items in c#

Hi, I am trying to fetch item column values from a board and am falling short. The following query works when I run it in your try-it-yourself page,

{
  boards(ids: MYBoardID) {
    name
    items {
      id
      name
      column_values(ids: ["text"]) {
        title
        value
      }
    }
  }
}

However, from c# the following fails with a Bad Request exception from the HttpClient request,
query = “{ “query”: " { boards(ids: MYBoardID) { name items { id name column_values(ids: [“text”]) { title value } } } } " }”;
(edit, the escape back slashes for the quotation marks aren’t showing in the post, but the following image is a snip of the actual code)

Essentially the 2 queries are the same with the exception of the added “query”: prefix and the closing brace suffix in the c#.

Can anyone tell me what may be the problem?
Thanks for looking
Kevin

Hey @Mickabooh!

Not a C# expert, but happy to put our heads together to figure this out.

I noticed that you’ve got a single backslash before the quotes around “text”. However, since this is an inner string inside an inner string, you might need to double-escape them. Something like this:

query = “{ \“query\”: \" { boards(ids: MYBoardID) { name items { id name column_values(ids: [\\\“text\\\”]) { title value } } } } \" }”

Alternatively, you might want to use GraphQL variables to pass your string arguments. Essentially, you add a reference for the column IDs to your query, and then pass the actual data in another object called “variables”. This approach minimizes the amount of escaping you need to do.

Here’s a generalization of that flow using pseudocode (I hope this makes sense, you’d need to adjust this to C#):

# store your query as a string
declare string my_query = "query ($columnIDs:[String!]) { boards(ids: 12345) { name items { id name column_values(ids: $columnIDs) { title value } } } }" 

# store your variables as an object/hash table/dictionary
declare string[] my_column_ids = ["text"]
declare object my_variables = {"columnIDs" : my_column_ids} # the list is inside this object

# construct request payload using JSON serializer to turn data structures into valid JSON
declare string payload = JSON.serialize( {"query" : my_query, "variables" : my_variables} )

Hope that helps!

That was it. Thank you very much.

I’ll definitely look into the the GraphQL variables… all of the escaping just escapes me.
Kevin

1 Like

Using variables and a sturdy JSON serializer will let you escape the world of escape characters. Try it out :slight_smile:

1 Like