Skip to content

Instantly share code, notes, and snippets.

@ManUtopiK
Last active May 5, 2024 20:11
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ManUtopiK/469aec75b655d6a4d912aeb3b75af3c9 to your computer and use it in GitHub Desktop.
Save ManUtopiK/469aec75b655d6a4d912aeb3b75af3c9 to your computer and use it in GitHub Desktop.
Clean graphql response : Remove edges, node and __typename from graphql response
/**
* Remove edges, node and __typename from graphql response
*
* @param {Object} input - The graphql response
* @returns {Object} Clean graphql response
*/
const cleanGraphQLResponse = function(input) {
if (!input) return null
const output = {}
const isObject = obj => {
return obj !== null && typeof obj === 'object' && !Array.isArray(obj)
}
Object.keys(input).forEach(key => {
if (input[key] && input[key].edges) {
output[key] = input[key].edges.map(edge =>
cleanGraphQLResponse(edge.node)
)
} else if (isObject(input[key])) {
output[key] = cleanGraphQLResponse(input[key])
} else if (key !== '__typename') {
output[key] = input[key]
}
})
return output
}
@Haltarys
Copy link

Haltarys commented Sep 10, 2019

Merci! <3

@ManUtopiK
Copy link
Author

Your welcome!
I used it to clean response of https://github.com/wp-graphql/wp-graphql
Be careful with this function because it remove __typename which could be bad for cache of tools like apollo.

@geongeorge
Copy link

Good work! Had to add a primitive type check because this was splitting my strings to single character arrays

/**
 * Remove edges, node and __typename from graphql response
 *
 * @param {Object} input - The graphql response
 * @returns {Object} Clean graphql response
 */
const cleanGraphQLResponse = function (input) {
  if (!input) return null;

  const isPrimitiveType = (test) => {
    return test !== Object(test);
  };

  if (isPrimitiveType(input)) return input;

  const output = {};
  const isObject = (obj) => {
    return obj !== null && typeof obj === 'object' && !Array.isArray(obj);
  };

  Object.keys(input).forEach((key) => {
    if (input[key] && input[key].edges) {
      output[key] = input[key].edges.map((edge) =>
        cleanGraphQLResponse(edge.node),
      );
    } else if (isObject(input[key])) {
      output[key] = cleanGraphQLResponse(input[key]);
    } else if (key !== '__typename') {
      output[key] = input[key];
    }
  });

  return output;
};

@abezhinaru
Copy link

abezhinaru commented Mar 10, 2023

function transformObject(obj) {
  if (!obj || ["string", "boolean", "number"].includes(typeof obj)) {
    return obj;
  }

  if (Array.isArray(obj)) {
    return obj.map(transformObject);
  } else if (obj.hasOwnProperty("edges")) {
    return obj.edges.map((edge) => transformObject(edge.node));
  }

  return Object.keys(obj).reduce((result, key) => {
    const r = transformObject(obj[key]);
    if (key === "node") {
      return r;
    }
    result[key] = r;
    return result;
  }, {});
}

// transformObject(query?.edges);

@samuelko123
Copy link

Typescript

type Json =
  | { [key: string]: Json }
  | Json[]
  | string
  | number
  | boolean
  | null;

type JsonObj = { [key: string]: Json };

function transformObject(obj: Json): Json {
  if (isObject(obj)) {
    if (obj.edges && Array.isArray(obj.edges)) {
      return obj.edges.map((edge) => {
        if (edge && isObject(edge)) {
          return transformObject(edge.node);
        }

        return edge;
      });
    }

    return Object.keys(obj).reduce((result: JsonObj, key) => {
      const value = obj[key];
      result[key] = isObject(value) ? transformObject(value) : obj[key];
      return result;
    }, {} as JsonObj);
  }

  if (Array.isArray(obj)) {
    return obj.map(transformObject);
  }

  return obj;
}

function isObject(input: Json): input is JsonObj {
  return typeof input === "object" && input !== null && !Array.isArray(input);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment