Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: dotansimha/graphql-code-generator
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.8.0
Choose a base ref
...
head repository: dotansimha/graphql-code-generator
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.8.1
Choose a head ref
  • 14 commits
  • 61 files changed
  • 6 contributors

Commits on Oct 3, 2019

  1. Update dependency react-scripts to v3.2.0 (#2672)

    Co-authored-by: Renovate Bot <bot@renovateapp.com>
    2 people authored and dotansimha committed Oct 3, 2019
    Copy the full SHA
    274f359 View commit details
  2. Update dependency aws-sdk to v2.541.0 (#2671)

    Co-authored-by: Renovate Bot <bot@renovateapp.com>
    2 people authored and dotansimha committed Oct 3, 2019
    Copy the full SHA
    81dfd3a View commit details
  3. Update dependency @types/codemirror to v0.0.78 (#2670)

    Co-authored-by: Renovate Bot <bot@renovateapp.com>
    2 people authored and dotansimha committed Oct 3, 2019
    Copy the full SHA
    cf0659b View commit details
  4. Use cosmiconfig (#2674)

    * Use cosmiconfig
    
    * Use namespace
    
    * Fixes
    
    * Fixes
    kamilkisiela authored and dotansimha committed Oct 3, 2019
    Copy the full SHA
    9337ef3 View commit details
  5. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    55b6cd5 View commit details
  6. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    acdc295 View commit details

Commits on Oct 4, 2019

  1. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    ad5aa72 View commit details
  2. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    52dea90 View commit details

Commits on Oct 5, 2019

  1. Update dependency apollo-server to v2.9.5 (#2687)

    Co-authored-by: Renovate Bot <bot@renovateapp.com>
    2 people authored and dotansimha committed Oct 5, 2019
    Copy the full SHA
    92a0e24 View commit details
  2. Update dependency @types/node to v10.14.20 (#2677)

    Co-authored-by: Renovate Bot <bot@renovateapp.com>
    2 people authored and dotansimha committed Oct 5, 2019
    Copy the full SHA
    560cfe8 View commit details
  3. Make document validations once (#2673)

    * Make document validations once
    
    * Remove unnecessary tests
    
    * Fix typings
    ardatan authored and dotansimha committed Oct 5, 2019

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    912023c View commit details
  4. New template: typescript-graphql-request (#2688)

    * initial commit
    
    * fix
    
    * added docs
    dotansimha authored Oct 5, 2019

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    14653a6 View commit details
  5. Make errors thrown more descriptive (#2686)

    * Make msg visible in all listr-renderer errors
    
    * Remove newline
    JureSotosek authored and dotansimha committed Oct 5, 2019
    Copy the full SHA
    99aa17f View commit details
  6. v1.8.1

    dotansimha committed Oct 5, 2019
    Copy the full SHA
    1662b10 View commit details
Showing with 1,085 additions and 497 deletions.
  1. +2 −1 docs/plugins/index.md
  2. +66 −0 docs/plugins/typescript-graphql-request.md
  3. +1 −1 lerna.json
  4. +2 −2 package.json
  5. +7 −4 packages/graphql-codegen-cli/package.json
  6. +99 −48 packages/graphql-codegen-cli/src/config.ts
  7. +4 −4 packages/graphql-codegen-cli/src/utils/listr-renderer.ts
  8. +4 −2 packages/graphql-codegen-cli/src/utils/watcher.ts
  9. +0 −21 packages/graphql-codegen-cli/src/yml.ts
  10. +15 −6 packages/graphql-codegen-cli/tests/cli-flags.spec.ts
  11. +8 −8 packages/graphql-codegen-cli/tests/init.spec.ts
  12. +46 −0 packages/graphql-codegen-cli/tests/utils.ts
  13. +2 −2 packages/graphql-codegen-core/package.json
  14. +10 −0 packages/graphql-codegen-core/src/codegen.ts
  15. +1 −11 packages/graphql-codegen-core/src/execute-plugin.ts
  16. +0 −120 packages/graphql-codegen-core/tests/execute-plugin.spec.ts
  17. +4 −4 packages/plugins/flow/flow/package.json
  18. +5 −5 packages/plugins/flow/operations/package.json
  19. +5 −5 packages/plugins/flow/resolvers/package.json
  20. +4 −4 packages/plugins/java/apollo-android/package.json
  21. +3 −3 packages/plugins/java/common/package.json
  22. +5 −5 packages/plugins/java/java/package.json
  23. +5 −5 packages/plugins/java/kotlin/package.json
  24. +5 −5 packages/plugins/java/resolvers/package.json
  25. +2 −2 packages/plugins/other/add/package.json
  26. +2 −2 packages/plugins/other/fragment-matcher/package.json
  27. +2 −2 packages/plugins/other/introspection/package.json
  28. +2 −2 packages/plugins/other/schema-ast/package.json
  29. +2 −2 packages/plugins/other/time/package.json
  30. +3 −3 packages/plugins/other/visitor-plugin-common/package.json
  31. +2 −1 packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts
  32. +4 −4 packages/plugins/typescript/apollo-angular/package.json
  33. +3 −3 packages/plugins/typescript/compatibility/package.json
  34. +4 −4 packages/plugins/typescript/document-nodes/package.json
  35. +3 −3 packages/plugins/typescript/graphql-files-modules/package.json
  36. +5 −0 packages/plugins/typescript/graphql-request/.gitignore
  37. +5 −0 packages/plugins/typescript/graphql-request/.npmignore
  38. +39 −0 packages/plugins/typescript/graphql-request/package.json
  39. +32 −0 packages/plugins/typescript/graphql-request/src/index.ts
  40. +46 −0 packages/plugins/typescript/graphql-request/src/visitor.ts
  41. +300 −0 packages/plugins/typescript/graphql-request/tests/__snapshots__/graphql-request.spec.ts.snap
  42. +81 −0 packages/plugins/typescript/graphql-request/tests/graphql-request.spec.ts
  43. +22 −0 packages/plugins/typescript/graphql-request/tsconfig.json
  44. +5 −5 packages/plugins/typescript/mongodb/package.json
  45. +5 −5 packages/plugins/typescript/operations/package.json
  46. +4 −4 packages/plugins/typescript/react-apollo/package.json
  47. +5 −5 packages/plugins/typescript/resolvers/package.json
  48. +5 −5 packages/plugins/typescript/stencil-apollo/package.json
  49. +5 −5 packages/plugins/typescript/type-graphql/package.json
  50. +4 −4 packages/plugins/typescript/typescript/package.json
  51. +4 −4 packages/plugins/typescript/urql/package.json
  52. +5 −5 packages/presets/import-types/package.json
  53. +5 −5 packages/presets/near-operation-file/package.json
  54. +1 −1 packages/utils/config-md-generator/package.json
  55. +2 −2 packages/utils/graphql-codegen-testing/package.json
  56. +1 −1 packages/utils/plugins-helpers/package.json
  57. +3 −0 website/i18n/en.json
  58. +30 −30 website/live-demo/package.json
  59. +2 −2 website/package.json
  60. +1 −0 website/sidebars.json
  61. +136 −120 yarn.lock
3 changes: 2 additions & 1 deletion docs/plugins/index.md
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ Below is a table that lists all available plugins which can be installed via NPM
| `typescript-apollo-angular` | Generate TypeScript types, and Apollo-Angular Services | [`@graphql-codegen/typescript-apollo-angular`](./typescript-apollo-angular.md) |
| `typescript-react-apollo` | Generate TypeScript types, and React-Apollo Components | [`@graphql-codegen/typescript-react-apollo`](./typescript-react-apollo.md) |
| `typescript-urql` | Generate TypeScript types, and Urql Components | [`@graphql-codegen/typescript-urql`](./typescript-urql.md) |
| `typescript-graphql-request` | Generates fully-typed ready-to-use SDK for graphql-request | [`@graphql-codegen/typescript-graphql-request`](./typescript-graphql-request.md) |
| `typescript-stencil-apollo` | Generate TypeScript types, and Stencil Apollo Components | [`@graphql-codegen/typescript-stencil-apollo`](./typescript-stencil-apollo.md) |
| `typescript-mongodb` | Generate Generate server-side TypeScript types, with MongoDB models | [`@graphql-codegen/typescript-mongodb`](./typescript-mongodb.md) |
| `typescript-graphql-files-modules` | Generate `declare module` for `.graphql` files | [`@graphql-codegen/typescript-graphql-files-modules`](./typescript-graphql-files-modules.md) |
@@ -30,7 +31,7 @@ Below is a table that lists all available plugins which can be installed via NPM
| `flow-resolvers` | Generate resolvers signature for Flow | [`@graphql-codegen/flow-resolvers`](./flow-resolvers.md) |
| `flow-operations` | Generate types for Flow type based on your GraphQL operations | [`@graphql-codegen/flow-operations`](./flow-operations.md) |
| `reason-client` | Generate ReasonML types based on your GraphQL schema for use in a client application | [`@graphql-codegen/reason-client`](./reason-client.md) |
| `kotlin` | generates Kotlin backend `classes` for Enums and Input types | [`@graphql-codegen/kotlin`](./kotlin.md) |
| `kotlin` | generates Kotlin backend `classes` for Enums and Input types | [`@graphql-codegen/kotlin`](./kotlin.md) |
| `java` | generates Java backend `classes` for Enums and Input types | [`@graphql-codegen/java`](./java.md) |
| `java-resolvers` | generates Java backend resolvers signature | [`@graphql-codegen/java-resolvers`](./java-resolvers.md) |
| `java-apollo-android` | generates Apollo Android parsers and mappers | [`@graphql-codegen/java-apollo-android`](./java-apollo-android.md) |
66 changes: 66 additions & 0 deletions docs/plugins/typescript-graphql-request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
id: typescript-graphql-request
title: TypeScript GraphQL-Request
---

This plugin generates [`graphql-request`](https://www.npmjs.com/package/graphql-request) ready-to-use SDK, which is fully-typed.

{@import: ../docs/plugins/client-note.md}

## Installation

[You can find a working example with this plugin here](https://github.com/dotansimha/graphql-codegen-graphql-request-example)

$ yarn add @graphql-codegen/typescript @graphql-codegen/typescript-operations @graphql-codegen/typescript-graphql-request

The, make sure you have `typescript` plugin and `typescript-operations` as well in your configuration:

```yml
schema: https://countries.trevorblades.com/
documents: 'src/graphql/**/*.graphql'
generates:
src/sdk.ts:
plugins:
- typescript
- typescript-operations
- typescript-graphql-request
```
## Usage
For the given input:
```graphql
query continents {
continents {
name
countries {
...CountryFields
}
}
}
fragment CountryFields on Country {
name
currency
}
```

It generates SDK you can import and wrap your `GraphQLClient` instance, and get fully-typed SDK based on your operations:

```ts
import { GraphQLClient } from 'graphql-request';
import { getSdk } from './sdk'; // THIS FILE IS THE GENERATED FILE

async function main() {
const client = new GraphQLClient('https://countries.trevorblades.com/');
const sdk = getSdk(client);
const { continents } = await sdk.continents(); // This is fully typed, based on the query

console.log(`GraphQL data:`, continents);
}
```

## Configuration

{@import: ../docs/generated-config/base-visitor.md}
{@import: ../docs/generated-config/client-side-base-visitor.md}
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.8.0",
"version": "1.8.1",
"npmClient": "yarn",
"useWorkspaces": true,
"concurrency": 2
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -42,10 +42,10 @@
"@types/glob": "7.1.1",
"@types/jest": "24.0.18",
"@types/mkdirp": "0.5.2",
"@types/node": "10.14.19",
"@types/node": "10.14.20",
"@types/request": "2.48.3",
"apollo-link": "1.2.13",
"apollo-server": "2.9.4",
"apollo-server": "2.9.5",
"graphql": "14.5.8",
"graphql-tag": "2.10.1",
"graphql-tools": "4.0.5",
11 changes: 7 additions & 4 deletions packages/graphql-codegen-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@graphql-codegen/cli",
"version": "1.8.0",
"version": "1.8.1",
"license": "MIT",
"bin": {
"gql-gen": "dist/commonjs/bin.js",
@@ -42,8 +42,8 @@
"homepage": "https://github.com/dotansimha/graphql-code-generator#readme",
"dependencies": {
"@babel/parser": "7.6.2",
"@graphql-codegen/core": "1.8.0",
"@graphql-codegen/plugin-helpers": "1.8.0",
"@graphql-codegen/core": "1.8.1",
"@graphql-codegen/plugin-helpers": "1.8.1",
"@types/debounce": "1.2.0",
"@types/is-glob": "4.0.1",
"@types/mkdirp": "0.5.2",
@@ -54,6 +54,7 @@
"chokidar": "3.2.1",
"commander": "3.0.2",
"common-tags": "1.8.0",
"cosmiconfig": "5.2.1",
"debounce": "1.2.0",
"detect-indent": "6.0.0",
"glob": "7.1.4",
@@ -66,7 +67,6 @@
"inquirer": "7.0.0",
"is-glob": "4.0.1",
"is-valid-path": "0.1.1",
"js-yaml": "3.13.1",
"json-to-pretty-yaml": "1.2.2",
"listr": "0.14.3",
"listr-update-renderer": "0.5.0",
@@ -81,13 +81,16 @@
},
"devDependencies": {
"@types/chokidar": "1.7.5",
"@types/cosmiconfig": "5.0.3",
"@types/detect-indent": "5.0.0",
"@types/inquirer": "6.5.0",
"@types/js-yaml": "3.12.1",
"@types/listr": "0.14.2",
"@types/log-symbols": "2.0.0",
"bdd-stdin": "0.2.0",
"graphql": "14.5.8",
"js-yaml": "3.13.1",
"make-dir": "3.0.0",
"ts-jest": "24.1.0"
},
"peerDependencies": {
147 changes: 99 additions & 48 deletions packages/graphql-codegen-cli/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { existsSync, readFileSync } from 'fs';
import { join, resolve } from 'path';
import * as cosmiconfig from 'cosmiconfig';
import { resolve } from 'path';
import { Types } from '@graphql-codegen/plugin-helpers';
import { DetailedError } from '@graphql-codegen/core';
import { parseConfigFile } from './yml';
import { Command } from 'commander';

export type YamlCliFlags = {
@@ -12,48 +11,114 @@ export type YamlCliFlags = {
overwrite: boolean;
};

function getCustomConfigPath(cliFlags: YamlCliFlags): string | null | never {
const configFile = cliFlags.config;
function generateSearchPlaces(moduleName: string) {
const extensions = ['json', 'yaml', 'yml', 'js', 'config.js'];
// gives codegen.json...
const regular = extensions.map(ext => `${moduleName}.${ext}`);
// gives .codegenrc.json... but no .codegenrc.config.js
const dot = extensions.filter(ext => ext !== 'config.js').map(ext => `.${moduleName}rc.${ext}`);

if (configFile) {
const configPath = resolve(process.cwd(), configFile);
return regular.concat(dot);
}

if (!existsSync(configPath)) {
throw new DetailedError(
`Config ${configPath} does not exist`,
`
Config ${configPath} does not exist.
function customLoader(ext: 'json' | 'yaml' | 'js') {
function loader(filepath: string, content: string) {
if (typeof process !== 'undefined' && 'env' in process) {
content = content.replace(/\$\{(.*)\}/g, (str, variable, index) => {
let varName = variable;
let defaultValue = '';

if (variable.includes(':')) {
const spl = variable.split(':');
varName = spl.shift();
defaultValue = spl.join(':');
}

return process.env[varName] || defaultValue;
});
}

$ graphql-codegen --config ${configPath}
if (ext === 'json') {
return (cosmiconfig as any).loadJson(filepath, content);
}

Please make sure the --config points to a correct file.
`
);
if (ext === 'yaml') {
return (cosmiconfig as any).loadYaml(filepath, content);
}

return configPath;
if (ext === 'js') {
return (cosmiconfig as any).loadJs(filepath, content);
}
}

return null;
return {
sync: loader,
async: loader,
};
}

export function loadAndParseConfig(filepath: string): Types.Config | never {
const ext = filepath.substr(filepath.lastIndexOf('.') + 1);
switch (ext) {
case 'yml':
return parseConfigFile(readFileSync(filepath, 'utf-8'));
case 'json':
return JSON.parse(readFileSync(filepath, 'utf-8'));
case 'js':
return require(resolve(process.cwd(), filepath));
default:
export async function loadConfig(
configFilePath?: string
):
| Promise<{
config: Types.Config;
filepath: string;
}>
| never {
const moduleName = 'codegen';
const cosmi = cosmiconfig(moduleName, {
searchPlaces: generateSearchPlaces(moduleName),
loaders: {
'.json': customLoader('json'),
'.yaml': customLoader('yaml'),
'.yml': customLoader('yaml'),
'.js': customLoader('js'),
noExt: customLoader('yaml'),
},
});
const result = await (configFilePath ? cosmi.load(configFilePath) : cosmi.search(process.cwd()));

if (!result) {
if (configFilePath) {
throw new DetailedError(
`Extension '${ext}' is not supported`,
`Config ${configFilePath} does not exist`,
`
Config ${filepath} couldn't be parsed. Extension '${ext}' is not supported.
Config ${configFilePath} does not exist.
$ graphql-codegen --config ${configFilePath}
Please make sure the --config points to a correct file.
`
);
}

throw new DetailedError(
`Unable to find Codegen config file!`,
`
Please make sure that you have a configuration file under the current directory!
`
);
}

if (result.isEmpty) {
throw new DetailedError(
`Found Codegen config file but it was empty!`,
`
Please make sure that you have a valid configuration file under the current directory!
`
);
}

return {
filepath: result.filepath,
config: result.config as Types.Config,
};
}

function getCustomConfigPath(cliFlags: YamlCliFlags): string | null | never {
const configFile = cliFlags.config;

return configFile ? resolve(process.cwd(), configFile) : null;
}

function collect<T = string>(val: T, memo: T[]): T[] {
@@ -76,31 +141,17 @@ export function parseArgv(argv = process.argv): Command & YamlCliFlags {

export async function createConfig(cliFlags: Command & YamlCliFlags = parseArgv(process.argv)): Promise<Types.Config | never> {
const customConfigPath = getCustomConfigPath(cliFlags);
const locations: string[] = [join(process.cwd(), './codegen.yml'), join(process.cwd(), './codegen.json')];

if (customConfigPath) {
locations.unshift(customConfigPath);
}

const filepath = locations.find(existsSync);

if (!filepath) {
throw new DetailedError(
`Unable to find Codegen config file!`,
`
Please make sure that you have a configuration file under the current directory!
`
);
}
const configSearchResult = await loadConfig(customConfigPath);

if (cliFlags.require && cliFlags.require.length > 0) {
for (const mod of cliFlags.require) {
await import(mod);
}
}

const parsedConfigFile = loadAndParseConfig(filepath);
parsedConfigFile.configFilePath = filepath;
const parsedConfigFile = configSearchResult.config as Types.Config;

parsedConfigFile.configFilePath = configSearchResult.filepath;

if (cliFlags.watch === true) {
parsedConfigFile.watch = cliFlags.watch;
8 changes: 4 additions & 4 deletions packages/graphql-codegen-cli/src/utils/listr-renderer.ts
Original file line number Diff line number Diff line change
@@ -46,22 +46,22 @@ export class Renderer {
.map(error => {
debugLog(`[CLI] Exited with an error`, error);

return { msg: isDetailedError(error) ? error.details : error, rawError: error };
return { msg: isDetailedError(error) ? error.details : null, rawError: error };
})
.map(({ msg, rawError }, i) => {
const source: string | Source | undefined = (err.errors[i] as any).source;

msg = chalk.gray(indentString(stripIndent(`${msg}`), 4));
msg = msg ? chalk.gray(indentString(stripIndent(`${msg}`), 4)) : null;
const stack = rawError.stack ? chalk.gray(indentString(stripIndent(rawError.stack), 4)) : null;

if (source) {
const sourceOfError = typeof source === 'string' ? source : source.name;
const title = indentString(`${logSymbol.error} ${sourceOfError}`, 2);

return [title, !stack ? msg : null, stack].filter(Boolean).join('\n');
return [title, msg, stack, stack].filter(Boolean).join('\n');
}

return stack ? stack : msg;
return [msg, stack].filter(Boolean).join('\n');
})
.join('\n\n');
logUpdate(['', count, details, ''].join('\n\n'));
6 changes: 4 additions & 2 deletions packages/graphql-codegen-cli/src/utils/watcher.ts
Original file line number Diff line number Diff line change
@@ -9,8 +9,8 @@ import { debugLog } from './debugging';
import { getLogger } from './logger';
import { join } from 'path';
import { FSWatcher } from 'chokidar';
import { loadAndParseConfig } from '../config';
import { lifecycleHooks } from '../hooks';
import { loadConfig } from '../config';

function log(msg: string) {
// double spaces to inline the message with Listr
@@ -119,7 +119,9 @@ export const createWatcher = (initialConfig: Types.Config, onNext: (result: Type

if (eventName === 'change' && config.configFilePath && fullPath === config.configFilePath) {
log(`${logSymbols.info} Config file has changed, reloading...`);
const newParsedConfig = loadAndParseConfig(config.configFilePath);
const configSearchResult = await loadConfig(config.configFilePath);

const newParsedConfig = configSearchResult.config as Types.Config;
newParsedConfig.watch = config.watch;
newParsedConfig.silent = config.silent;
newParsedConfig.overwrite = config.overwrite;
Loading