'GraphQLJSONObject' refers to a value, but is being used as a type here. Did you mean 'typeof GraphQLJSONObject'?ts(2749)

Viewed 127

I have set up Typescript with NodeJs for my backend server. In one of the entity files, I am using GraphQLJSONObject from the package graphql-type-json.

Example:

import { GraphQLJSONObject } from 'graphql-type-json';

...

  @Field(() => GraphQLJSONObject, { nullable: true })
  field_name: GraphQLJSONObject; //error line

while compiling, I am getting an error as:

'GraphQLJSONObject' refers to a value, but is being used as a type here. Did you mean 'typeof GraphQLJSONObject'?ts(2749)

This is happening for all the type occurrences of GraphQLJSONObject across the repository.

Example 2: In the case of field resolvers. For example:

  async user_details(
    @Root() parent: Promise<User>,
  ): Promise<GraphQLJSONObject> { //error
    const user = await parent;
    if (user.is_super_admin) {
      return { superAdmin: user.is_super_admin };
    } else return null;
  }

If I add Promise<typeof GraphQLJSONObject> instead of Promise<GraphQLJSONObject> then I start getting errors:

Type '{ superAdmin: true; }' is not assignable to type 'GraphQLScalarType'.ts(2322)

Type 'null' is not assignable to type 'GraphQLScalarType'.ts(2322)

How shall I fix this, Looking forward to help on this.

1 Answers

Error is very self-explanatory. You should use typeof GraphQLJSONObject for the field type.

You can refer to this doc

@Field(() => typeof GraphQLJSONObject, { nullable: true })
field_name: typeof GraphQLJSONObject; 
Related