-
-
Save AugustoCalaca/eb96d9f5ff43a0a2117af842bdef3c03 to your computer and use it in GitHub Desktop.
timestamps and monooseIDResolver to be spread in any GraphQL Object Type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { GraphQLString, GraphQLNonNull } from 'graphql'; | |
export const mongooseIDResolver = { | |
_id: { | |
type: GraphQLNonNull(GraphQLString), | |
description: 'Mongo _id', | |
resolve: obj => obj._id.toString(), | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
}, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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