Retrieve a Collection of Tally on GraphQL

Having trouble creating a GraphQL query that can collect multiple counts in a React project? Unsure about the data type to use in this scenario?

Here's the code snippet:

    import {   GraphQLInt,   GraphQLNonNull } from 'graphql' import * as moment from 'moment' import orm from '../../orm' import authDecorator from '../helpers/auth-decorator'

interface IQueryMissionCountArgs {   authToken: string }

export default authDecorator({   requireAdmin: true,   requireAuth: true })({   type: Object(new GraphQLNonNull(GraphQLInt)),   async resolve (
      args: IQueryMissionCountArgs   ) {
    const lastWeekAcceptedMissionsCount = await orm.models.Mission.count({
      where: {
        createdAt: {
          $between: [moment().subtract(7, 'days').toDate(), new Date()]
        },
        status: 'accepted'
      }
    })
    const lastWeekPostedMissionsCount = await orm.models.Mission.count({
      where: {
        createdAt: {
          $between: [moment().subtract(7, 'days').toDate(), new Date()]
        }
      }
    })
    return {
      lastWeekPostedMissions: lastWeekPostedMissionsCount,
      lastWeekAcceptedMissions: lastWeekAcceptedMissionsCount,
    }   } })

This is causing the following error message:

Int cannot represent non 32-bit signed integer value: [object Object]\n

Answer №1

The field you have specified is expected to be an integer:

type: Object(new GraphQLNonNull(GraphQLInt))

However, you are returning an Object with two properties (lastWeekPostedMissions and lastWeekAcceptedMissions).

To resolve this issue, you should create a new GraphQLObjectType like so:

const WeeklyMissionSummary = new GraphQLObjectType({ name: 'WeeklyMissionSummary', fields: { lastWeekPostedMissions: { type: new GraphQLNonNull(GraphQLInt) }, lastWeekAcceptedMissions: { type: new GraphQLNonNull(GraphQLInt) }, }, })

Then assign this new object as the type for your field:

type: new GraphQLNonNull(WeeklyMissionSummary)

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

What is the process of type checking in Typescript when passing arguments?

I'm curious about TypeScript and why the two function calls below result in different type checking outcomes. Can someone shed some light on this for me? interface LabelledValue { label: string; } function printLabel(labelledObj: LabelledValue) ...

Using Express middleware in a TypeScript Express application

I'm currently converting the backend of an ExpressJS application to Typescript. While working on the auth.routes.ts file, I encountered an issue with the middleware (authMiddleware). It seems like there might be a typing error, as the same code in the ...

Exploring the use of generic types in TypeScript interfaces

I have the following two interfaces: export interface TestSchema<S> { data: S; description: string; } export type someType = 'option1' | 'option2'; export interface AnotherInterface { primary: string; secondary: someType; ...

Can you explain the distinction between Reflect.getMetadata and Reflect.getOwnMetadata?

Just like the title says, the reflect-metadata API comes with a method called getMetadata and another called getOwnMetadata. Can you explain the distinction between them? The same question applies to hasOwnMetadata, and so on. ...

"Enhancing User Interactions in React with TypeScript through Event Handling

In my current React and TypeScript project, I have a number button that is structured like this: <button className="num" type='button' value={'1'} onClick={inputHandler}>1</button> As you can see, I've includ ...

The SunEditor onChange event does not reflect updated state information

software version next: 12.0.7 suneditor: 2.41.3 suneditor-react: 3.3.1 const SunEditor = dynamic(() => import("suneditor-react"), { ssr: false, }); import "suneditor/dist/css/suneditor.min.css"; // Import Sun Editor's CSS Fi ...

Tips for running batch files prior to debugging in VS Code

Currently, I am working on a project using Typescript, nodeJS, and VS Code. When it comes to debugging in VS Code, I have set up configurations in my launch.json file. { "type": "node", "request": "launch", "name": "La ...

Cypress: Importing line in commands.ts is triggering errors

After adding imports to the commands.ts file, running tests results in errors. However, in commands.ts: import 'cypress-localstorage-commands'; /* eslint-disable */ declare namespace Cypress { interface Chainable<Subject = any> { c ...

utilizing mat-sort-header on a table column displaying the size of a nested array attribute within every record

Imagine I have a MatTableDataSource that is created using the following array: [ {propA: 'something', propB: ['a', 'b', 'c']}, {propA: 'somethingElse', propB: ['d', 'e', 'f']}] ...

Error with an Array of Objects in an Array when using Angular and Typescript

Our system has an array called "food/essen" with a total of 10 items. The food plan is made up of 8 objects from the "Food" array and includes an ID for the week number. An issue we are facing in our web application is that although it recognizes the 8 o ...

Guide to setting up value observation in React Context for optimal functionality

Imagine a scenario where there is a Parent Component that provides a Context containing a Store Object. This Store holds a value and a function to update this value. class Store { // value // function updateValue() {} } const Parent = () => { const ...

Troubleshooting a deletion request in Angular Http that is returning undefined within the MEAN stack

I need to remove the refresh token from the server when the user logs out. auth.service.ts deleteToken(refreshToken:any){ return this.http.delete(`${environment.baseUrl}/logout`, refreshToken).toPromise() } header.component.ts refreshToken = localS ...

Verify the type without making any assumptions about the checked type

Take a look at this type definition: interface ISmth<T> { id: number; data: T; } Now, I am interested in creating an array that contains elements of this type: var a = [{ id: 1, data: [], }, { id: 2, data: 4, }, { id: 3, data: "abc ...

Setting a callback function as a prop for react-paginate in TypeScript: A step-by-step guide

When using react-paginate, there is a prop called onPageChange with the following type: onPageChange?(selectedItem: { selected: number }): void; After implementing it like this: const onPageChange = (selected): void => { console.log(selected); } ...

Compilation in TypScript has encountered an error - The use of 'await' expressions at the top level is restricted unless the 'module' option is appropriately configured

When attempting to generate dynamic tests based on data from an excel sheet using TypeScript with TestCafe, everything runs smoothly... until I try to create the dynamic "tests" within the "fixture." This is my code: import { fixture, test, GFunctions } f ...

What is the proper way to refactor this TypeScript class?

Can you assist me in converting this class into TypeScript and explaining why it's not functioning? class Students { public name: string; public surname: string; public age: number; } constructor(name:string,surname:string,age:number) { ...

I am having trouble locating my TypeScript package that was downloaded from the NPM registry. It seems to be showing as "module not found"

Having some challenges with packaging my TypeScript project that is available on the npm registry. As a newcomer to module packaging for others, it's possible I've made an error somewhere. The following sections in the package.json appear to be ...

Verify the compatibility of the device with ScreenOrientation.lock() function - An error occurred indicating that screen.orientation.lock() is not supported on this particular device

I've been trying to implement ScreenOrientation.lock() according to the documentation, but I'm having trouble getting it to work correctly. https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation/lock When I use window.screen.orienta ...

Calculate variance between two numbers using Angular

Is there a way I can calculate the difference between two values in a table column and assign it to 'differenceInValues' variable? Any help would be appreciated. Thanks! <td scope="row"> {{change.oldValue}} </td> <td sc ...

Encountered problem in Angular with Cognito: Unable to access property 'user' of null in authorization.service.ts at line 29

For the purpose of understanding, I am working on implementing a proof of concept involving AWS Cognito and API Gateway. I have customized the UserPoolId and ClientId in the authorization.service.ts file based on the instructions in a video tutorial. The a ...