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
mutation { | |
updateAskingPrice(id: "1", newPrice: 10000) { | |
id | |
year | |
make | |
model | |
askingPrice | |
} | |
} |
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
type Vehicle { | |
id: ID! | |
year: Int! | |
make: String! | |
model: Int! | |
askingPrice: Float @isAuthenticated | |
costBasis: Float @hasRole(role: “MANAGER”) | |
numberOfOffers: Int @hasRole(role: “MANAGER”) | |
} |
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
type Vehicle { | |
id: ID! | |
year: Int! | |
make: String! | |
model: Int! | |
askingPrice: Float @isAuthenticated | |
costBasis: Float @hasRole(role: “MANAGER”) | |
numberOfOffers: Int @hasRole(role: “MANAGER”) | |
} |
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
export const schema = makeExecutableSchema({ | |
typeDefs, | |
resolvers, | |
directiveResolvers | |
}); |
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 Query = { | |
vehicles: (parent, args, context, info) => | |
context.db.query.vehicles({ where: { dealership: args.id } }) | |
} |
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 directiveResolvers = { | |
..., | |
hasRole: (next, source, {role}, ctx) => { | |
const user = getUser() | |
if (role === user.role) return next(); | |
throw new Error(`Must have role: ${role}, you have role: ${user.role}`) | |
}, | |
... | |
} |
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
directive @isAuthenticated on FIELD | FIELD_DEFINITION | |
directive @hasRole(role: String) on FIELD | FIELD_DEFINITION | |
... | |
type Mutation { | |
updateAskingPrice(id: ID!, newPrice: Float!): Vehicle! @hasRole(role: "MANAGER") | |
} | |
... |
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 Query = { | |
vehicles: async (parent, args, context, info) => { | |
const vehicles = await context.db.query.vehicles({ | |
where: { dealership: args.id } | |
}) | |
const user = getUser(context) | |
return protectFieldsByRole( | |
[ | |
{ field: `costBasis`, role: `MANAGER` }, | |
{ field: `numberOfOffers`, role: `MANAGER` } |
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 Query = { | |
vehicles: async (parent, args, context, info) => { | |
const vehicles = await context.db.query.vehicles({ | |
where: { dealership: args.id } | |
}) | |
const user = getUser(context) | |
return vehicles.map(vehicle => ({ | |
...vehicle, | |
costBasis: |
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 Mutation = { | |
updateVehicleAskingPrice: async (parent, { id, askingPrice }, context, info) => { | |
const userId = getUserId(context) | |
const isRequestingUserManager = await context.db.exists.User({ | |
id: userId, | |
role: `MANAGER` | |
}) | |
if (isRequestingUserManager) { | |
return await context.db.mutation.updateVehicle({ | |
where: { id }, |