1

I've got a contrived table "Json", which stores json as a string.

id | data
----------------------------
1  | "{ 'foo': 'bar' }"
2  | "{ 'cat': 'dog' }" 

I'd like to be able to return it from a graphql query in json form (rather than converting on client end). How do I notate a miscellaneous object type in graphQl?

Something like:

query {
  json(id: 1) {
    data {
      #whatever data is has?
    }
  }
}

--would return--

{
  "data": {
    "foo": "bar"
  }
}
Seph Reed
  • 4,704
  • 7
  • 30
  • 65

1 Answers1

0

GraphQL is strongly typed in this regard, so you can't do this. You have to return a the JSON as a string (or some other defined GraphQL type that suites your purpose, but you'll probably want to use a string) and parse it on the client. Every part of the GraphQL query/response corresponds to the schema, and each field has a type.

Dmitry Minkovsky
  • 30,036
  • 20
  • 97
  • 138
  • Hmmm... I'm in nodejs. Can you think of any good ways to force it? – Seph Reed Aug 23 '19 at 21:26
  • You can't, it goes against the core concepts/processes of how GraphQL works. If you go schemaless at some point in the tree, execution can't continue there after that. You could "force it", but that would be the end of the processing from point down in the tree. And to do this forcing, you'd have to hack some core tools. – Dmitry Minkovsky Aug 23 '19 at 21:29
  • I don't care about processing down the tree at all. But having to hack the core tools sounds like a hassle. Thank you. – Seph Reed Aug 23 '19 at 21:31
  • You could try to do this server side after GraphQL resolution, once you have the result, if you can identify the JSON strings in the result somehow. Then you could replace the JSON strings with the parsed objects. But then your query wouldn't match your response, you couldn't use GraphQL clients, etc. – Dmitry Minkovsky Aug 23 '19 at 21:34
  • What I'm really trying to do is have graphql be able to work of the same functions as my rest endpoints. Kind of doing a hybrid. So in this case, I either have to get GraphQl to return misc json, or have my endpoint return a string. – Seph Reed Aug 23 '19 at 21:41
  • Interesting approaches in that dupe link! – Dmitry Minkovsky Aug 24 '19 at 03:44