GraphQL - get all fields from a nested JSON object

I put the GraphQL shell on top of the outgoing REST API, as described in Zero to GraphQL in 30 minutes . I have an API endpoint for a product with one property that points to a nested object:

// API Response { entity_id: 1, nested_object: { key1: val1, key2: val2, ... } } 

Is it possible to define a schema so that I can get all this nested object without explicitly defining the nested object and all its properties? I want my query to simply indicate that I want a nested object, and I do not need to specify all the properties that I want from a nested object:

 // What I want { product(id: "1") { entityId nestedObject } } // What I don't want { product(id: "1") { entityId nestedObject { key1 key2 ... } } } 

I can make the second version, but it requires a lot of additional code, including creating NestedObjectType and specifying all attached properties. I also figured out how to automatically get a list of all keys, for example:

 const ProductType = new GraphQLObjectType({ ... fields: () => ({ nestedObject: { type: new GraphQLList(GraphQLString), resolve: product => Object.keys(product.nested_object) } }) }) 

I did not understand the way to automatically return the entire object.

+12
graphql
source share
2 answers

I can make the second version, but it requires a lot of additional code, including creating NestedObjectType and specifying all attached properties.

Do it! It will be great. This is the way to use GraphQL in full.

In addition to preventing oversampling, it also gives you many other benefits, such as type checking, and more readable and supported code, as your schema provides a more complete description of your data. Later, you will thank yourself for doing the extra work.

If for some reason you really do not want to go along this route, although you fully understand the consequences, you can encode nested objects as strings using JSON.stringify .

But, as I said, I do not recommend doing this!

+5
source share

You can try using the scalar JSON type. You can find more here (based on apollographql) .

  • add scalar JSON to the schema definition;
  • add {JSON: GraphQLJSON} to the resolution functions;
  • use the JSON type in Shema:
 scalar JSON type Query { getObject: JSON } 
  • request example:
 query { getObject } 
  • result:
 { "data": { "getObject": { "key1": "value1", "key2": "value2", "key3": "value3" } } } 

Main code:

 const express = require("express"); const graphqlHTTP = require("express-graphql"); const { buildSchema } = require("graphql"); const GraphQLJSON = require("graphql-type-json"); const schema = buildSchema(' scalar JSON type Query { getObject: JSON } '); const root = { JSON: GraphQLJSON, getObject: () => { return { key1: "value1", key2: "value2", key3: "value3" }; } }; const app = express(); app.use( "/graphql", graphqlHTTP({ schema: schema, rootValue: root, graphiql: true }) ); app.listen(4000); console.log("Running a GraphQL API server at localhost:4000/graphql"); 
0
source share

All Articles