Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

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

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
80
increased by142.42%
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 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 16 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