简体   繁体   中英

GraphQL schema type for object with dynamic keys

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?

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]!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM