Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline graphql-codegen deps #128

Merged
merged 6 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/moody-laws-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ts-gql/compiler": patch
---

Inline some dependencies to reduce the size of the package
3 changes: 1 addition & 2 deletions packages/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
"@babel/parser": "^7.9.6",
"@babel/runtime": "^7.9.2",
"@babel/types": "^7.9.6",
"@graphql-codegen/plugin-helpers": "^2.4.2",
"@graphql-codegen/typescript-operations": "^2.3.5",
"@nodelib/fs.walk": "^1.2.4",
"@ts-gql/config": "^0.9.2",
"chokidar": "^3.4.0",
Expand All @@ -41,6 +39,7 @@
"graphql": "^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || 14 || 15 || 16"
},
"devDependencies": {
"@graphql-codegen/plugin-helpers": "^5.0.3",
"@ts-gql/tag": "*",
"@types/babel__code-frame": "^7.0.1",
"@types/graceful-fs": "^4.1.3",
Expand Down
13 changes: 1 addition & 12 deletions packages/compiler/src/get-generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
DocumentValidationCache,
} from "./validate-documents";
import { DocumentNode } from "graphql";
import { weakMemoize } from "./weakMemoize";

function memoize<V>(fn: (arg: string) => V): (arg: string) => V {
const cache: { [key: string]: V } = {};
Expand All @@ -38,18 +39,6 @@ function memoize<V>(fn: (arg: string) => V): (arg: string) => V {

const walk = promisify(_walk);

function weakMemoize<Arg extends object, Return>(
fn: (arg: Arg) => Return
): (arg: Arg) => Return {
const cache = new WeakMap<Arg, Return>();
return (arg: Arg) => {
if (cache.has(arg)) return cache.get(arg)!;
const result = fn(arg);
cache.set(arg, result);
return result;
};
}

function getPrintCompilerError() {
let readFile = memoize((filename: string) => fs.readFile(filename, "utf8"));

Expand Down
14 changes: 11 additions & 3 deletions packages/compiler/src/operation-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from "./fs";
import { DocumentNode, ExecutableDefinitionNode } from "graphql";
import { codegen } from "./codegen-core";
import { codegen } from "./vendor/codegen-core";
import { hashString, parseTsGqlMeta } from "./utils";
import { FsOperation } from "./fs-operations";
import {
Expand All @@ -11,6 +11,7 @@
import { Config } from "@ts-gql/config";
import { lazyRequire } from "lazy-require.macro";
import { inlineIntoFirstOperationOrFragment } from "./inline-fragments";
import { typescriptOperationsPlugin } from "./vendor/typescript-operations";

Check warning on line 14 in packages/compiler/src/operation-types.ts

View workflow job for this annotation

GitHub Actions / Linting

'typescriptOperationsPlugin' is defined but never used

function getUsedFragments(node: ExecutableDefinitionNode) {
const visit = lazyRequire<typeof import("graphql/language/visitor")>().visit;
Expand All @@ -23,12 +24,20 @@
return [...usedFragments];
}

let plugin:
| typeof import("./vendor/typescript-operations").typescriptOperationsPlugin
| null = null;

async function generateOperationTypes(
config: Config,
operation: DocumentNode,
filename: string,
operationHash: string
): Promise<FsOperation> {
if (!plugin) {
plugin = (await import("./vendor/typescript-operations"))
.typescriptOperationsPlugin;
}
let result = codegen({
documents: [
{
Expand Down Expand Up @@ -62,8 +71,7 @@
},
],
pluginMap: {
"typescript-operations":
lazyRequire<typeof import("@graphql-codegen/typescript-operations")>(),
"typescript-operations": { plugin },
},
});

Expand Down
8 changes: 4 additions & 4 deletions packages/compiler/src/test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,11 @@ export const document = JSON.parse("{\\"kind\\":\\"Document\\",\\"definitions\\"
"type": "output",
},
{
"content": "// ts-gql-integrity:9d11274ae031b42219b854bcccba80db
"content": "// ts-gql-integrity:b66967fb35ee424fc53721db0fe28146
/*
ts-gql-meta-begin
{
"hash": "6272f8db7f5b43003cf2f82e304fb523"
"hash": "40967a7756b270276b96576086c615f3"
}
ts-gql-meta-end
*/
Expand All @@ -406,7 +406,7 @@ import { TypedDocumentNode } from "@ts-gql/tag";
type ThingQueryVariables = SchemaTypes.Exact<{ [key: string]: never; }>;


type ThingQuery = { readonly __typename: 'Query', readonly someObj: { readonly __typename: 'OutputThing', readonly arr: ReadonlyArray<{ readonly __typename: 'OutputThing', readonly other: string, readonly id: string }> } };
type ThingQuery = { readonly __typename: 'Query', readonly json: MyGloballyDefinedJSONType | null, readonly someObj: { readonly __typename: 'OutputThing', readonly arr: ReadonlyArray<{ readonly __typename: 'OutputThing', readonly other: string, readonly id: string }> } };



Expand All @@ -424,7 +424,7 @@ declare module "./@schema" {
}
}

export const document = JSON.parse("{\\"kind\\":\\"Document\\",\\"definitions\\":[{\\"kind\\":\\"OperationDefinition\\",\\"operation\\":\\"query\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"Thing\\"},\\"variableDefinitions\\":[],\\"directives\\":[],\\"selectionSet\\":{\\"kind\\":\\"SelectionSet\\",\\"selections\\":[{\\"kind\\":\\"Field\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"someObj\\"},\\"arguments\\":[],\\"directives\\":[],\\"selectionSet\\":{\\"kind\\":\\"SelectionSet\\",\\"selections\\":[{\\"kind\\":\\"Field\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"arr\\"},\\"arguments\\":[],\\"directives\\":[],\\"selectionSet\\":{\\"kind\\":\\"SelectionSet\\",\\"selections\\":[{\\"kind\\":\\"FragmentSpread\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"Frag_a\\"},\\"directives\\":[]}]}},{\\"kind\\":\\"FragmentSpread\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"Frag_b\\"},\\"directives\\":[]}]}}]}},{\\"kind\\":\\"FragmentDefinition\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"Frag_a\\"},\\"typeCondition\\":{\\"kind\\":\\"NamedType\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"OutputThing\\"}},\\"directives\\":[],\\"selectionSet\\":{\\"kind\\":\\"SelectionSet\\",\\"selections\\":[{\\"kind\\":\\"Field\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"other\\"},\\"arguments\\":[],\\"directives\\":[]}]}},{\\"kind\\":\\"FragmentDefinition\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"Frag_b\\"},\\"typeCondition\\":{\\"kind\\":\\"NamedType\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"OutputThing\\"}},\\"directives\\":[],\\"selectionSet\\":{\\"kind\\":\\"SelectionSet\\",\\"selections\\":[{\\"kind\\":\\"Field\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"arr\\"},\\"arguments\\":[],\\"directives\\":[],\\"selectionSet\\":{\\"kind\\":\\"SelectionSet\\",\\"selections\\":[{\\"kind\\":\\"Field\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"id\\"},\\"arguments\\":[],\\"directives\\":[]}]}}]}}]}")
export const document = JSON.parse("{\\"kind\\":\\"Document\\",\\"definitions\\":[{\\"kind\\":\\"OperationDefinition\\",\\"operation\\":\\"query\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"Thing\\"},\\"variableDefinitions\\":[],\\"directives\\":[],\\"selectionSet\\":{\\"kind\\":\\"SelectionSet\\",\\"selections\\":[{\\"kind\\":\\"Field\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"json\\"},\\"arguments\\":[],\\"directives\\":[]},{\\"kind\\":\\"Field\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"someObj\\"},\\"arguments\\":[],\\"directives\\":[],\\"selectionSet\\":{\\"kind\\":\\"SelectionSet\\",\\"selections\\":[{\\"kind\\":\\"Field\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"arr\\"},\\"arguments\\":[],\\"directives\\":[],\\"selectionSet\\":{\\"kind\\":\\"SelectionSet\\",\\"selections\\":[{\\"kind\\":\\"FragmentSpread\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"Frag_a\\"},\\"directives\\":[]}]}},{\\"kind\\":\\"FragmentSpread\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"Frag_b\\"},\\"directives\\":[]}]}}]}},{\\"kind\\":\\"FragmentDefinition\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"Frag_a\\"},\\"typeCondition\\":{\\"kind\\":\\"NamedType\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"OutputThing\\"}},\\"directives\\":[],\\"selectionSet\\":{\\"kind\\":\\"SelectionSet\\",\\"selections\\":[{\\"kind\\":\\"Field\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"other\\"},\\"arguments\\":[],\\"directives\\":[]}]}},{\\"kind\\":\\"FragmentDefinition\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"Frag_b\\"},\\"typeCondition\\":{\\"kind\\":\\"NamedType\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"OutputThing\\"}},\\"directives\\":[],\\"selectionSet\\":{\\"kind\\":\\"SelectionSet\\",\\"selections\\":[{\\"kind\\":\\"Field\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"arr\\"},\\"arguments\\":[],\\"directives\\":[],\\"selectionSet\\":{\\"kind\\":\\"SelectionSet\\",\\"selections\\":[{\\"kind\\":\\"Field\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"id\\"},\\"arguments\\":[],\\"directives\\":[]}]}}]}}]}")
",
"filename": "__generated__/ts-gql/Thing.ts",
"type": "output",
Expand Down
1 change: 1 addition & 0 deletions packages/compiler/src/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ test("something", async () => {
`,
graphql`
query Thing {
json
someObj {
arr {
...Frag_a
Expand Down
1 change: 1 addition & 0 deletions packages/compiler/src/vendor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
These packages are inlined from https://github.com/dotansimha/graphql-code-generator, mainly to remove the dependency on relay-compiler which adds quite a large dependency tree
32 changes: 32 additions & 0 deletions packages/compiler/src/vendor/auto-bind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// https://github.com/sindresorhus/auto-bind/blob/5f9859a2c163a6567f0595b6268ef667c8248c6a/index.js
const getAllProperties = (object: object) => {
const properties = new Set<[Record<string, any>, string | symbol]>();

do {
for (const key of Reflect.ownKeys(object)) {
properties.add([object, key]);
}
} while (
(object = Reflect.getPrototypeOf(object)!) &&
object !== Object.prototype
);

return properties;
};

export function autoBind<SelfType extends Record<string, any>>(
self: SelfType
): SelfType {
for (const [object, key] of getAllProperties(self.constructor.prototype)) {
if (key === "constructor") {
continue;
}

const descriptor = Reflect.getOwnPropertyDescriptor(object, key);
if (descriptor && typeof descriptor.value === "function") {
(self as any)[key] = self[key as string].bind(self);
}
}

return self;
}
206 changes: 206 additions & 0 deletions packages/compiler/src/vendor/typescript-operations/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import {
AvoidOptionalsConfig,
RawDocumentsConfig,
} from "../visitor-plugin-common";

/**
* @description This plugin generates TypeScript types based on your GraphQLSchema _and_ your GraphQL operations and fragments.
* It generates types for your GraphQL documents: Query, Mutation, Subscription and Fragment.
*
* Note: In most configurations, this plugin requires you to use `typescript as well, because it depends on its base types.
*/
export interface TypeScriptDocumentsPluginConfig extends RawDocumentsConfig {
/**
* @description The [GraphQL spec](https://spec.graphql.org/draft/#sel-FAHjBJFCAACE_Gh7d)
* allows arrays and a single primitive value for list input. This allows to
* deactivate that behavior to only accept arrays instead of single values. If
* set to `false`, the definition: `query foo(bar: [Int!]!): Foo` will output
* `bar: Array<Int>` instead of `bar: Array<Int> | Int` for the variable part.
* @default true
*
* @exampleMarkdown
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript'],
* config: {
* arrayInputCoercion: false
* },
* },
* },
* };
* export default config;
* ```
*/
arrayInputCoercion?: boolean;
/**
* @description This will cause the generator to avoid using TypeScript optionals (`?`) on types,
* so the following definition: `type A { myField: String }` will output `myField: Maybe<string>`
* instead of `myField?: Maybe<string>`.
* @default false
*
* @exampleMarkdown
* ## Override all definition types
*
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript'],
* config: {
* avoidOptionals: true
* },
* },
* },
* };
* export default config;
* ```
*
* ## Override only specific definition types
*
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript'],
* config: {
* avoidOptionals: {
* field: true
* inputValue: true
* object: true
* defaultValue: true
* }
* },
* },
* },
* };
* export default config;
* ```
*/
avoidOptionals?: boolean | AvoidOptionalsConfig;
/**
* @description Generates immutable types by adding `readonly` to properties and uses `ReadonlyArray`.
* @default false
*
* @exampleMarkdown
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript'],
* config: {
* immutableTypes: true
* },
* },
* },
* };
* export default config;
* ```
*/
immutableTypes?: boolean;

/**
* @description Set to `true` in order to generate output without `export` modifier.
* This is useful if you are generating `.d.ts` file and want it to be globally available.
* @default false
*
* @exampleMarkdown
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript'],
* config: {
* noExport: true
* },
* },
* },
* };
* export default config;
* ```
*/
noExport?: boolean;
/**
* @description Allow to override the type value of `Maybe`.
* @default T | null
*
* @exampleMarkdown
* ## Allow undefined
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript'],
* config: {
* maybeValue: 'T | null | undefined'
* },
* },
* },
* };
* export default config;
* ```
*
* ## Allow `null` in resolvers:
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript'],
* config: {
* maybeValue: 'T extends PromiseLike<infer U> ? Promise<U | null> : T | null'
* },
* },
* },
* };
* export default config;
* ```
*/
maybeValue?: string;

/**
* @description Adds undefined as a possible type for query variables
* @default false
*
* @exampleMarkdown
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript'],
* config: {
* allowUndefinedQueryVariables: true
* },
* },
* },
* };
* export default config;
* ```
*/

allowUndefinedQueryVariables?: boolean;
}
Loading
Loading