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

support graphql 17 full esm #895

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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/chilly-dingos-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gqty/cli': major
---

Support GraphQL 17 (full ESM)
2 changes: 1 addition & 1 deletion integration/graphql-17/ava.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export default {
extensions: {
ts: 'module',
},
nodeArguments: ['--require=bob-tsm', '--loader=bob-tsm'],
nodeArguments: ['--loader=bob-tsm'],
};
2 changes: 1 addition & 1 deletion integration/graphql-17/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.1",
"private": true,
"scripts": {
"generate": "bob-tsm src/generate.ts",
"generate": "node --loader=bob-tsm src/generate.ts",
"test": "pnpm i --ignore-scripts && c8 ava"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
"pnpm": {
"overrides": {
"trim@<0.0.3": ">=0.0.3",
"glob-parent@<5.1.2": ">=5.1.2"
"glob-parent@<5.1.2": ">=5.1.2",
"graphql-tag": "npm:[email protected]"
},
"peerDependencyRules": {
"allowedVersions": {
Expand Down
41 changes: 40 additions & 1 deletion packages/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { buildCode } from 'bob-ts';
import { build } from 'esbuild';
import { promises } from 'fs';
import rimraf from 'rimraf';
import replace from '@rollup/plugin-replace';
import pkg from './package.json';

async function main() {
Expand Down Expand Up @@ -44,6 +45,19 @@ async function main() {
minify: true,
external: ['graphql'],
}),
build({
bundle: true,
format: 'esm',
target: 'node12.20',
entryPoints: ['./src/deps.ts'],
outfile: 'dist/deps.mjs',
platform: 'node',
minify: true,
external: ['graphql'],
banner: {
js: `import{createRequire as createRequireTop}from"module";const require=createRequireTop(import.meta.url);\nimport{dirname as dirnameRoot}from"path";import{fileURLToPath as fileURLToPathRoot}from"url";const __dirname=dirnameRoot(fileURLToPathRoot(import.meta.url));`,
},
}),
promises.copyFile('LICENSE', 'dist/LICENSE'),
promises.copyFile('README.md', 'dist/README.md'),
writePackageJson({
Expand All @@ -65,7 +79,27 @@ async function main() {
'./src/index.ts',
],
clean: false,
format: 'interop',
format: 'cjs',
outDir: 'dist',
target: 'node12.20',
esbuild: {
minify: false,
},
sourcemap: false,
external: ['./deps.js'],
keepDynamicImport: false,
rollup: {
interop: 'esModule',
},
}),
buildCode({
entryPoints: [
'./src/inspectWriteGenerate.ts',
'./src/envelop.ts',
'./src/index.ts',
],
clean: false,
format: 'esm',
outDir: 'dist',
target: 'node12.20',
esbuild: {
Expand All @@ -76,6 +110,11 @@ async function main() {
keepDynamicImport: false,
rollup: {
interop: 'esModule',
plugins: [
replace({
'deps.js': 'deps.mjs',
}),
],
},
}),
]);
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@
"devDependencies": {
"@graphql-codegen/core": "^2.5.1",
"@graphql-codegen/typescript": "^2.4.11",
"@graphql-tools/delegate": "^8.7.10",
"@graphql-tools/utils": "^8.6.12",
"@graphql-tools/wrap": "^8.4.19",
"@rollup/plugin-replace": "^4.0.0",
"@size-limit/preset-small-lib": "^7.0.8",
"@types/lodash": "^4.14.182",
"@types/lodash.sortby": "^4.7.7",
Expand Down
2 changes: 0 additions & 2 deletions packages/cli/src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ export { printSchemaWithDirectives } from '@graphql-tools/utils';

export { default as sortBy } from 'lodash/sortBy.js';

export { introspectSchema, wrapSchema } from '@graphql-tools/wrap';

export { default as prettier, Options as PrettierOptions } from 'prettier';

export { default as mkdirp } from 'mkdirp';
Expand Down
36 changes: 19 additions & 17 deletions packages/cli/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,10 @@ import type {
GraphQLSchema,
GraphQLUnionType,
} from 'graphql';
import * as graphql from 'graphql';
import { defaultConfig, gqtyConfigPromise } from './config';
import * as deps from './deps.js';
import { formatPrettier } from './prettier';

const {
isEnumType,
isInputObjectType,
isInterfaceType,
isNullableType,
isObjectType,
isScalarType,
isUnionType,
lexicographicSortSchema,
parse,
isSchema,
assertSchema,
} = graphql;

export interface GenerateOptions {
/**
* The endpoint to use for the `queryFetcher` function
Expand Down Expand Up @@ -88,7 +73,7 @@ export interface GenerateOptions {
*/
transformSchema?: (
schema: GraphQLSchema,
graphql_js: typeof graphql
graphql_js: typeof import('graphql')
) => Promise<GraphQLSchema> | GraphQLSchema;
}

Expand Down Expand Up @@ -122,7 +107,24 @@ export async function generate(
scalarsEnumsHash: ScalarsEnumsHash;
isJavascriptOutput: boolean;
}> {
const gqtyConfig = (await gqtyConfigPromise).config;
const [gqtyConfig, graphql] = await Promise.all([
gqtyConfigPromise.then((v) => v.config),
import('graphql'),
]);

const {
isEnumType,
isInputObjectType,
isInterfaceType,
isNullableType,
isObjectType,
isScalarType,
isUnionType,
lexicographicSortSchema,
parse,
isSchema,
assertSchema,
} = graphql;

const isJavascriptOutput =
javascriptOutput ??
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/inspectWriteGenerate.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { existsSync, promises } from 'fs';
import type { GraphQLSchema, IntrospectionQuery } from 'graphql';
import * as graphql from 'graphql';
import { extname, resolve } from 'path';
import { defaultConfig, DUMMY_ENDPOINT, gqtyConfigPromise } from './config';
import * as deps from './deps.js';
import type { GenerateOptions, TransformSchemaOptions } from './generate';
import { getRemoteSchema } from './introspection';
import { writeGenerate } from './writeGenerate';

const { buildClientSchema, buildSchema } = graphql;

export async function inspectWriteGenerate({
endpoint,
destination,
Expand Down Expand Up @@ -101,7 +98,10 @@ export async function inspectWriteGenerate({
} else {
defaultConfig.introspection.endpoint = DUMMY_ENDPOINT;

const files = await deps.fg(endpoint);
const [files, { buildClientSchema, buildSchema }] = await Promise.all([
deps.fg(endpoint),
import('graphql'),
]);
if (files.length) {
const gqlextensions = ['.graphql', '.gql'];
const jsonFiles = files.filter((file) => extname(file) === '.json');
Expand Down
62 changes: 33 additions & 29 deletions packages/cli/src/introspection.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { AsyncExecutor } from '@graphql-tools/utils';
import type { GraphQLSchema } from 'graphql';
import * as graphql from 'graphql';
import type {
ExecutionResult,
GraphQLSchema,
IntrospectionQuery,
} from 'graphql';
import { defaultConfig, gqtyConfigPromise } from './config';
import * as deps from './deps.js';

export interface IntrospectionOptions {
/**
Expand All @@ -25,31 +26,34 @@ export const getRemoteSchema = async (
*/
{ headers }: Pick<IntrospectionOptions, 'headers'> = {}
): Promise<GraphQLSchema> => {
const executor: AsyncExecutor = async ({ document, variables }) => {
headers ||=
(await gqtyConfigPromise).config.introspection?.headers ||
defaultConfig.introspection.headers;
const query = graphql.print(document);
const { request } = await import('undici');
const { body } = await request(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...headers,
},
body: JSON.stringify({ query, variables }),
});
const response = await body.json();

return response;
};

const schema = deps.wrapSchema({
schema: await deps.introspectSchema(executor, {
endpoint,
}),
executor,
const [{ request }, { buildClientSchema, getIntrospectionQuery }] =
await Promise.all([import('undici'), import('graphql')]);

headers ||=
(await gqtyConfigPromise).config.introspection?.headers ||
defaultConfig.introspection.headers;

const query = getIntrospectionQuery({
inputValueDeprecation: true,
descriptions: true,
schemaDescription: true,
specifiedByUrl: true,
});

const { body } = await request(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...headers,
},
body: JSON.stringify({ query }),
});

return schema;
const introspectionQuery: ExecutionResult<IntrospectionQuery> =
await body.json();

if (!introspectionQuery.data)
throw Error('Unexpected error while introspecting schema');

return buildClientSchema(introspectionQuery.data);
};
Loading