Skip to content

Instantly share code, notes, and snippets.

@AugustoCalaca
Forked from sibelius/_usage.tsx
Last active April 4, 2020 21:48
Show Gist options
  • Save AugustoCalaca/eb96d9f5ff43a0a2117af842bdef3c03 to your computer and use it in GitHub Desktop.
Save AugustoCalaca/eb96d9f5ff43a0a2117af842bdef3c03 to your computer and use it in GitHub Desktop.
timestamps and monooseIDResolver to be spread in any GraphQL Object Type
import { GraphQLString, GraphQLNonNull } from 'graphql';
export const mongooseIDResolver = {
_id: {
type: GraphQLNonNull(GraphQLString),
description: 'Mongo _id',
resolve: obj => obj._id.toString(),
}
};
import { GraphQLString } from 'graphql';
export const timestamps = {
createdAt: {
type: GraphQLString,
resolve: obj => obj.createdAt ? obj.createdAt.toISOString() : null,
},
updatedAt: {
type: GraphQLString,
resolve: obj => obj.updatedAt ? obj.updatedAt.toISOString() : null,
},
};
const UserType = new GraphQLObjectType<IUser, GraphQLContext>({
name: 'User',
description: 'User data',
fields: () => ({
id: globalIdField('User'),
...mongooseIDResolver,
name: {
type: GraphQLString,
resolve: user => user.name,
},
email: {
type: GraphQLString,
resolve: user => user.email,
},
...timestamps,
}),
interfaces: () => [nodeInterface],
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment