Utilize inherited interface properties in implemented types for graphql operations

I am working with an interface called Book that includes 10 fields in TypeScript:

interface Book {
 field1: String
 field2: String
 field3: String
 # it has 10 fields
}

Now, I want to implement this interface in 10 different types without having to manually copy and paste all the fields. Is there a way to do this efficiently?

For instance, is there a shortcut like the following:

type textBook implements Book{
  ...Book Fields
}

Answer №1

When it comes to avoiding repetition in syntax, there isn't a straightforward solution. If an interface defines a field, each implementing type must explicitly define that field. One option is to use graphql-tag for creating type definitions and utilize interpolation to reduce redundancy. However, the trade-off here is decreased code readability, which may not be worth it in the long run. Additionally, this approach limits your ability to store type definitions in separate .graphql files.


const sharedFields = `
  a: String
  b: String
`
const typeDefs = gql`
  type A {
    ${sharedFields}
  }
  type B {
    ${sharedFields}
  }
`

If the shared fields are conceptually related, another strategy could be to group them into a separate type.


interface Book {
  bookFields: BookFields
}

type BookFields {
  field1: String
  field2: String
  field3: String
  # etc.
}

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

Display the variance in values present in an array using Node.js

I am facing a challenge and need help with printing arrays that contain both same and different values. I want to compare two arrays and print the values that are present in both arrays in one array, while also creating another array for the differing val ...

Storing JWT securely in cookie or local storage for a Node.js/Angular 2 web application

I've been researching how to save jwt tokens in either local storage or cookies but I'm having trouble finding clear instructions online. Can someone provide guidance on how to instruct the server to recognize a user for future sessions? //a ...

Discovering old (potentially neglected) npm dependencies: strategies for locating outdated packages

I am aware of the npm outdated command, which shows out-of-date dependencies only if a new version has been published. However, in certain cases (such as abandoned projects), there may not be a new version released and the package could still be considere ...

Looking to retrieve country, state, city, and area based on inputting a pincode value using Node.js?

I'm currently working on a web project with nodeJs and ejs. I am looking for a solution that can automatically update the country, state, city, and area fields based on the input of a pin-code (zip-code). Are there any recommended packages in node js ...

Exploring multipart uploads in Express JS 4: Leveraging body parser and dicer for efficient file handling

Currently, with express 4 and body-parser set up as shown below: var bodyParser = require('body-parser'); ... app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); After integrating dicer, it seems like body parser ...

Enable the upsert option in MongoDB Mongoose Collection.findAndModify: true

I'm currently working on implementing a user-to-user messaging feature within my NodeJS, Express, MongoDB application. The approach I'm taking involves utilizing two MongoDB documents: 'Messages' to store individual messages and ' ...

Renew Firebase Token

Currently, my email and password authentication flow in web Firebase JavaScript involves generating a token that I then verify on my node.js backend using firebase-admin. To make things easier, I store this generated token in the browser's local/sessi ...

Encountering a socket's unavailability due to pre-bind connection

I've been utilizing zeromq in my node.js server to send messages to my worker. To accomplish this, I create a socket named "router" on the node.js side. var sender = zmq.socket('router'); sender.bind('tcp://127.0.0.1:6633', functi ...

Implementing a notification system similar to Stack Overflow in a Node.js and MongoDB application

Apologies if my inquiry seems vague, but let me explain my situation. I have a forum similar to StackOverflow, built on nodejs and mongodb. The forum includes posts and comments. I am in need of implementing a notification system that alerts users when a n ...

Sending JSON data back to the client from a Node.js server

I am currently working on a node.js and express script that takes a file, executes a script on it, and logs the stdout to the console. However, I am looking to send this output, which is in JSON format, back to the client in a response instead of just lo ...

CommunicationsError on Socket.io server

I am currently experiencing an issue with a Socket.io server that is running through an Express.js server with Next.js. The server is sending errors without any client being connected. However, clients can still connect from the browser without any proble ...

Circular Reference: When a model has multiple owners in a belongsTo relationship

I am a beginner with loopback and I am currently exploring ACL implementation. Within my application, I have a "PersistedModel" named 'Page', which has relationships with two other models: 'Employee' and 'Customer'. Both of t ...

Sending both files and JSON data in a single request using the express framework in Node.js

Below is the JADE form: <form id="formAddchallandetails" action="/adddata" method="post" name="adduser"> <input id="inputunloadingDestination1" type="text" name="finalchallan[1][unloadingDestination]" placeholder="unloading Destination"> ...

Displaying the data from a database on a browser using node.js, MySQL, and Jade

Currently, I am delving into Node.js, Express.js, and Jade while utilizing a MySQL database. As a newcomer to node.js, I decided to start with something simple: presenting data from the database in a table on the browser. Unfortunately, my attempts have no ...

Guiding towards the correct form depending on which button is selected

For my current project, I am utilizing the MEAN stack. I have successfully created multiple registration forms. One of these forms is index.html which utilizes Angular to display various views/tabs within it, making it a multi-paged form. Another registr ...

View the list of cron jobs in PM2

Greetings, I have initiated two cron jobs using pm2 and am curious if there is a method to display a list of these cron jobs. Both of the cron jobs are responsible for running a shell script to control the server's start/stop functionality. Addition ...

Setting up Babel to effectively function across multiple directories

I've been utilizing Babel for some time now and it effectively transforms my ES Next code to the dist directory using this command: babel src --out-dir dist --source-maps --copy-files Up until now, all my JavaScript code has been stored in the src fo ...

Encountering a gyp error while trying to run npm install on Ubuntu

I've recently started working with React and I'm facing an issue while trying to clone a git repository. When I attempted to run npm install to set up the necessary dependencies, I encountered an error on my initial attempt. The error message wa ...

How are objects typically created in Node.js applications?

These code snippets are from Node.js tests, and I am curious about why one method of instantiating an object is favored over another? // 1 var events = require('events'); var emitter = new events.EventEmitter(); emitter.on('test', doSo ...

The console.log for a GET request in Express Router is displaying undefined parameters

After searching extensively on SO for a question similar to mine, I have come to the realization that my issue should be quite straightforward. The closest thread I discovered was this link. My current focus is on learning Node.JS, and I am in the process ...