Skip to content

Custom scalar types in GraphQL schema language? #497

@ggregoire

Description

@ggregoire

Hi,

I tried to use a custom scalar type Date in my schema, written with the GraphQL schema language.

It seems that buildSchema takes only one param.
Is there a way to "register" my custom type in order to let buildSchema know what is the Date type?

import { buildSchema } from 'graphql';
import { GraphQLScalarType } from 'graphql/type';

const Date = new GraphQLScalarType({
  name: 'Date',
  serialize(value) {
    return value;
  },
});

const schema = buildSchema(`
  scalar Date

  type User {
    id: Int
    firstName: String
    lastName: String
    sex: String
    birthDate: Date
  }

  type Query {
    users(id: Int): [User]
  }
`);

const root = {
  users: async ({ id }) => {
    const users = db('users');
    return await id ? users.where('id', id) : users;
  },
}

The value of birthDate is 1986-02-11 07:13:14. I expect the same after the serialization.

My query in graphiQL:

{
  users(id: 1) {
    id
    firstName
    lastName
    sex
    birthDate
  }
}

The result:

{
  "data": {
    "users": [
      {
        "id": 1,
        "firstName": "Midas",
        "lastName": "Bokma",
        "sex": "m",
        "birthDate": null
      }
    ]
  },
  "errors": [
    {
      "message": "Expected a value of type \"Date\" but received: Tue Feb 11 1986 07:13:14 GMT-0600 (CST)",
      "locations": [
        {
          "line": 7,
          "column": 5
        }
      ]
    }
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions