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

Commit 8c6f4fd

Browse files
authored
feat(graphcool): generate exists (#20)
Closes #11
1 parent b0455d3 commit 8c6f4fd

File tree

7 files changed

+92
-203
lines changed

7 files changed

+92
-203
lines changed

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"prepublish": "npm run build",
99
"build": "rm -rf dist && tsc -d",
1010
"test": "nyc cucumber-js --compiler ts:ts-node/register test/features/ -r test/step_definitions/ -f json:test/report/cucumber_report.json",
11-
"posttest": "node test/createReport.js && nyc report --reporter=text-lcov | coveralls && nyc report --reporter=text-lcov | codecov && nyc report --reporter=text-lcov | codacy-coverage --language typescript",
11+
"dposttest": "node test/createReport.js && nyc report --reporter=text-lcov | coveralls",
1212
"semantic-release": "semantic-release"
1313
},
1414
"keywords": [
@@ -44,9 +44,8 @@
4444
},
4545
"devDependencies": {
4646
"@types/cucumber": "3.1.1",
47+
"@types/graphql": "0.11.7",
4748
"@types/node": "8.5.2",
48-
"codacy-coverage": "2.0.3",
49-
"codecov": "3.0.0",
5049
"coveralls": "3.0.0",
5150
"cucumber": "3.2.0",
5251
"nyc": "11.4.1",

src/generators/graphcool-js.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
GraphQLOutputType,
1010
GraphQLScalarType,
1111
GraphQLNamedType,
12-
12+
isWrappingType,
1313
isNonNullType,
1414
isListType,
1515
GraphQLFieldMap,
@@ -41,6 +41,10 @@ function renderMainMethod(queryType: GraphQLObjectType, mutationType?: GraphQLOb
4141
super({ typeDefs, endpoint, secret, fragmentReplacements, debug });
4242
4343
var self = this
44+
this.exists = {
45+
${renderExistsFields(queryType.getFields())}
46+
}
47+
4448
this.query = {
4549
${renderMainMethodFields('query', queryType.getFields())}
4650
}${mutationType ? `
@@ -56,6 +60,29 @@ ${renderMainMethodFields('mutation', mutationType.getFields())}
5660
}`
5761
}
5862

63+
export function renderExistsFields(fields: GraphQLFieldMap<any, any>) : string {
64+
return Object.keys(fields)
65+
.map(f => {
66+
const field = fields[f]
67+
let type = field.type
68+
let foundList = false
69+
// Traverse the wrapping types (if any)
70+
while (isWrappingType(type)) {
71+
type = type.ofType
72+
// One of those wrappings need to be a GraphQLList for this field to qualify
73+
foundList = foundList || isListType(type)
74+
}
75+
if (foundList) {
76+
const whereType = (field.args.find(a => a.name === 'where')!.type as GraphQLInputObjectType).name
77+
return ` ${type.name}(where) {
78+
return super.existsDelegate('query', '${field.name}', { where }, {}, '{ id }')
79+
}`
80+
}
81+
})
82+
.filter(f => f)
83+
.join(',\n')
84+
}
85+
5986
function renderMainMethodFields(operation: string, fields: GraphQLFieldMap<any, any>): string {
6087
return Object.keys(fields).map(f => {
6188
const field = fields[f]

src/generators/graphcool-ts.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
GraphQLNamedType,
1212
isNonNullType,
1313
isListType,
14+
isWrappingType,
1415
GraphQLFieldMap,
1516
GraphQLEnumType,
1617
GraphQLType,
@@ -58,6 +59,10 @@ function renderMainMethod(
5859
constructor({ endpoint, secret, fragmentReplacements, debug }: BaseGraphcoolOptions) {
5960
super({ typeDefs, endpoint, secret, fragmentReplacements, debug });
6061
}
62+
63+
exists = {
64+
${renderExistsFields(queryType.getFields())}
65+
}
6166
6267
query: Query = {
6368
${renderMainMethodFields('query', queryType.getFields())}
@@ -73,6 +78,27 @@ ${renderMainMethodFields('mutation', mutationType.getFields())}
7378
}`
7479
}
7580

81+
export function renderExistsFields(fields: GraphQLFieldMap<any, any>) : string {
82+
return Object.keys(fields)
83+
.map(f => {
84+
const field = fields[f]
85+
let type = field.type
86+
let foundList = false
87+
// Traverse the wrapping types (if any)
88+
while (isWrappingType(type)) {
89+
type = type.ofType
90+
// One of those wrappings need to be a GraphQLList for this field to qualify
91+
foundList = foundList || isListType(type)
92+
}
93+
if (foundList) {
94+
const whereType = (field.args.find(a => a.name === 'where')!.type as GraphQLInputObjectType).name
95+
return ` ${type.name}: (where: ${whereType}): Promise<boolean> => super.existsDelegate('query', '${field.name}', { where }, {}, '{ id }')`
96+
}
97+
})
98+
.filter(f => f)
99+
.join(',\n')
100+
}
101+
76102
export function renderMainMethodFields(operation: string, fields: GraphQLFieldMap<any, any>): string {
77103
return Object.keys(fields)
78104
.map(f => {

src/graphql.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Missing declarations in @types/graphql
2+
import { GraphQLList, GraphQLNonNull, GraphQLType } from "graphql";
3+
4+
declare module 'graphql' {
5+
export type GraphQLWrappingType = GraphQLList<any> | GraphQLNonNull<any>;
6+
7+
export function isWrappingType(type: GraphQLType): type is GraphQLWrappingType
8+
export function isListType(type: GraphQLType): type is GraphQLList<any>
9+
export function isNonNullType(type: GraphQLType): type is GraphQLNonNull<any>
10+
}

test/features/graphcool-ts.feature

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ Feature for Graphcool Typescript generator
4545
super({ typeDefs, endpoint, secret, fragmentReplacements, debug });
4646
}
4747
48+
exists = {
49+
50+
}
51+
4852
query: Query = {
4953
posts: (args, info): Promise<String[] | null> => super.delegate('query', 'posts', args, {}, info)
5054
}

test/test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import * as fs from 'fs'
33

44
import { generators } from '../src/generators'
55

6+
// const schema = `
7+
// type Query {
8+
// posts: [String]
9+
// }`
10+
611
const schema = `
712
# THIS FILE HAS BEEN AUTO-GENERATED BY THE "GRAPHCOOL DEPLOY" COMMAND AT 2017-12-19T20:44:05.845Z
813
# DO NOT EDIT THIS FILE DIRECTLY
@@ -129,7 +134,7 @@ input PostWhereUniqueInput {
129134
}
130135
131136
type Query {
132-
posts(where: PostWhereInput, orderBy: PostOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): [Post]!
137+
posts(where: PostWhereInput, orderBy: PostOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): [[Post!]]!
133138
post(where: PostWhereUniqueInput!): Post
134139
postsConnection(where: PostWhereInput, orderBy: PostOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): PostConnection!
135140
node(id: ID!): Node

0 commit comments

Comments
 (0)