Skip to content

Instantly share code, notes, and snippets.

View LawJolla's full-sized avatar

Dennis Walsh LawJolla

View GitHub Profile
@LawJolla
LawJolla / scratch.graphql
Created February 26, 2018 23:40
example mutation
mutation {
updateAskingPrice(id: "1", newPrice: 10000) {
id
year
make
model
askingPrice
}
}
@LawJolla
LawJolla / scratch.graphql
Created February 26, 2018 23:38
Updated schema
type Vehicle {
id: ID!
year: Int!
make: String!
model: Int!
askingPrice: Float @isAuthenticated
costBasis: Float @hasRole(role: “MANAGER”)
numberOfOffers: Int @hasRole(role: “MANAGER”)
}
@LawJolla
LawJolla / scratch.graphql
Last active March 1, 2018 14:52
Updated schema
type Vehicle {
id: ID!
year: Int!
make: String!
model: Int!
askingPrice: Float @isAuthenticated
costBasis: Float @hasRole(role: “MANAGER”)
numberOfOffers: Int @hasRole(role: “MANAGER”)
}
@LawJolla
LawJolla / scratch_33.es6
Created February 26, 2018 23:35
makeExecutableSchema
export const schema = makeExecutableSchema({
typeDefs,
resolvers,
directiveResolvers
});
@LawJolla
LawJolla / scratch_33.es6
Created February 26, 2018 23:33
Clutter free resolver
const Query = {
vehicles: (parent, args, context, info) =>
context.db.query.vehicles({ where: { dealership: args.id } })
}
@LawJolla
LawJolla / scratch_33.es6
Created February 26, 2018 23:32
hasRole directive resolver
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}`)
},
...
}
@LawJolla
LawJolla / scratch.graphql
Last active March 1, 2018 14:57
Directed Schema
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")
}
...
@LawJolla
LawJolla / scratch_33.es6
Last active March 1, 2018 15:39
Wrapped Query
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` }
@LawJolla
LawJolla / scratch_33.es6
Created February 26, 2018 23:26
Query Schema with permissions
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:
@LawJolla
LawJolla / scratch_33.es6
Created February 26, 2018 23:24
updateAskingPrice with permissions
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 },