Skip to content

Commit

Permalink
fix: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
schickling committed Jan 14, 2018
1 parent fe587e6 commit 2a2fee9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 deletions.
36 changes: 14 additions & 22 deletions src/Graphcool.ts → src/Prisma.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Binding } from 'graphql-binding'
import { Exists, GraphcoolOptions, QueryMap, SubscriptionMap } from './types'
import { Exists, PrismaOptions, QueryMap, SubscriptionMap } from './types'
import { sign } from 'jsonwebtoken'
import { makeGraphcoolLink } from './link'
import { makePrismaLink } from './link'
import { GraphQLResolveInfo, isListType, isWrappingType } from 'graphql'
import { buildExistsInfo } from './info'
import { importSchema } from 'graphql-import'
Expand All @@ -15,7 +15,7 @@ const typeDefsCache: { [schemaPath: string]: string } = {}
const sharedLink = new SharedLink()
let remoteSchema: GraphQLSchema | undefined

export class Graphcool extends Binding<QueryMap, SubscriptionMap> {
export class Prisma extends Binding<QueryMap, SubscriptionMap> {
exists: Exists

constructor({
Expand All @@ -24,45 +24,37 @@ export class Graphcool extends Binding<QueryMap, SubscriptionMap> {
secret,
fragmentReplacements,
debug,
}: GraphcoolOptions) {
}: PrismaOptions) {
if (!typeDefs) {
throw new Error('No `typeDefs` provided when calling `new Graphcool()`')
throw new Error('No `typeDefs` provided when calling `new Prisma()`')
}

if (typeDefs.endsWith('.graphql')) {
typeDefs = getCachedTypeDefs(typeDefs)
}

if (endpoint === undefined) {
if (process.env.GRAPHCOOL_ENDPOINT) {
endpoint = process.env.GRAPHCOOL_ENDPOINT
} else {
throw new Error(
`No Graphcool endpoint found. Either provide \`endpoint\` constructor option or set \`GRAPHCOOL_ENDPOINT\` env var.`,
)
}
throw new Error(
`No Prisma endpoint found. Please provide the \`endpoint\` constructor option.`,
)
}

if (!endpoint!.startsWith('http')) {
throw new Error(`Invalid Graphcool endpoint provided: ${endpoint}`)
throw new Error(`Invalid Prisma endpoint provided: ${endpoint}`)
}

if (secret === undefined) {
if (process.env.GRAPHCOOL_SECRET) {
secret = process.env.GRAPHCOOL_SECRET
} else {
throw new Error(
`No Graphcool secret found. Either provide \`secret\` constructor option or set \`GRAPHCOOL_SECRET\` env var.`,
)
}
throw new Error(
`No Prisma secret found. Please provide the \`secret\` constructor option.`,
)
}

fragmentReplacements = fragmentReplacements || {}

debug = debug || false

const token = sign({}, secret!)
const link = makeGraphcoolLink({ endpoint: endpoint!, token, debug })
const link = makePrismaLink({ endpoint: endpoint!, token, debug })

if (!remoteSchema) {
remoteSchema = makeRemoteExecutableSchema({
Expand Down Expand Up @@ -101,7 +93,7 @@ export class Graphcool extends Binding<QueryMap, SubscriptionMap> {
}
}

class ExistsHandler implements ProxyHandler<Graphcool> {
class ExistsHandler implements ProxyHandler<Prisma> {
constructor(private schema: GraphQLSchema, private delegate: any) { }

get(target: any, typeName: string) {
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { Graphcool } from './Graphcool'
export { makeGraphcoolLink } from './link'
export { BaseGraphcoolOptions } from './types'
export { Prisma } from './Prisma'
export { makePrismaLink } from './link'
export { BasePrismaOptions } from './types'
export { extractFragmentReplacements, forwardTo } from 'graphql-binding'
2 changes: 1 addition & 1 deletion src/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { WebSocketLink } from 'apollo-link-ws'
import { onError } from 'apollo-link-error'
import * as ws from 'ws'

export function makeGraphcoolLink({
export function makePrismaLink({
endpoint,
token,
debug,
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ export interface Exists {
[rootField: string]: (filter: { [key: string]: any }) => Promise<boolean>
}

export interface BaseGraphcoolOptions {
export interface BasePrismaOptions {
fragmentReplacements?: FragmentReplacements
endpoint?: string
secret?: string
debug?: boolean
}

export interface GraphcoolOptions extends BaseGraphcoolOptions {
export interface PrismaOptions extends BasePrismaOptions {
typeDefs: string
}

Expand All @@ -28,4 +28,4 @@ export interface SubscriptionMap {
args?: any,
info?: GraphQLResolveInfo | string,
) => AsyncIterator<any> | Promise<AsyncIterator<any>>
}
}

0 comments on commit 2a2fee9

Please sign in to comment.