javascriptgraphqlgraphql-jsgraphql-javaexpress-graphql

How do I define a typeDefs in a schema for the following response?


I am trying to define a typeDefs for the following response in graphql:

CompactRes={
  "Compact": [
    [
      0,
      1,
      2,
      3
    ],
    [
      4,
      5,
      6,
      [
        7,
        2.567
      ]
    ]]}
  

Here is my schema:

const typeDefs = gql`
type Compact{
compact:[[Float]]
  }
 type Query {
    response(count: ID!): Compact
  }
`;
const resolvers = {
  Query: {
    response: (parent, args, context, info) =>{
      return CompactRes;
        },
     },
};
 

I'm getting the following error when I run this:

enter image description here


Solution

  • I just found a solution , We can achieve it by Mutation and Scalar

    const CompactRes={
      "Compact": [
        [
          0,
          1,
          2,
          3
        ],
        [
          4,
          5,
          6,
          [
            7,
            2.567
          ]
        ]]}
      
        const typeDefs = gql`
        scalar CompactType
        type Compact{
        compact:Mutation
          }
         type Query {
            response(count: ID!): Compact
          }
        type Mutation{
            getCompactResponse(count:ID):CompactType
        }
        `;
        const resolvers = {
          Query: {
            response: (parent, args, context, info) =>{
              return CompactRes;
                },
             },
          Mutation:{
            getCompactResponse(_,{input}){
              return compactResponse;
            }
          },
        };
    

    Output here