Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transform to filter arbitrary type fields #819

Closed
victorandree opened this issue May 31, 2018 · 4 comments
Closed

Add transform to filter arbitrary type fields #819

victorandree opened this issue May 31, 2018 · 4 comments
Labels
feature New addition or enhancement to existing solutions v5

Comments

@victorandree
Copy link
Contributor

victorandree commented May 31, 2018

Like FilterRootFields but for arbitrary types, in case you have fields that you do not want to expose. Suggested signature:

export type TypeFieldsFilter = (
  // Or `GraphQLObjectType | GraphQLInputObjectType | GraphQLInterfaceType`
  type: GraphQLNamedType,
  fieldName: string,
  field: GraphQLField<any, any>,
) => boolean;

export default class FilterTypeFields implements Transform {
  constructor(filter: TypeFieldsFilter) {}
}

This should be easy to implement with access to visitSchema (but that's not part of the public interface for this project). Quick implementation for object types. I realized that fields are different on object types, input object types and interfaces, so maybe each should have a separate transform...

import {
  GraphQLField,
  GraphQLFieldConfig,
  GraphQLObjectType,
  GraphQLSchema,
} from 'graphql';
import { Transform } from 'graphql-tools';
import {
  createResolveType,
  fieldToFieldConfig,
} from 'graphql-tools/dist/stitching/schemaRecreation';
import {
  visitSchema,
  VisitSchemaKind,
} from 'graphql-tools/dist/transforms/visitSchema';

export type TypeFieldsFilter = (
  type: GraphQLObjectType,
  fieldName: string,
  field: GraphQLField<any, any>,
) => boolean;

export default class FilterTypeFields implements Transform {
  private filter: TypeFieldsFilter;

  constructor(filter: TypeFieldsFilter) {
    this.filter = filter;
  }

  public transformSchema(originalSchema: GraphQLSchema): GraphQLSchema {
    return visitSchema(originalSchema, {
      [VisitSchemaKind.OBJECT_TYPE]: ((type: GraphQLObjectType) => {
        return filterFields(type, this.filter);
      }) as any,
    });
  }
}

function filterFields(type: GraphQLObjectType, filter: TypeFieldsFilter) {
  const resolveType = createResolveType((_name, originalType) => originalType);
  const fields = type.getFields();
  const newFields = {} as { [key: string]: GraphQLFieldConfig<any, any> };

  Object.keys(fields).forEach(fieldName => {
    const field = fields[fieldName];
    if (filter(type, fieldName, field)) {
      newFields[fieldName] = fieldToFieldConfig(field, resolveType, true);
    }
  });

  return new GraphQLObjectType({
    name: type.name,
    description: type.description,
    astNode: type.astNode,
    fields: newFields,
  });
}
@ghost ghost added the feature New addition or enhancement to existing solutions label May 31, 2018
@chollier
Copy link
Contributor

hey @victorandree since you basically have the code ready here, have you thought about making a PR to add the feature to graphql-tools?

@yaacovCR
Copy link
Collaborator

yaacovCR commented Apr 8, 2019

Fixed by @orta with #964

yaacovCR added a commit to yaacovCR/graphql-tools-fork that referenced this issue May 21, 2019
yaacovCR added a commit to yaacovCR/graphql-tools-fork that referenced this issue May 21, 2019
yaacovCR added a commit to yaacovCR/graphql-tools-fork that referenced this issue Jun 2, 2019
yaacovCR added a commit to yaacovCR/graphql-tools-fork that referenced this issue Jun 18, 2019
yaacovCR added a commit to yaacovCR/graphql-tools-fork that referenced this issue Sep 22, 2019
yaacovCR added a commit to yaacovCR/graphql-tools-fork that referenced this issue Sep 22, 2019
yaacovCR added a commit to yaacovCR/graphql-tools-fork that referenced this issue Sep 22, 2019
yaacovCR added a commit to yaacovCR/graphql-tools-fork that referenced this issue Oct 3, 2019
yaacovCR added a commit to yaacovCR/graphql-tools-fork that referenced this issue Oct 25, 2019
yaacovCR added a commit to yaacovCR/graphql-tools-fork that referenced this issue Oct 25, 2019
yaacovCR added a commit to yaacovCR/graphql-tools-fork that referenced this issue Nov 4, 2019
yaacovCR added a commit to yaacovCR/graphql-tools-fork that referenced this issue Dec 31, 2019
yaacovCR added a commit to yaacovCR/graphql-tools-fork that referenced this issue Dec 31, 2019
yaacovCR added a commit to yaacovCR/graphql-tools-fork that referenced this issue Jan 8, 2020
yaacovCR added a commit to yaacovCR/graphql-tools-fork that referenced this issue Jan 21, 2020
yaacovCR added a commit to yaacovCR/graphql-tools-fork that referenced this issue Feb 27, 2020
yaacovCR added a commit to yaacovCR/graphql-tools-fork that referenced this issue Mar 26, 2020
yaacovCR added a commit to yaacovCR/graphql-tools-fork that referenced this issue Mar 26, 2020
@kamilkisiela
Copy link
Collaborator

We recently released an alpha version of GraphQL Tools (#1308) that should fix the issue.

Please update graphql-tools to next or run:

npx match-version graphql-tools next

Let us know if it solves the problem, we're counting for your feedback! :)

@yaacovCR yaacovCR mentioned this issue Mar 27, 2020
22 tasks
@yaacovCR
Copy link
Collaborator

yaacovCR commented Apr 1, 2020

Folded into #1306

@yaacovCR yaacovCR closed this as completed Apr 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New addition or enhancement to existing solutions v5
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants