Apollo/GraphQL field type for object with dynamic keys

Answers • 2 Asked • Jun 24 2019
Let's say my graphql server wants to fetch the following data as JSON where person3 and person5 are some id's:
"persons": { "person3": { "id": "person3", "name": "Mike" }, "person5": { "id": "person5", "name": "Lisa" } }
Question: How to create the schema type definition with apollo?
The keys person3 and person5 here are dynamically generated depending on my query (i.e. the area used in the query). So at another time I might get person1, person2, person3 returned. As you see persons is not an Iterable, so the following won't work as a graphql type definition I did with apollo:
type Person { id: String name: String } type Query { persons(area: String): [Person] }

The keys in the persons object may always be different.
One solution of course would be to transform the incoming JSON data to use an array for persons, but is there no way to work with the data as such?

Write your answer...

On a mission to build Next-Gen Community Platform for Developers