Skip to content
This repository has been archived by the owner on May 10, 2018. It is now read-only.

Commit

Permalink
feat(bindings): added prisma bindings (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrandwijk authored Jan 15, 2018
1 parent ccb4116 commit b8f784a
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/generators/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { generator as graphcooljsgenerator } from './graphcool-js'
import { generator as graphcooltsgenerator } from './graphcool-ts'
import { generator as prismajsgenerator } from './prisma-js'
import { generator as prismatsgenerator } from './prisma-ts'
import { generator as bindingtsgenerator } from './binding-ts'
import { generator as bindingjsgenerator } from './binding-js'
import { Generator } from '../types';

export const generators: { [key: string]: Generator} = {
'graphcool-js': graphcooljsgenerator,
'graphcool-ts': graphcooltsgenerator,
'prisma-js': prismajsgenerator,
'prisma-ts': prismatsgenerator,
'binding-ts': bindingtsgenerator,
'binding-js': bindingjsgenerator,
}
71 changes: 71 additions & 0 deletions src/generators/prisma-js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { GraphQLFieldMap, GraphQLInputObjectType, GraphQLObjectType, isListType, isWrappingType } from 'graphql';

import { Generator } from '../types';
import { renderMainMethodFields, renderMainSubscriptionMethodFields } from './graphcool-js';

export const generator: Generator = {
Main: renderMainMethod,
Header: renderHeader,
}

function renderHeader(schema: string): string {
return `const { Prisma } = require('prisma-binding')
const { GraphQLResolveInfo } = require('graphql')
const typeDefs = \`
${schema}\``
}


function renderMainMethod(queryType: GraphQLObjectType, mutationType?: GraphQLObjectType | null, subscriptionType?: GraphQLObjectType | null) {
return `module.exports.Prisma = class Binding extends Prisma {
constructor({ endpoint, secret, fragmentReplacements, debug }) {
super({ typeDefs, endpoint, secret, fragmentReplacements, debug });
var self = this
this.exists = {
${renderExistsFields(queryType.getFields())}
}
this.query = {
${renderMainMethodFields('query', queryType.getFields())}
}${mutationType ? `
this.mutation = {
${renderMainMethodFields('mutation', mutationType.getFields())}
}`: ''}${subscriptionType ? `
this.subscription = {
${renderMainSubscriptionMethodFields('mutation', subscriptionType.getFields())}
}`: ''}
}
delegate(operation, field, args, context, info) {
return super.delegate(operation, field, args, context, info)
}
}`
}

export function renderExistsFields(fields: GraphQLFieldMap<any, any>) : string {
return Object.keys(fields)
.map(f => {
const field = fields[f]
let type = field.type
let foundList = false
// Traverse the wrapping types (if any)
while (isWrappingType(type)) {
type = type.ofType
// One of those wrappings need to be a GraphQLList for this field to qualify
foundList = foundList || isListType(type)
}
if (foundList) {
const whereType = (field.args.find(a => a.name === 'where')!.type as GraphQLInputObjectType).name
return ` ${type.name}(where) {
return super.existsDelegate('query', '${field.name}', { where }, {}, '{ id }')
}`
}
})
.filter(f => f)
.join(',\n')
}
68 changes: 68 additions & 0 deletions src/generators/prisma-ts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { GraphQLObjectType } from 'graphql';

import { Generator } from '../types';
import {
generator as gcgenerator,
renderExistsFields,
renderMainMethodFields,
renderMainSubscriptionMethodFields,
} from './graphcool-ts';

export const generator: Generator = {
...gcgenerator,
Main: renderMainMethod,
Header: renderHeader,
}

const scalarMapping = {
Int: 'number',
String: 'string',
ID: 'string | number',
Float: 'number',
Boolean: 'boolean'
}

function renderHeader(schema: string): string {
return `import { Prisma as BasePrisma, BasePrismaOptions } from 'prisma-binding'
import { GraphQLResolveInfo } from 'graphql'
const typeDefs = \`
${schema}\``
}

function renderMainMethod(
queryType: GraphQLObjectType,
mutationType?: GraphQLObjectType | null,
subscriptionType?: GraphQLObjectType | null
) {
return `export class Prisma extends BasePrisma {
constructor({ endpoint, secret, fragmentReplacements, debug }: BasePrismaOptions) {
super({ typeDefs, endpoint, secret, fragmentReplacements, debug });
}
exists = {
${renderExistsFields(queryType.getFields())}
}
query: Query = {
${renderMainMethodFields('query', queryType.getFields())}
}${
mutationType
? `
mutation: Mutation = {
${renderMainMethodFields('mutation', mutationType.getFields())}
}`
: ''
}${
subscriptionType
? `
subscription: Subscription = {
${renderMainSubscriptionMethodFields(subscriptionType.getFields())}
}`
: ''
}
}`
}
2 changes: 2 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6802,5 +6802,7 @@ input ViewsWhereUniqueInput {

fs.writeFileSync('./testGraphcoolBinding.ts', generateCode(schema, generators['graphcool-ts']))
fs.writeFileSync('./testGraphcoolBinding.js', generateCode(schema, generators['graphcool-js']))
fs.writeFileSync('./testPrismaBinding.ts', generateCode(schema, generators['prisma-ts']))
fs.writeFileSync('./testPrismaBinding.js', generateCode(schema, generators['prisma-js']))
fs.writeFileSync('./testBinding.ts', generateCode(schema, generators['binding-ts']))
fs.writeFileSync('./testBinding.js', generateCode(schema, generators['binding-js']))

0 comments on commit b8f784a

Please sign in to comment.