Search code examples
typescriptnext.jsgraphqlapollo-server

GraphQL Nexus asNexusMethod showing type error


I implemented the DateTime as a nexus method as shown in the docs in my Next.js project.

// types/DateTime.ts
import { GraphQLDate, GraphQLDateTime, GraphQLTime } from 'graphql-iso-date';
import { asNexusMethod } from 'nexus';

export const GQLDate = asNexusMethod(GraphQLDate, 'date');
export const GQLTime = asNexusMethod(GraphQLTime, 'time');
export const GQLDateTime = asNexusMethod(GraphQLDateTime, 'datetime');

The file is also included in the types in GraphQL schema

// Other imports
import * as types from './types';

export const schema = makeSchema({
  types: types,
  // ...
});

However, I am getting following TypeScript error:

Argument of type 'GraphQLScalarType' is not assignable to parameter of type 'GraphQLNamedType'.
  Type 'GraphQLScalarType' is missing the following properties from type 'GraphQLScalarType<unknown, unknown>': specifiedByURL, [Symbol.toStringTag]ts(2345)

How do I resolve this error?


Solution

  • I used graphql-scalars and it worked perfectly, as shown here.

    import { DateTimeResolver } from 'graphql-scalars';
    
    const DateTime = asNexusMethod(DateTimeResolver, 'datetime');
    
    export const schema = makeSchema({
      types: [
        DateTime,
        /** Queries, mutations, and other scalar types here */
      ],
      /** more config options here */
    })
    
    // Inside type file
    t.nonNull.datetime('createdAt');
    

    If you are using any linting tool, just make sure that you hit the GraphQL API endpoint after adding the type. Otherwise, it keeps showing the type error in the IDE.