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
# prisma.yml
# The name of our project
service: graphql-server-github-auth-example
# Development stage on local cluster
stage: dev
cluster: local
# Our datamodel
datamodel:
# database/datamodel/types.graphql
type User {
id: ID! @unique
createdAt: DateTime!
updatedAt: DateTime!
githubUserId: String! @unique
// src/github.ts
import * as fetch from 'isomorphic-fetch'
export async function getGithubToken(githubCode: string): Promise<string> {
const endpoint = 'https://github.com/login/oauth/access_token'
const data = await fetch(endpoint, {
method: 'POST',
headers: {
const authLink = new ApolloLink((operation, forward) => {
AsyncStorage.getItem('token').then(token => {
operation.setContext(({ headers = {} }) => ({
headers: {
...headers,
Authorization: token ? `Bearer ${token}` : null
}
}))
return forward(operation)
const authLink = new ApolloLink(async (operation, forward) => {
const token = await AsyncStorage.getItem('token')
operation.setContext(({ headers = {} }) => ({
headers: {
...headers,
Authorization: token ? `Bearer ${token}` : null
}
}))
# database/schema.graphql
type Mutation {
...
# takes two arguments(data and where) and returns a File
updateFile(data: FileUpdateInput!, where: FileWhereUniqueInput!): File
...
}
# Tells us which properties can be given to data
// src/resolvers/Mutation/file.ts
export const file = {
renameFile: async (parent, {id, name}, ctx: Context, info) => {
return ctx.db.mutation.updateFile({ data: { name }, where: { id } }, info)
},
deleteFile: async (parent, { id }, ctx: Context, info) => {
return await ctx.db.mutation.deleteFile({ where: { id } }, info)
}
}
// src/files.ts
const data = {
name,
size,
secret,
contentType,
url
}
// src/index.ts
import { Prisma } from "./generated/prisma"
const DATABASE_SCHEMA_PATH = './database/schema.graphql'
new Prisma({
endpoint: process.env.PRISMA_ENDPOINT,
secret: process.env.PRISMA_SECRET,
})
# src/schema.graphql
# import ID, File from "./generated/database.graphql"
# Everything user can read
type Query {
file(id: ID!): File # Takes id property as an argument and returns a File
files: [File!]! # Returns every file stored
}