Graphql-tools: Unable to add date scalar type

Created on 12 Nov 2016  路  4Comments  路  Source: ardatan/graphql-tools

I'm trying to add a Date scalar type as per the docs. But schema compilation throws an error.

http://dev.apollodata.com/tools/graphql-tools/scalars.html

Code

const rootSchema = [`

scalar Date

type User {
    id: String
    created: Date
}

type Query {
 removed to keep things terse 
}

schema {
  query: Query
}

`];

const rootResolvers = {
    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: any) {
            if (ast.kind === Kind.INT) {
                return parseInt(ast.value, 10); // ast value is always in string format
            }
            return null;
        },
    }),
    Query: {
    },
};

// Compose schemas / resolvers
    const schema = [...rootSchema];
const resolvers = Object.assign({}, rootResolvers);

// Create schema
const executableSchema = makeExecutableSchema({
    typeDefs: schema,
    resolvers,
});

export default executableSchema;

Error

/Users/Danijel/projects/am/am-api/node_modules/graphql-tools/dist/schemaGenerator.js:224
[1]                 throw new SchemaError(typeName + " was defined in resolvers, but it's not an object");
[1]                 ^
[1] Error: Date was defined in resolvers, but it's not an object
[1]     at /Users/Danijel/projects/am/am-api/node_modules/graphql-tools/dist/schemaGenerator.js:224:23
[1]     at Array.forEach (native)
[1]     at /Users/Danijel/projects/am/am-api/node_modules/graphql-tools/dist/schemaGenerator.js:215:49
[1]     at Array.forEach (native)
[1]     at addResolveFunctionsToSchema (/Users/Danijel/projects/am/am-api/node_modules/graphql-tools/dist/schemaGenerator.js:210:35)
[1]     at _generateSchema (/Users/Danijel/projects/am/am-api/node_modules/graphql-tools/dist/schemaGenerator.js:44:5)
[1]     at Object.makeExecutableSchema (/Users/Danijel/projects/am/am-api/node_modules/graphql-tools/dist/schemaGenerator.js:56:20)
[1]     at Object.<anonymous> (/Users/Danijel/projects/am/am-api/lib/graphQL/schema.js:208:42)
[1]     at Module._compile (module.js:556:32)
[1]     at Object.Module._extensions..js (module.js:565:10)
[1]     at Module.load (module.js:473:32)
[1]     at tryModuleLoad (module.js:432:12)
[1]     at Function.Module._load (module.js:424:3)
[1]     at Module.require (module.js:483:17)
[1]     at require (internal/module.js:20:19)
[1]     at Object.<anonymous> (/Users/Danijel/projects/am/am-api/lib/router.js:5:18)

Most helpful comment

same issue for custom enums. Any solutions provided?

All 4 comments

Hi, for your information there have been recent modifications on the custom scalars. See https://github.com/apollostack/graphql-tools/pull/189

Just a question : which version of the graphql-tools npm package do you have in node_modules/ ?

@oricordeau it looks like this is broken for 0.8.1. It almost looks like the docs were rolled out without the code to support it!!

Should be fixed in 0.8.2! Thanks for reporting and sorry for the delay.

same issue for custom enums. Any solutions provided?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brennantaylor picture brennantaylor  路  4Comments

freiksenet picture freiksenet  路  4Comments

flippidippi picture flippidippi  路  3Comments

radiegtya picture radiegtya  路  5Comments

confuser picture confuser  路  4Comments