# What is a good GraphQL lib

**URL:** <https://forum.inductiveautomation.com/t/what-is-a-good-graphql-lib/98492>\
**Category:** Ignition\
**Created:** [November 11, 2024, 11:27pm UTC](https://forum.inductiveautomation.com/t/what-is-a-good-graphql-lib/98492 "2024-11-11T23:27:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wpmccormick/32/13940_2.png) [@wpmccormick](https://forum.inductiveautomation.com/u/wpmccormick)\
**Post date:** [November 11, 2024, 11:27pm UTC](https://forum.inductiveautomation.com/t/what-is-a-good-graphql-lib/98492/1 "2024-11-11T23:27:39Z")

</div>

I'm looking for decent lightweight GraphQL lib to use in Ignition.

Anyone else doing this?

---

<div class="post-metadata">

**Author:** ![Benjamin\_Furlani](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/benjamin_furlani/32/66084_2.png) [@Benjamin\_Furlani](https://forum.inductiveautomation.com/u/Benjamin_Furlani)\
**Post date:** [November 12, 2024, 12:27am UTC](https://forum.inductiveautomation.com/t/what-is-a-good-graphql-lib/98492/2 "2024-11-12T00:27:47Z")

</div>

When you say GraphQL lib for Ignition what do you mean?

---

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wpmccormick/32/13940_2.png) [@wpmccormick](https://forum.inductiveautomation.com/u/wpmccormick)\
**Post date:** [November 12, 2024, 4:36am UTC](https://forum.inductiveautomation.com/t/what-is-a-good-graphql-lib/98492/3 "2024-11-12T04:36:14Z")

</div>

A GraphQL client implementation that can work from Ignition scripting ... so a Python 2.7 lib would be my preference. And to be decent/lightweight I'd add pure Python and no required config files.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 12, 2024, 5:23pm UTC](https://forum.inductiveautomation.com/t/what-is-a-good-graphql-lib/98492/4 "2024-11-12T17:23:16Z")

</div>

I would look for a _Java_ library that you can run out-of-band on the GraphQL server you're trying to communicate to, generating consuming classes out-of-band, then dump them into a module (preferred) or the `lib/` directory of the gateway, as described in this post dealing with Protobuf:

> [@Protobuf data encoding](https://forum.inductiveautomation.com/t/protobuf-data-encoding/97572/7):
>
> it's working after getting some guidance from Ignition support team grinning Define the Protobuf schema that describes the structure of the data you want to serialize/deserialize. This schema file (.proto) defines the types and structure of the messages. Generate Java code from the Protobuf schema using a Protobuf compiler (protoc) to generate Java classes from the .proto schema file. The protoc compiler reads the .proto file and outputs Java source files corresponding to the schema. …

---

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wpmccormick/32/13940_2.png) [@wpmccormick](https://forum.inductiveautomation.com/u/wpmccormick)\
**Post date:** [November 12, 2024, 5:27pm UTC](https://forum.inductiveautomation.com/t/what-is-a-good-graphql-lib/98492/5 "2024-11-12T17:27:22Z")

</div>

I have no control over server.

How much of a pain is going to be to get a client working with httpPost and other native and json libs?

Are there any other easy-button options? Perhaps some other middleware?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 12, 2024, 5:35pm UTC](https://forum.inductiveautomation.com/t/what-is-a-good-graphql-lib/98492/6 "2024-11-12T17:35:49Z")

</div>

Sorry, 'on' was a poor choice of words. I'm not an expert in GraphQL, but I mean:

1. Create a Java project using Maven or Gradle that consumes some Java GraphQL lib (e.g. [Apollo](https://github.com/apollographql/apollo-kotlin).
2. Point your Java project at your existing GraphQL server (assuming it supports introspection): [2. Add the GraphQL schema - Apollo GraphQL Docs](https://www.apollographql.com/docs/kotlin/tutorial/02-add-the-graphql-schema#download-your-servers-schema) to generate the Java classes you require.
3. Attach those classes to Ignition, either by dumping the built .jar file into `lib/` or building a custom module.

I don't think there is any 'free lunch' here. It looks like it should be pretty easy to deal with "raw" responses via `system.net.httpClient`: [Serving over HTTP | GraphQL](https://graphql.org/learn/serving-over-http/), but if you want any of the niceties a better client would offer you, it's going to take some doing.

---

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wpmccormick/32/13940_2.png) [@wpmccormick](https://forum.inductiveautomation.com/u/wpmccormick)\
**Post date:** [November 12, 2024, 5:55pm UTC](https://forum.inductiveautomation.com/t/what-is-a-good-graphql-lib/98492/7 "2024-11-12T17:55:11Z")

</div>

Yea that seems like a lot of work to do a single freaking API call to say "here's the RFID tag I printed for product 12345"

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [November 12, 2024, 8:22pm UTC](https://forum.inductiveautomation.com/t/what-is-a-good-graphql-lib/98492/8 "2024-11-12T20:22:09Z")

</div>

> [@paul-griffith](#):
>
> It looks like it should be pretty easy to deal with "raw" responses via `system.net.httpClient`:

I would go with this. You're right, it probably is not worth all the work to do all the jar/attaching to ignition etc, when realistically you just need to spend a little time figuring out how to parse a response.

---

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wpmccormick/32/13940_2.png) [@wpmccormick](https://forum.inductiveautomation.com/u/wpmccormick)\
**Post date:** [November 13, 2024, 2:19am UTC](https://forum.inductiveautomation.com/t/what-is-a-good-graphql-lib/98492/9 "2024-11-13T02:19:47Z")

</div>

The GraphQL response is always valid JSON, so not a huge deal. I'm sure some magic lib would do more of the heavy lifting for dealing with errors and whatnot.

However, the query itself is something that kind of looks like JSON, but it's not.

So what I needed to do was enclose the GraphQL query in a JSON string as a value to a key named _query_:

```python
mutation = {"query": "mutation {createItemSet(input : %s){tracker_serials}}" % itemSetData}                  

```

Also needed to regexp out commas and quotes around key names supplied in the itemSetData.

```python
# Step 1: Remove commas between dictionary items, but keep array-level commas
itemSetData = re.sub(r',(?=\s*"(?:\w+)":)', r'\n', itemSetData)

# Step 2: Remove quotes around keys only
itemSetData = re.sub(r'"(\w+)"\s*:', r'\1: ', itemSetData)

```

And finally the httpPost get's the job done:

```python
contentType = "application/json"
auth = {"Authorization": tokenId}

postData = system.util.jsonEncode(mutation)
response = system.net.httpPost(url=uri, contentType=contentType,headerValues=auth,postData=postData,throwOnError=False)
responsedata = system.util.jsonDecode(response)

```

Exception handling removed for brevity.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 13, 2024, 4:40pm UTC](https://forum.inductiveautomation.com/t/what-is-a-good-graphql-lib/98492/10 "2024-11-13T16:40:12Z")

</div>

> [@wpmccormick](#):
>
> httpPost

Seriously, don't use this function. They're going to be formally deprecated in 8.3.0, but in the meantime they obscure so many things about the HTTP relationship it's never worth it.

`system.net.httpClient` is a more performant, more debuggable, more powerful replacement. It'll also automatically handle the encoding and decoding of JSON for you (if the data param is a dictionary, the content-type is automatically set to `application/json`, and the response object has a json decode method built in):

```python
auth = {"Authorization": tokenId}

client = system.net.httpClient()
response = client.post(uri, headers=auth, data=mutation)
responsedata = client.post(uri, headers=auth, data=mutation).json

```

One caveat: if this is a project library script (which it probably should be), you should instantiate the `client` instance _outside_ any defined functions, e.g.

```python
client = ...

def runGraphQlQuery(query):
    client.post()
    ....

```
