graphqlapollo-serverexpress-graphqlrestdatasource

Apollo-GraphQL field type for object with dynamic keys from input type


Is there a way in apollo-graphql to define name of the field from values passed in input? Something like below, how to define key[i] here?

type response{
      key[0] : { 
            field1 : String,
            field2 : String
    }

where input is

input request {
       Keys: [String]
    }

response of rest call that I am trying to read

{
 "plan" :  {
     "key1" : {
           field1 : "value1"
           field2 : "value2"
      },
      "key2" : {
           field1 : "value1"
           field2 : "value2"
      }
    }
 }

Solution

  • Got Answer in following post:

    Apollo/GraphQL field type for object with dynamic keys

    Here is the summary:

    GraphQL relies on both the server and the client knowing ahead of time what fields are available available for each type. In some cases, the client can discover those fields (via introspection), but for the server, they always need to be known ahead of time. So to somehow dynamically generate those fields based on the returned data is not really possible.