Skip to content

Instantly share code, notes, and snippets.

View maticzav's full-sized avatar
🎉
Keep moving forward!

Matic Zavadlal maticzav

🎉
Keep moving forward!
View GitHub Profile
// Standalone
import { applyMiddleware } from 'graphql-middleware'
const schemaWithMiddleware = applyMiddleware(
schema,
metricsMiddleware,
authMiddleware,
beepMiddleware,
)
const loggingMiddleware = async (resolve, parent, args, context, info) => {
try {
resolve()
} catch(err) {
report(err)
}
}
const loggingMiddleware = {
Query: async (resolve, parent, args, context, info) => {
try {
resolve()
} catch(err) {
report(err)
}
},
}
const loggingMiddleware = {
Query: {
foo: async (resolve, parent, args, context, info) => {
try {
resolve()
} catch(err) {
report(err)
}
},
},
// src/permissions/index.ts
import { shield, and } from 'graphql-shield'
import * as rules from './rules'
export const permissions = shield({
Query: {
viewer: rules.isGrocer,
},
Mutation: {
// src/permissions/rules.ts
import { rule, and, or, not } from 'graphql-shield'
import { Context, getUserEmail } from '../utils'
export const isGrocer = rule()(async (parent, args, ctx: Context, info) => {
const email = getUserEmail(ctx)
// Is there a Grocer with such email in our database (Prisma)?
return ctx.db.exists.Grocer({ email })
})
# src/schema.graphql
type Query {
viewer: Viewer
products: [Product!]!
}
type Mutation {
addItemToBasket(productId: ID!): Viewer
removeItemFromBasket(itemId: ID!): Viewer
# database/datamodel.graphql
type Grocer {
id: ID! @unique
createdAt: DateTime!
updatedAt: DateTime!
email: String! @unique
}
import fetch from 'isomorphic-fetch'
import { ApolloClient } from 'apollo-client'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { from, ApolloLink } from 'apollo-link'
import { createUploadLink } from 'apollo-upload-client'
let apolloClient = null
// Apollo server polyfill
if (!process.browser) {
040ee84504ae89bd76c1536376520cc8832e49649dc751df18f8e8020f0b8e534da34d848bc67ca30977edb902cb6e48c3d4a54553cb63910fd4b9c7735208766f