-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom scalar types in GraphQL schema language? #497
Comments
@ggregoire That won't work, because the |
Maybe not the right place to continue the discussion, but I got this error
with the following simple code (almost a copy/paste of makeExecutableSchema({
typeDefs: [`
scalar Date
type User {
id: Int
firstName: String
lastName: String
sex: String
birthDate: Date
}
type Query {
users(id: Int): [User]
}
`],
resolvers: {
Query: {
async users({ id }) {
const users = db('users');
return await id ? users.where('id', id) : users;
}
},
Date: {
__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;
}
},
}
}); |
@ggregoire yeah, you should open an issue on graphql-tools. But for what it's worth, I copy-pasted your exact code and it created a schema with the Date type just fine, so I'm not sure what's going on. |
My bad, I let this in my code 🙄 const officialWay = buildSchema(`
type User {
id: Int
firstName: String
lastName: String
sex: String
birthDate: Date
}
type Query {
users(id: Int): [User]
}
`); @helfer Thank you for the help! |
@helfer I'm having the same exact issue @ggregoire was in the first post, but I can't make sense out of what transpired on this thread. module.exports = buildSchema(`
# A JavaScript Date object.
scalar DateTime
# A geoframe object, which contains several polygons.
type Geoframe {
id: ID!
# Device ID count.
count: Int
createdAt: DateTime!
createdBy: User!
polygons: [Polygon]
}
type Query {
geoframes: [Geoframe]
}
`); The resolvers: module.exports = {
DateTime: new GraphQLScalarType({
name: 'DateTime',
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;
},
}),
geoframes() {
return new Promise((resolve, reject) => {
Geoframe.find({}, (err, docs) => {
const newDocs = docs.map(doc =>
Object.assign({}, doc.toObject(), {
createdAt: doc._id.getTimestamp(),
})
);
resolve(newDocs);
});
});
},
}; |
@ffxsam What approach did you land on? I agree that it is bizarre that there is no obvious solution beyond going outside the library. Edit: here's a solution: https://stackoverflow.com/questions/47824603/graphql-custom-scalar-definition-without-graphql-tools |
@ffxsam Hi. parseValue(value) {
return new Date(value); // value from the client
}, What does |
@mrdulin The GraphQL client side. So, for example, if Apollo sends a mutation to the GraphQL server to update a date value. |
The solution in the post worked for me, although with v15.3.0 ( not latest ), I had to do: I feel like this should be better documented. Every example I could find on google, SO, etc was with Apollo. The scalar section of the docs is very small, and doesn't cover this at all. |
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 theDate
type?The value of
birthDate
is1986-02-11 07:13:14
. I expect the same after the serialization.My query in graphiQL:
The result:
The text was updated successfully, but these errors were encountered: