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 authLink = new ApolloLink((operation, forward) => { | |
AsyncStorage.getItem('token').then(token => { | |
operation.setContext(({ headers = {} }) => ({ | |
headers: { | |
...headers, | |
Authorization: token ? `Bearer ${token}` : null | |
} | |
})) | |
return forward(operation) |
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/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: { |
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/types.graphql | |
type User { | |
id: ID! @unique | |
createdAt: DateTime! | |
updatedAt: DateTime! | |
githubUserId: 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
# 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: |
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/index.ts | |
import { Prisma } from './generated/prisma' | |
new Prisma({ | |
endpoint: process.env.PRISMA_ENDPOINT, | |
secret: process.env.PRISMA_SECRET | |
}) |
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 | |
# import User, Note from './generated/database.graphql' | |
type Query { | |
me: User, | |
note(id: ID!): Note | |
} | |
type 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/resolvers/index.ts | |
import { me } from './Query/me' | |
import { auth } from './Mutation/auth' | |
export const resolvers = { | |
Query: { | |
me, | |
}, | |
Mutation: { | |
...auth, |
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/resolvers/Mutation/auth.ts | |
import * as jwt from 'jsonwebtoken' | |
export const auth = { | |
authenticate: async (parent, { githubCode }, ctx: Context, info) => { | |
// ... | |
return { | |
token: jwt.sign({ userId: user.id }, process.env.JWT_SECRET), // <- Sign in | |
user |
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/resolvers/index.ts | |
import { note } from './Query/note' | |
import { notes } from './Mutation/notes' | |
export const resolvers = { | |
Query: { | |
note, | |
}, | |
Mutation: { | |
...notes |
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
# To Authenticate | |
mutation Authenticate($githubCode: String!) { | |
authenticate(githubCode: $githubCode) { | |
token | |
user { | |
name | |
} | |
} | |
} |