-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a927433
commit bddad62
Showing
36 changed files
with
7,599 additions
and
137 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
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,34 @@ | ||
# graphql-codegen-binding | ||
|
||
## Usage | ||
|
||
### CLI | ||
|
||
```sh | ||
$ npm install -g graphql-codegen-binding | ||
$ graphql-codegen-binding | ||
|
||
Usage: graphql-codegen-binding -s [schema] -e [endpoint] -h [headers] -g [generator] -t [target] | ||
|
||
Options: | ||
--help Show help [boolean] | ||
--version Show version number [boolean] | ||
--schema, -s Path to schema.graphql file [string] | ||
--endpoint, -e GraphQL endpoint to fetch schema from [string] | ||
--headers, -h Header to use for downloading the schema (with endpoint URL) | ||
[string] | ||
--generator, -g Type of the generator. Available generators: typescript, | ||
javascript [string] [required] | ||
--target, -t Target file. Example: schema.ts [string] [required] | ||
``` | ||
|
||
### Typescript | ||
|
||
```ts | ||
import { generateCode } from 'graphql-codegen-binding' | ||
import * as fs from 'fs' | ||
|
||
const code = generateCode(fs.readFileSync('schema.graphql'), 'typescript') | ||
|
||
fs.writeFileSync('MyBinding.ts', code) | ||
``` |
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,70 @@ | ||
{ | ||
"name": "graphql-codegen-prisma-binding", | ||
"version": "0.0.1", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"repository": { | ||
"url": "https://github.com/graphcool/graphql-codegen-binding.git" | ||
}, | ||
"bin": { | ||
"graphql-codegen-prisma-binding": "./dist/bin.js" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"contributors": [ | ||
{ | ||
"name": "Tim Suchanek", | ||
"email": "suchanek@prisma.io" | ||
} | ||
], | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@types/graphql": "^0.13.0", | ||
"@types/jest": "^22.2.2", | ||
"@types/node": "^9.6.2", | ||
"jest": "^22.4.3", | ||
"prettier": "^1.10.2", | ||
"ts-jest": "^22.4.2", | ||
"tslint": "^5.6.0", | ||
"typescript": "^2.6.2" | ||
}, | ||
"scripts": { | ||
"test": "jest", | ||
"build": "tsc -d && chmod +x dist/bin.js", | ||
"lint": "tslint src/**/*.ts", | ||
"precommit": "lint-staged", | ||
"prepublishOnly": "yarn lint && yarn test && yarn build" | ||
}, | ||
"peerDependencies": { | ||
"graphql": "^0.11.0 || ^0.12.0 || ^0.13.0" | ||
}, | ||
"jest": { | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"jsx", | ||
"json" | ||
], | ||
"rootDir": "./src", | ||
"transform": { | ||
"^.+\\.(ts|tsx)$": "../node_modules/ts-jest/preprocessor.js" | ||
}, | ||
"testMatch": [ | ||
"**/*.test.(ts|js)" | ||
], | ||
"globals": { | ||
"ts-jest": { | ||
"tsConfigFile": "../tsconfig.json" | ||
} | ||
} | ||
}, | ||
"dependencies": { | ||
"graphql": "^0.11.0 || ^0.12.0 || ^0.13.0", | ||
"graphql-codegen-binding": "^0.0.15", | ||
"graphql-config": "2.0.1", | ||
"mkdirp": "^0.5.1", | ||
"yargs": "^11.0.0" | ||
} | ||
} |
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,64 @@ | ||
#!/usr/bin/env node | ||
|
||
import * as yargs from 'yargs' | ||
import { generateCode } from '.' | ||
|
||
const argv = yargs | ||
.usage( | ||
`Usage: $0 -s [schema] -e [endpoint] -h [headers] -g [generator] -t [target]`, | ||
) | ||
.options({ | ||
schema: { | ||
alias: 's', | ||
describe: 'Path to schema.graphql file', | ||
type: 'string', | ||
}, | ||
endpoint: { | ||
alias: 'e', | ||
describe: 'GraphQL endpoint to fetch schema from', | ||
type: 'string', | ||
}, | ||
headers: { | ||
alias: 'h', | ||
describe: 'Header to use for downloading the schema (with endpoint URL)', | ||
type: 'string', | ||
}, | ||
generator: { | ||
alias: 'g', | ||
describe: | ||
'Type of the generator. Available generators: typescript, javascript', | ||
type: 'string', | ||
}, | ||
target: { | ||
alias: 't', | ||
describe: 'Target file. Example: schema.ts', | ||
type: 'string', | ||
}, | ||
}) | ||
.demandOption(['g', 't']).argv | ||
|
||
run(argv) | ||
|
||
async function run(argv) { | ||
const endpointHeaders = {} | ||
const headers = argv.headers | ||
? Array.isArray(argv.headers) ? argv.headers : [argv.headers] | ||
: undefined | ||
if (headers) { | ||
Object.assign( | ||
endpointHeaders, | ||
...headers.map(h => ({ [h.split('=')[0]]: h.split('=')[1] })), | ||
) | ||
} | ||
|
||
const { generator, target, endpoint } = argv | ||
await generateCode({ | ||
schemaPath: argv.schema, | ||
generator, | ||
target, | ||
endpoint, | ||
headers: endpointHeaders, | ||
}) | ||
|
||
console.log('Done generating binding') | ||
} |
42 changes: 42 additions & 0 deletions
42
packages/graphql-codegen-prisma-binding/src/generateCode.ts
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,42 @@ | ||
import * as fs from 'fs' | ||
import { GraphQLEndpoint } from 'graphql-config' | ||
import { makeBinding } from './makeBinding' | ||
import { GeneratorType } from './types' | ||
import * as mkdirp from 'mkdirp' | ||
import * as path from 'path' | ||
|
||
export interface CodeGenerationInput { | ||
schemaPath?: string | ||
schema?: string | ||
endpoint?: string | ||
generator: GeneratorType | ||
target: string | ||
headers?: any | ||
} | ||
|
||
export async function generateCode(argv: CodeGenerationInput) { | ||
if (!argv.schema && !argv.schemaPath && !argv.endpoint) { | ||
throw new Error( | ||
'Please either provide the schema or the endpoint you want to get the schema from.', | ||
) | ||
} | ||
|
||
const schema = argv.schema | ||
? argv.schema | ||
: argv.schemaPath | ||
? fs.readFileSync(argv.schemaPath, 'utf-8') | ||
: await downloadFromEndpointUrl(argv) | ||
|
||
const code = makeBinding(schema, argv.generator) | ||
mkdirp(path.dirname(argv.target)) | ||
fs.writeFileSync(argv.target, code) | ||
} | ||
|
||
function downloadFromEndpointUrl(argv) { | ||
const endpoint = new GraphQLEndpoint({ | ||
url: argv.endpoint, | ||
headers: argv.headers, | ||
}) | ||
|
||
return endpoint.resolveSchemaSDL() | ||
} |
Oops, something went wrong.