Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL schema type for object with dynamic keys

Tags:

graphql

I'm using an external API as my source for data so I can't control what the keys are in the records returned. I have a record with an object like this in it:

images: {
  '50': 'https://mir-s3-cdncf.behance.net.jpg',
  '100': 'https://mir-s3-cdn-cf.behance.net/user/100/579c455d13419.jpg',
  '115': 'https://mir-s3-cdn-cf.behance.net/user/115/.jpg',
  '138': 'https://mir-s3-cdn-cf.behance.net/user/138/.jpg'
}

I can't tell what the keys are going to be in this object up front. Is there a way to handle this in a GraphQL schema?

like image 917
sturoid Avatar asked Oct 29 '25 17:10

sturoid


1 Answers

I got this working with the package "graphql-type-json". Here is the code using that package:


// Define new JSON types.
const AppTypes = `
  scalar JSON
  scalar JSONObject
`;

// Define resolvers for the new types which point to the types from the library.
const { GraphQLJSON, GraphQLJSONObject } = require('graphql-type-json');

const AppResolvers = {
  JSON: GraphQLJSON,
  JSONObject: GraphQLJSONObject
};

// Use the types.
entityTree: [JSONObject]!
like image 111
sturoid Avatar answered Oct 31 '25 11:10

sturoid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!