Is it problematic to utilize data types to signify information that may be limited by permission settings?

Currently, I am engaged in developing a TypeScript and GraphQL system in which certain GQL requests’ results can vary based on the user's level of permission within the system. To illustrate this concept, let's consider quotes and line items.

When a user requests to view a quote, it may contain a list of line items with information like price and quantity. However, access to the price details differs among users. Presently, we are handling this by allowing most fields to be nullable, but this approach has proven to be challenging and lacks accuracy in representing the system. Our goal is to utilize the TypeScript and GraphQL type systems more effectively and accurately reflect the system dynamics.

We are considering creating specific types to define the data that can be returned at each permission level. For example,

interface LineItemWithPricing {
  id: string;
  name: string;
  description: string;
  cost: number;
  taxable: boolean;
  quantity: number;
}

interface LineItemWithoutPricing {
  id: string;
  name: string;
  description: string;
  quantity: number;
}

... (more attributes exist for a line item and other field combinations are possible)

type LineItem = LineItemWithPricing | LineItemWithoutPricing | <other options>;

(I'm skipping over the potential use of Pick and Omit for brevity and clarity.)

The main query that arises is whether incorporating permission levels into the type system for systems where data values and API return types are affected by permissions is a viable option. Is there a standard method for representing this? Are there superior alternatives available?

We have begun implementing some of these type strategies, and it seems feasible to delineate our system using sets of types like this. The concern revolves around encountering maintenance challenges in the long term.

Answer №1

I have experience working with both typed and un-typed languages, and I believe that types play a crucial role in enforcing business rules and maintaining clarity in code. Imagine if your type system is flawed, the data produced by your application will also be flawed. As companies expand, the importance of types as a software feature within your proprietary codebase will only increase. So, trust me on this one.

In the realm of software development, there are always superior options available. One approach is to implement business logic at the lowest layer of your data stack (typically the database level) along with utilizing types. This combination creates a powerful synergy that results in more accurate and reliable generated data overall.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Converting hexadecimal to binary using Javascript or Typescript before writing a file on an Android or iOS device

Hey everyone! I'm facing a puzzling issue and I can't seem to figure out why it's happening. I need to download a file that is stored in hex format, so I have to first read it as hex, convert it to binary, and then write it onto an Android/ ...

Another fetch is initiated unnecessarily following a React Apollo mutation

I am currently in the process of developing a small ToDo list application using React Apollo and GraphQL. When I want to add a new ToDo item, I click on the "Add" button which redirects me to a different URL containing a form. Upon submitting the form, a m ...

Is there a way to include values in the body of an HTTP GET request using Angular?

I've created a function in my service that looks like this: /** * Retrieve all data * @param sendSelectedValues string */ getAllActPlanBalanceYearData(sendSelectedValues: any): Observable<any> { const url = `/yearlyvalues/act-and ...

bringing TypeScript functions into another file

I am attempting to include a function in my main.ts file, but I keep encountering errors like 'is not a module' or 'unexpected import token' when I try to execute my file using node main.ts. These functions are not part of any node mod ...

Typescript error when using fill or justify prop in React-bootstrap with Typescript

Code import { useCallback, useState, useEffect } from 'react'; import { Tabs, Tab, Spinner, Alert } from 'react-bootstrap'; import { Categories } from '../../models/ICategory'; import IMovie from '../../models/IMovie&apo ...

Experimenting with a TypeScript custom hook for React testing

I have successfully created a customized hook called useForm using TypeScript and it is functioning properly. I have tested it with @testing-library/react-hooks and the tests have passed. However, TypeScript is flagging an issue in a specific location - as ...

MUI version 5 with styled-components and ListItemButton: The specified property 'to'/'component' is not recognized

While transitioning from MUI v4 to v5, I encountered a hurdle: in v4, I utilized makeStyles(), but now aim to fully switch to styled(). Unfortunately, I am struggling to get Typescript to recognize a styled(ListItemButton)(...) with to= and component= attr ...

Stopping or pausing an HTML5 audio element in Angular with component.ts

Is there a way to create a custom function in my component.ts file to pause an HTML audio element? I can't seem to find any built-in methods for pausing audio when using document.getElement. <audio controls id="audio-file" > <source src="sam ...

Encountering build issues after transitioning from Angular version 9.17 to 9.19

After upgrading from Angular 9.17 to 9.19, my ng build process started failing with an error related to variable declaration in both @types/node and zone.js: ERROR in node_modules/@types/node/ts3.5/globals.global.d.ts:1:13 - error TS2403: Subsequent varia ...

Using .on('mouseover', d => ..) does not yield the same `d` value as using .attr('foo', d => ..) in D3.js

I'm facing an issue with a mouseover tooltip in Observable that seems to fail when I transfer it to a Grafana plugin using React, D3, and Typescript. The technique I followed can be found in this article: Link to Article To simplify the code and deb ...

Problem with TypeScript involving parameter destructuring and null coalescing

When using parameter destructuring with null coalescing in TypeScript, there seems to be an issue with the optional name attribute. I prefer not to modify the original code like this: const name = data?.resource?.name ?? [] just to appease TypeScript. How ...

Angular 8 Issue: Absence of _body and Headers in Response

Our back-end code is written in C# within the .NET environment, targeting the 4.6.1 framework. Recently, our front-end was upgraded from Angular 4 to Angular 8. During this upgrade, webpack transitioned from version 2.3 to version 4.41 and typescript from ...

An unhandled promise rejection occurred because no routes could be found to match. URL Segment:

I'm facing an issue with my application where it doesn't recognize the route even though I have defined and imported it in app.module. Whenever I try to redirect to a specific route upon data retrieval, I encounter this exception: onSubmit(){ ...

How to efficiently use lunr in typescript?

The Definitely Typed repository demonstrates the importation in this manner: import * as lunr from 'lunr'; However, when attempting to use it in Stackblitz, it results in the following error: lunr is not a function Any ideas on how to resolve ...

Visibility in classes can shift once a new file is imported

Currently, I am encountering a puzzling issue in my angular2 project using typescript. In my main.ts file, which contains a component along with some imports at the start of the file, there is a custom type class (let's call it TypeFoo) located in mod ...

Dissimilarities in behavior between Angular 2 AOT errors

While working on my angular 2 project with angular-cli, I am facing an issue. Locally, when I build it for production using ng build --prod --aot, there are no problems. However, when the project is built on the server, I encounter the following errors: . ...

Sharing markdown content between two Vue.js components

I have a markdown editor in View A which is displaying the result in the current View. My goal is to share this result with another page, View B. In View A, there is a button that allows the user to share the markdown result with View B. I am using a texta ...

Implementing service injection within filters in NestJS

Looking to integrate nestjs-config into the custom exception handler below: import { ExceptionFilter, Catch, ArgumentsHost, Injectable } from '@nestjs/common'; import { HttpException } from '@nestjs/common'; import { InjectConfig } fro ...

Customize the Express Request type in TypeScript to handle the error: Request<ParamsDictionary, any, any, QueryString.ParsedQs, Record<string, any>>.signedCookies: any

Despite exploring numerous existing discussions on this topic, I have attempted 150 different solutions without success. It seems that the issue lies in my approach, but where exactly? The custom express types file that I've created consists of addin ...

Having trouble invoking an Angular 6 Service using an HTML EventListener

Within my angular ag-grid setup, I've implemented a cellRenderer and cellRendererParams. The cellRenderer calls a method to generate a button in each cell of the ag-grid. constructor(private notificationService: NotificationService) { } ngOnInit() { ...