I am calling a report API with Nodejs which gives me a JSON response. The api can be called with different ID (in this case different report id). For example:
https://report.domain.io/report/1
https://report.domain.io/report/2
and so on. As the id is different in different time it will give me different responses. The attribute/s name will be different. So I cannot map it in a GraphQLObjectType. For example if the JSON response is following:
[
{
"modelbrand": "Sony",
"modelcode": "F3111",
"avg_free_storage": 225401514
},
{
"modelbrand": "Sony",
"modelcode": "F3111",
"avg_free_storage": 224547840
}
]
Then I can map it with GraphQL like following:
Report() {
return new graphql.GraphQLObjectType({
name: "Report",
description: "This represents report",
fields: () => ({
"modelbrand": {type: graphql.GraphQLString},
"modelcode": {type: graphql.GraphQLString},
"avg_free_storage": {type: graphql.GraphQLString}
})
});
}
cause I know the attributes are fixed. But if the attribute/s name like modelbrand, modelcode changes to something else (obviously it will be different for different report) then I cannot map it in GraphQLObjectType. In that case, I need a another type (which should be a JSON type) to map these responses. Unfortunately, it is not available as primitive type for GraphQL. I am not finding any custom/scalar type or node library to solve this problem. There is a solution in stack overflow but seems like it is not working.
Could I please get a solution for that? An Nodejs example will be appreciated. Thanks.
For example, the graphql-type-json package defines the GraphQLJSON object, which is an instance of GraphQLScalarType . You can use this object to define a JSON scalar that accepts any value that is valid JSON.
const filterType = new GraphQLObjectType ({ name: 'filterType', fields: { fieldName: { type: GraphQLString }, fieldValues: { type: GraphQLString }, filterType: { type: GraphQLString }, } }) const QueryType = new GraphQLObjectType({ name: 'Query', fields: () => ({ accounts: { type: new GraphQLList(accountType), args: { ...
A GraphQL object type has a name and fields, but at some point those fields have to resolve to some concrete data. That's where the scalar types come in: they represent the leaves of the query. We know this because those fields don't have any sub-fields - they are the leaves of the query.
GraphQL services typically respond using JSON, however the GraphQL spec does not require it. JSON may seem like an odd choice for an API layer promising better network performance, however because it is mostly text, it compresses exceptionally well with GZIP.
Please refer this graphql-type-json. This can be used as custom scalar json type.
In schema file
scalar JSON
type User {
id: ID!
userDetails: JSON
}
type Query {
getUser(id: ID!): User
}
In resolver file
import GraphQLJSON from 'graphql-type-json';
import {User} from './connectors';
const resolvers = {
Query: getUser(_, args) {
return User.find({
where: args
});
},
JSON: {
__serialize(value) {
return GraphQLJSON.parseValue(value);
}
}
}
This may help you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With