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
import fetch from 'isomorphic-fetch'
import { ApolloClient } from 'apollo-client'
import { HttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { from, ApolloLink } from 'apollo-link'
// WORKING -------------------------------------------------------------------
const httpLink = new HttpLink({
uri: process.env.GRAPHCOOL_ENDPOINT,
import { GraphQLServer } from 'graphql-yoga'
import { importSchema } from 'graphql-import'
import { Prisma } from './generated/prisma'
import { Context } from './utils'
const resolvers = {
Query: {
async allBooks(parent, args, ctx, info) {
return await ctx.db.query.books({}, info)
}
type Book {
id: ID! @unique
title: String!
page: [Int!]!
}
mutation {
first: createBook(data: {
title: "First book",
page: {
set: [1,2,3,4]
}
}) {
id
}
}
040ee84504ae89bd76c1536376520cc8832e49649dc751df18f8e8020f0b8e534da34d848bc67ca30977edb902cb6e48c3d4a54553cb63910fd4b9c7735208766f
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) {
# database/datamodel.graphql
type Grocer {
id: ID! @unique
createdAt: DateTime!
updatedAt: DateTime!
email: String! @unique
}
# src/schema.graphql
type Query {
viewer: Viewer
products: [Product!]!
}
type Mutation {
addItemToBasket(productId: ID!): Viewer
removeItemFromBasket(itemId: ID!): Viewer
// 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/permissions/index.ts
import { shield, and } from 'graphql-shield'
import * as rules from './rules'
export const permissions = shield({
Query: {
viewer: rules.isGrocer,
},
Mutation: {