This repository has been archived by the owner on May 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bindings): added prisma bindings (#36)
- Loading branch information
1 parent
ccb4116
commit b8f784a
Showing
4 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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, | ||
} |
This file contains 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
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') | ||
} |
This file contains 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
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())} | ||
}` | ||
: '' | ||
} | ||
}` | ||
} |
This file contains 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