Created
March 5, 2020 18:35
-
-
Save bogoslavskiy/5393a06e59a6b8b28b5fe51a906ae5d5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { gql } from 'apollo-server-express'; | |
import { DeviceMutationResolvers } from '../generated/graphql'; | |
import DeviceTokenModel from '../models/DeviceTokenModel'; | |
const typeDefs = gql` | |
enum DevicePlatform { | |
IOS | |
ANDROID | |
} | |
input DeviceRegisterInput { | |
user_id: NEString! | |
token: NEString! | |
devicePlatform: DevicePlatform! | |
deviceYear: String | |
systemVersion: String | |
deviceName: String | |
} | |
type DeviceMutation { | |
register(input: DeviceRegisterInput!): Boolean! | |
unregister(token: NEString!): Boolean! | |
} | |
extend type Mutation { | |
device: DeviceMutation! | |
} | |
`; | |
const DeviceMutation: DeviceMutationResolvers = { | |
async register(_, { input }) { | |
const exisingToken = await DeviceTokenModel.findOne({ | |
token: input.token, | |
user_id: input.user_id | |
}); | |
if (!exisingToken) { | |
const deviceToken = new DeviceTokenModel(input); | |
await deviceToken.save(); | |
} | |
return true; | |
}, | |
async unregister(_, { token }) { | |
const deviceToken = await DeviceTokenModel.findOne({ token }); | |
if (deviceToken) { | |
await deviceToken.remove(); | |
return true; | |
} | |
return false; | |
} | |
}; | |
const resolvers = { | |
Mutation: { | |
device: () => ({}), | |
}, | |
DeviceMutation | |
}; | |
export default { typeDefs, resolvers }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment