title: GraphQL ObjectType with dynamic fields based on arguments
body: <p>We are in the situation that the response of our GraphQL Query has to return some dynamic properties of an object. In our case we are not able to predefine all possible properties - so it has to be dynamic.</p>
  
  <p>As we think there are two options to solve it.</p>
  
  <pre><code>const MyType = new GraphQLObjectType({
    name: 'SomeType',
    fields: {
      name: {
        type: GraphQLString,
      },
      elements: {
        /*
        THIS is our special field which needs to return a dynamic object 
        */
      },
      // ...
    },
  });
  </code></pre>
  
  <p>As you can see in the example code is element the property which has to return an object. A response when resolve this could be:</p>
  
  <pre><code>{
    name: 'some name',
    elements: {
      an_unkonwn_key: {
        some_nested_field: {
          some_other: true,
        },
      },
      another_unknown_prop: 'foo',
    },
  }
  </code></pre>
  
  <p><strong>1) Return a "Any-Object"</strong></p>
  
  <p>We could just return any object - so GraphQL do not need to know which fields the Object has. When we tell GraphQL that the field is the type GraphQlObjectType it needs to define fields. Because of this it seems not to be possible to tell GraphQL that someone is just an Object. </p>
  
  <p>Fo this we have changed it like this:</p>
  
  <pre><code>elements: {
        type: new GraphQLObjectType({ name: 'elements' });
      },
  </code></pre>
  
  <p><strong>2) We could define dynamic field properties because its in an function</strong></p>
  
  <p>When we define fields as an function we could define our object dynamically. But the field function would need some information (in our case information which would be passed to elements) and we would need to access them to build the field object.</p>
  
  <p>Example:</p>
  
  <pre><code>const MyType = new GraphQLObjectType({
    name: 'SomeType',
    fields: {
      name: {
        type: GraphQLString,
      },
      elements: {
        type: new GraphQLObjectType({
          name: 'elements',
          fields: (argsFromElements) =&gt; {
            // here we can now access keys from "args"
            const fields = {};
            argsFromElements.keys.forEach((key) =&gt; {
              // some logic here ..
              fields[someGeneratedProperty] = someGeneratedGraphQLType;
            });
            return fields;
          },
        }),
        args: {
          keys: {
            type: new GraphQLList(GraphQLString),
          },
        },
      },
      // ...
    },
  });
  </code></pre>
  
  <p>This could work but the question would be if there is a way to pass the args and/or resolve object to the fields.</p>
  
  <p><strong>Question</strong>
  So our question is now: Which way would be recommended in our case in GraphQL and is solution 1 or 2 possible ? Maybe there is another solution ?</p>
  
  <p><strong>Edit</strong>
  Solution 1 would work when using the ScalarType. Example:</p>
  
  <pre><code>type: new GraphQLScalarType({
          name: 'elements',
          serialize(value) {
            return value;
          },
        }),
  </code></pre>
  
  <p>I am not sure if this is a recommended way to solve our situation.</p>