Skip to content

proto-graphql/graphql-field-mask

Repository files navigation

graphql-field-mask

CI Coverage Status npm LICENSE

google.protobuf.FieldMask from GraphQL query

Usage

import { FieldMask } from "google-protobuf/google/protobuf/field_mask_pb";
import { fieldMaskPathsFromResolveInfo } from "graphql-field-mask";

const queryType = new GraphQLObjectType({
  name: "Query",
  fields: {
    viewer: {
      type: User,
      resolve(_source, _args, ctx, info) {
        const paths = fieldMaskPathsFromResolveInfo("User", info);
        const mask = new FieldMask().setPathsList(paths);

        // ...
      }
    }
  }
})

Convert to snake case

import { snakeCase } from "change-case";
import { fieldMaskPathsFromResolveInfo, GetFieldNameFunc } from "graphql-field-mask";

const getFieldName: GetFieldNameFunc = ({ field }) => snakeCase(field.name);

resolve(_source, _args, ctx, info) {
  const paths = fieldMaskPathsFromResolveInfo("User", info, { getFieldName });
  const mask = new FieldMask().setPathsList(paths);

  // ...
}

With custom scalar

import { getNamedType, isScalarType } from "graphql";
import { fieldMaskPathsFromResolveInfo, GetFieldNameFunc } from "graphql-field-mask";

const getFieldName: GetFieldNameFunc = ({ field }) => {
  const fieldType = getNamedType(field.type);
  if (isScalarType(fieldType)) {
    switch (fieldType.name) {
    case 'Date':
      return ['year', 'month', 'day'].map(c => `${fieldName}.${c}`);
    // ...
    }
  }
  return field.name
};

resolve(_source, _args, ctx, info) {
  const paths = fieldMaskPathsFromResolveInfo("User", info, { getCustomScalarFieldMaskPaths });
  const mask = new FieldMask().setPathsList(paths);

  // ...
}

With extra fields

import { fieldMaskPathsFromResolveInfo, AddExtraFieldsFunc } from "graphql-field-mask";

const addExtraFields: AddExtraFieldsFunc = ({ field }) => {
  return (field.extension as { dependentFields?: string[] }).dependentFields ?? []
};

resolve(_source, _args, ctx, info) {
  const paths = fieldMaskPathsFromResolveInfo("User", info, { addExtraFields });
  const mask = new FieldMask().setPathsList(paths);

  // ...
}
import { ProtobufFieldExtensions, ProtobufMessageExtensions, ProtobufOneofExtensions } from "proto-nexus";
import { fieldMaskPathsFromResolveInfo, GetFieldNameFunc } from "graphql-field-mask";

const getFieldName: GetFieldNameFunc = ({ field }) => {
  const ext = (field.extensions ?? {}) as Partial<ProtobufFieldExtensions>;
  return ext.protobufField?.name  ?? null;
};

const getAbstractTypeFieldMaskPaths: GetAbstractTypeFieldMaskPathsFunc = (info, getFieldMaskPaths) => {
  const oneofExt = (info.abstractType.extensions ?? {}) as Partial<ProtobufOneofExtensions>;
  const objExt = (info.concreteType.extensions ?? {}) as Partial<ProtobufMessageExtensions>;
  const prefix = (oneofExt.protobufOneof.fields ?? []).find(f => f.type === objExt.protobufMessage?.fullName)?.name;
  return prefix ? getFieldMaskPaths().map(p => `${prefix}.${p}`) : []
}

resolve(_source, _args, ctx, info) {
  const paths = fieldMaskPathsFromResolveInfo("User", info, { getFieldName, getAbstractTypeFieldMaskPaths });
  const mask = new FieldMask().setPathsList(paths);

  // ...
}

Author