Socket
Socket
Sign inDemoInstall

graphql-field-mask

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-field-mask

google.protobuf.FieldMask from GraphQL query


Version published
Weekly downloads
107
increased by1.9%
Maintainers
1
Weekly downloads
 
Created
Source

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);

  // ...
}

With ProtoNexus

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

FAQs

Package last updated on 17 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc