简体   繁体   中英

schema type for an unknown object in graphql

I have the following object coming back from db:

{
  "total_rows": 200,
  "bookmark": "g1AAAABteJzLYWBgYMpgTmEQTM4vTc5ISXIwNDLXMwBCwxyQVCJDUv3___-zMpjc7D8wgEEiCx71eSwgJQ1A6j-GtiwA6MscCg",
  "rows": [
      {
          "id": "51a1ff51b3b4719d05e40ac4bb0d0566",
              "objects": {
                  "0": {
                      "type": "ipv4-addr",
                      "value": "192.168.1.10",
                      "resolves_to_refs": "2"
                  },
                  "1": {
                      "type": "network-traffic"
                  }
        }
  ],
  "counts": {
      "created_by_ref": {
          "0203a7e6-b174-4af9-812d-ab889816e868": 1,
          "0250789a-14c3-4751-b4a0-c017af82b8f1": 1,
          "03c63db6-2a84-4627-88be-a83208d524e6": 1,
          "05cba3da-11ff-4a7a-aae9-0b1614cd5300": 1,
          "fc825d33-26ea-4563-9478-2e1887b87112": 1
      },
      "file.hashes.MD5": {
          "UNDEFINED": 200
      },
      "file.name": {
          "UNDEFINED": 200
      },
      "ipv4_addr.value": {
          "127.0.0.1": 200,
          "192.168.1.10": 200
      },
      "last_observed": {
          "1583503380000": 5,
          "1583589780000": 9,
          "1585749840000": 12
      }
  },
  "num_of_rows": 10
}

I am trying to fit in a graphql schema to the above. I have the following which works paryially:

const graphql = require("graphql");

const { GraphQLObjectType, GraphQLString, GraphQLSchema, GraphQLInt, GraphQLList } = graphql;


const SearchResultType = new GraphQLObjectType({
  name: "SearchResult",
  fields:  ()=>({
    total_rows: { type: GraphQLInt },
    bookmark: { type: GraphQLString },
    //rows: { type: new GraphQLList(GraphQLInt) },
    num_of_rows: { type: GraphQLInt }
  })
});




const RootQuery = new GraphQLObjectType({
  name: "RootQueryType",
  fields: {
    searchResult:{
      type: SearchResultType,
      args: { id: { type: GraphQLString } },
      resolve(parentValue: any, args: any) {
        console.log(args)
        return resultMock;
      }
    }
  }
});

module.exports = new GraphQLSchema({
  query: RootQuery,
});

The above works for those data types which are defined. However there are some objects in the mockResult above like:

  "objects": {
      "0": {
          "type": "ipv4-addr",
          "value": "192.168.1.10",
          "resolves_to_refs": "2"
      },
      "1": {
          "type": "network-traffic"
      }

or

  "counts": {
      "created_by_ref": {
          "0203a7e6-b174-4af9-812d-ab889816e868": 1,
          "0250789a-14c3-4751-b4a0-c017af82b8f1": 1,
          "03c63db6-2a84-4627-88be-a83208d524e6": 1,
          "05cba3da-11ff-4a7a-aae9-0b1614cd5300": 1,
          "fc825d33-26ea-4563-9478-2e1887b87112": 1

So as you see these objects keys are random or at least not guessable until we receive them. Is there any way I can define a sth like this: rows: { type: new GraphQLList(any random object we do not know ) }, as a type in schema below:

const SearchResultType = new GraphQLObjectType({
  name: "SearchResult",
  fields:  ()=>({
    total_rows: { type: GraphQLInt },
    bookmark: { type: GraphQLString },
    rows: { type: new GraphQLList(any random object we do not know ) },
    num_of_rows: { type: GraphQLInt }
  })
});

You can use the GraphQL JSON Scalar (for example from this implementation ). I would not recommend doing this though (in fact years ago I did a talk "GraphQL JSON Scalar considered harmful"). Instead, you might want to transform map-like objects into lists of key-value pairs.

So for example for your counts object you could do the following:

type CreatedByRef {
  key: ID
  count: Int
}
Object.keys(counts.created_by_ref).map(key => ({
  key,
  count: counts.created_by_ref[key],
}));

This will change the shape of the result but preserve all the properties of GraphQL.

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