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
// Standalone | |
import { applyMiddleware } from 'graphql-middleware' | |
const schemaWithMiddleware = applyMiddleware( | |
schema, | |
metricsMiddleware, | |
authMiddleware, | |
beepMiddleware, | |
) |
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
const loggingMiddleware = async (resolve, parent, args, context, info) => { | |
try { | |
resolve() | |
} catch(err) { | |
report(err) | |
} | |
} |
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
const loggingMiddleware = { | |
Query: async (resolve, parent, args, context, info) => { | |
try { | |
resolve() | |
} catch(err) { | |
report(err) | |
} | |
}, | |
} |
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
const loggingMiddleware = { | |
Query: { | |
foo: async (resolve, parent, args, context, info) => { | |
try { | |
resolve() | |
} catch(err) { | |
report(err) | |
} | |
}, | |
}, |
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
// src/permissions/index.ts | |
import { shield, and } from 'graphql-shield' | |
import * as rules from './rules' | |
export const permissions = shield({ | |
Query: { | |
viewer: rules.isGrocer, | |
}, | |
Mutation: { |
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
// 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 }) | |
}) |
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
# src/schema.graphql | |
type Query { | |
viewer: Viewer | |
products: [Product!]! | |
} | |
type Mutation { | |
addItemToBasket(productId: ID!): Viewer | |
removeItemFromBasket(itemId: ID!): Viewer |
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
# database/datamodel.graphql | |
type Grocer { | |
id: ID! @unique | |
createdAt: DateTime! | |
updatedAt: DateTime! | |
email: String! @unique | |
} |
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 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) { |
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
040ee84504ae89bd76c1536376520cc8832e49649dc751df18f8e8020f0b8e534da34d848bc67ca30977edb902cb6e48c3d4a54553cb63910fd4b9c7735208766f |