In GraphQL, how can I specify nested arrays as a field type?

In GraphQL, I am trying to create a GeoJSON object type.

When I specify a 4-dimensional array of GraphQLFloat s, I get an error when starting my server:

 Error: Decorated type deeper than introspection query. 

The type definition is as follows:

 var GraphQLGeoJSON = new GraphQLObjectType({ name: 'GeoJSON', fields: { type: { type: GraphQLString, resolve: (obj) => obj.type, }, coordinates: { type: new GraphQLList(new GraphQLList(new GraphQLList(new GraphQLList(GraphQLFloat)))), resolve: (obj) => obj.coordinates, } } }); 

How can I solve this error? This is the place where it was selected in the initial state:

https://github.com/graphql/graphql-js/blob/568dc52a4f9cc9bdec4f9283e6e528970af06cde/src/utilities/buildClientSchema.js#L105

+5
source share
1 answer

As a result, we defined the scalar type GeoJSON instead of the type of the object. This would allow us to perform rigorous checks against the GeoJSON specification. So far, just to keep us moving, we have defined a (not fully implemented) custom type of GeoJSON:

 var GeoJSON = new GraphQLScalarType({ name: 'GeoJSON', serialize: (value) => { // console.log('serialize value', value); return value; }, parseValue: (value) => { // console.log('parseValue value', value); return value; }, parseLiteral: (ast) => { // console.log('parseLiteral ast', ast); return ast.value; } }); 

... which allows us to use it as follows:

 var Geometry = new GraphQLObjectType({ name: 'Geometry', fields: () => ({ id: globalIdField('Geometry'), geojson: { type: GeoJSON, }, }, }; 

You can use this strategy to define a custom type to represent an array of arrays or define a custom type to represent only nested arrays, and then use new GraphQLList(CoordinatesType) , etc. It depends on the data you are modeling.

+6
source

Source: https://habr.com/ru/post/1237121/


All Articles