Sha256: e2825eb382bef566826d758bc725d01fe6d6cd88a0fab5d4d9be1e3188253056

Contents?: true

Size: 1.04 KB

Versions: 56

Compression:

Stored size: 1.04 KB

Contents

import graphql, { filter } from "graphql-anywhere";

/**
 * A simple resolver which returns object properties to easily
 * traverse a graphql response
 * @param {String} fieldName - An object property
 * @param {Object} root - An object
 * @returns {any} - An object's property value
 */
const resolver = (fieldName: string, root: any) => root[fieldName];

/**
 * A helper function to mock a graphql api request and return its
 * result. The result can be filtered by the same query so it just
 * returns a data subset.
 * @param {String} document - A graphql query document
 * @param {options} options - An object with optional options
 * @returns {Object} - The result of the query filtered or not
 */
const resolveGraphQLQuery = (document: any, options: any = {}) => {
  const { filterResult, rootValue, context, variables } = options;

  const result = graphql(
    resolver,
    document,
    rootValue,
    context,
    variables,
  );

  if (filterResult) {
    return filter(document, result);
  }
  return result;
};

export default resolveGraphQLQuery;

Version data entries

56 entries across 56 versions & 2 rubygems

Version Path
decidim-comments-0.4.1 app/frontend/support/resolve_graphql_query.ts
decidim-0.4.3 decidim-comments/app/frontend/support/resolve_graphql_query.ts
decidim-0.4.2 decidim-comments/app/frontend/support/resolve_graphql_query.ts
decidim-0.4.1 decidim-comments/app/frontend/support/resolve_graphql_query.ts
decidim-comments-0.4.0 app/frontend/support/resolve_graphql_query.ts
decidim-0.4.0 decidim-comments/app/frontend/support/resolve_graphql_query.ts
decidim-comments-0.3.2 app/frontend/support/resolve_graphql_query.ts
decidim-0.3.2 decidim-comments/app/frontend/support/resolve_graphql_query.ts
decidim-comments-0.3.1 app/frontend/support/resolve_graphql_query.ts
decidim-0.3.1 decidim-comments/app/frontend/support/resolve_graphql_query.ts
decidim-comments-0.3.0 app/frontend/support/resolve_graphql_query.ts
decidim-0.3.0 decidim-comments/app/frontend/support/resolve_graphql_query.ts
decidim-comments-0.2.0 app/frontend/support/resolve_graphql_query.ts
decidim-0.2.0 decidim-comments/app/frontend/support/resolve_graphql_query.ts
decidim-comments-0.1.0 app/frontend/support/resolve_graphql_query.ts
decidim-0.1.0 decidim-comments/app/frontend/support/resolve_graphql_query.ts