Skip to content

Commit

Permalink
fix: check package resolution of @graphql-codegen/typescript-resolver…
Browse files Browse the repository at this point in the history
…s before passing it to codegen api (#516)
  • Loading branch information
piglovesyou authored Jun 12, 2021
1 parent 9ce2286 commit 1dd873a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 32 deletions.
29 changes: 4 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -472,22 +472,6 @@ TSConfigFile: tsconfig.compile.json
# for `gql()` and `load()` calls.
typeInjectEntrypoint: node_modules/@types/graphql-let/index.d.ts

# "schemaEntrypoint", optional. You need this only if you want to use Resolver Types.
# Since you could point to multiple schemas, this path is
# used to generate `.d.ts` to generate `*.graphqls.d.ts`. If you do this,
#
# schema: **/*.graphqls
# schemaEntrypoint: schema.graphqls
#
# you can import the generated resolver types like below.
#
# import { Resolvers } from '../schema.graphqls'
#
# It doesn't matter if the file of the path exists. I recommend
# you to specify a normal relative path without glob symbols (`**`) like this.
schemaEntrypoint: schema.graphqls
schemaEntrypoint: lib/schema.graphqls

# "silent", optional. `false` by default.
# Pass `true` if you want to suppress all standard output from graphql-let.
silent: false
Expand Down Expand Up @@ -598,26 +582,22 @@ schema. Just use what you need; it's most likely to be `jest-transform-graphql`.

If you meet the following conditions, graphql-let generates Resolver Types.

- You have `schemaEntrypoint` in the config
- You have file paths including glob patterns in `schema`
- You have
[`@graphql-codegen/typescript-resolvers`](https://graphql-code-generator.com/docs/plugins/typescript-resolvers)
installed
- your `schemaEntrypoint` in .graphql-let.yml points to a single local GraphQL
schema file (`.graphqls`)

Run:

```bash
yarn add -D @graphql-codegen/typescript-resolvers
yarn graphql-let
```

Then you will get `${schemaEntrypoint}.d.ts`. Import the types from it.
Then you will get resolver types in `graphql-let/__generated__/__types__`.

```typescript
import { Resolvers } from "../schema.graphqls";
import { Resolvers } from "graphql-let/__generated__/__types__";
const resolvers: Resolvers = {
Query: {
Expand All @@ -631,9 +611,8 @@ const resolvers: Resolvers = {
export default resolvers;
```

`graphql-let/schema/loader` is also available. It generates/updates
`graphql-let/__generated__/__types__.d.ts` but it doesn't transpile anything;
just passes the file content to the next webpack loader.
`graphql-let/schema/loader` is also available to update resolver types. It doesn't transpile anything;
just detects file modification and passes the content to the next loader.

```diff
// webpack.config.ts
Expand Down
5 changes: 3 additions & 2 deletions src/lib/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import { CodegenContext, getSchemaImportContext, isAllSkip } from './types';
import ConfiguredOutput = Types.ConfiguredOutput;

const OPTIONAL_SCHEMA_PLUGINS = ['typescript-resolvers'];
function findOptionalSchemaPlugins() {
function getOptionalSchemaPlugins() {
const plugins: string[] = [];
for (const c of OPTIONAL_SCHEMA_PLUGINS) {
try {
require(`@graphql-codegen/${c}`);
plugins.push(c);
// eslint-disable-next-line no-empty
} catch (e) {}
Expand All @@ -31,7 +32,7 @@ function findOptionalSchemaPlugins() {
// cases where you need to configure this more. Issue it then.
function getFixedSchemaConfig() {
return {
plugins: ['typescript', ...findOptionalSchemaPlugins()],
plugins: ['typescript', ...getOptionalSchemaPlugins()],
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export function genDts(

if (tsxFullPaths.length !== dtsContents.length) {
throw new Error(
`Never supposed to be here. Please make an issue on GitHub.`,
`Never. Requested .tsx length and result .d.ts length are not matched.`,
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/lib/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EOL } from 'os';

const shouldCareNewline = EOL !== '\n';
const RegexCRLF = /\r\n/g;
const leadingStringOfGeneratedContent = '/* ';
const leadingLettersForFileHash = '/* ';
const hexHashLength = 40;

function normalizeNewline(input: string | Buffer): string {
Expand All @@ -24,7 +24,7 @@ export function createHashFromBuffers(ss: (string | Buffer)[]): string {
}

export function withHash(sourceHash: string, content: string): string {
return `${leadingStringOfGeneratedContent}${sourceHash}
return `${leadingLettersForFileHash}${sourceHash}
* This file is automatically generated by graphql-let. */
${content}`;
Expand All @@ -36,8 +36,8 @@ export function readHash(filePath: string): string | null {
// https://gist.github.com/piglovesyou/537a8157ba691e8e9e023263bfc7838d
const content = readFileSync(filePath, 'utf-8');
const hash = content.slice(
leadingStringOfGeneratedContent.length,
leadingStringOfGeneratedContent.length + hexHashLength,
leadingLettersForFileHash.length,
leadingLettersForFileHash.length + hexHashLength,
);
if (hash) return hash;
return null;
Expand Down

0 comments on commit 1dd873a

Please sign in to comment.