graphql icon indicating copy to clipboard operation
graphql copied to clipboard

Define resolver for Custom scalar type

Open valburyakov opened this issue 8 years ago • 6 comments

I'm trying to migrate my old nestjs app with graphQL to new GraphqlModule. And i faced with problem how to define resolver for scalar type, previously i had:

scalar Date

type User {
  _id: ID!
  fullName: String
  email: String
  hireDate: Date
  pictureUrl: String
}
import { GraphQLScalarType } from 'graphql';
import { Kind } from 'graphql/language';

export const resolver = {
  Date: new GraphQLScalarType({
    name: 'Date',
    description: 'Date custom scalar type',
    parseValue(value) {
      return new Date(value); // value from the client
    },
    serialize(value) {
      return value.getTime(); // value sent to the client
    },
    parseLiteral(ast) {
      if (ast.kind === Kind.INT) {
        return parseInt(ast.value, 10); // ast value is always in string format
      }

      return null;
    }
  })
};

So how do i define resolver for Date type using nestjs annotations ? I've tried this but it doesn't work

@Resolver('Date')
export class DateResolver {

  @DelegateProperty('Date')
  getDate() {
    return new GraphQLScalarType({
      name: 'Date',
      description: 'Date custom scalar type',
      parseValue(value) {
        return new Date(value); // value from the client
      },
      serialize(value) {
        return value.getTime(); // value sent to the client
      },
      parseLiteral(ast) {
        if (ast.kind === Kind.INT) {
          return parseInt(ast.value, 10);
        }

        return null;
      }
    });
  }
}

valburyakov avatar Jan 28 '18 21:01 valburyakov

@valburyakov I also had a problem with it.

Solution:

date-time.scalar-type.ts

// ...
export const GraphQLDateTime = new GraphQLScalarType({
  name: 'DateTime',
  parseValue(value) {
    return new Date(value);
  },
  serialize(value) {
    return moment(value).toISOString();
  },
  parseLiteral(ast) {
    if (ast.kind === Kind.STRING) {
      return new Date(ast.value);
    }

    return null;
  },
});

bootstrap.ts

/** Scalar types **/
import { scalarTypes } from './type-implementations';

// ....

export class AppModule {

  // .........

  private createSchema(): GraphQLSchema {
    const schema = this.graphQLFactory.createSchema({
      typeDefs: [
        this.graphQLFactory.mergeTypesByPaths('./**/*.graphqls'),
      ],
      resolvers: {
        ...scalarTypes,
      },
      directiveResolvers: {},
    });

    const delegates = this.graphQLFactory.createDelegates();

    return mergeSchemas({
      schemas: [
        schema,
      ],
      resolvers: delegates,
    });
  }
}

But resolvers in createSchema function have problem, because this function overwrite resolvers property. I created pull request to fix it. Maybe there are other better solutions, but I have not found one.

pawpartyka avatar Jan 30 '18 15:01 pawpartyka

@partyka95 Thx for the solution. But for me it still doesn't work, I did like in your example but no resolver for custom scalar is loaded:

app.module.ts

createSchema() {
    const typeDefs = this.graphQLFactory.mergeTypesByPaths('./**/*.graphql');
    const schema = this.graphQLFactory.createSchema({
      typeDefs,
      resolvers: {
        Date: GraphQLDateTime
      },
      directiveResolvers: {},
    });

    const delegates = this.graphQLFactory.createDelegates();

    return mergeSchemas({
      schemas: [
        schema,
      ],
      resolvers: delegates
    });
  }

Or this solution is working only with your fix in PR ? And show your scalarTypes .

valburyakov avatar Feb 01 '18 13:02 valburyakov

@valburyakov :

https://github.com/excitement-engineer/graphql-iso-date

https://github.com/nestjs/graphql/pull/8

pawpartyka avatar Feb 02 '18 13:02 pawpartyka

Another temporary solution :

  import { ResolversExplorerService } from '@nestjs/graphql/resolvers-explorer.service'
  import { makeExecutableSchema } from 'graphql-tools'

  const resolverFunctions = {
    CustomScalar: myCustomScalarType
  }

  (...)

  createSchema() {
    const typeDefs = this.graphQLFactory.mergeTypesByPaths('./**/*.gql')
    //const schema = this.graphQLFactory.createSchema(typeDefs)

    const resolvers = this.resolversExplorerService.explore()
    Object.assign(resolvers, customScalarsFunctions)

    const schema = makeExecutableSchema({
      typeDefs,
      resolvers: resolvers,
    })
    return schema
  }

dcasier avatar Feb 27 '18 21:02 dcasier

Fixed thanks to #8 pull request. I'll push it together with the incoming release 🙂

kamilmysliwiec avatar Mar 28 '18 20:03 kamilmysliwiec

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Apr 25 '20 15:04 lock[bot]