Skip to content

Commit

Permalink
graphql cli support graphql 17 full esm
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloSzx committed May 29, 2022
1 parent 5888988 commit e079b06
Show file tree
Hide file tree
Showing 9 changed files with 833 additions and 892 deletions.
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
2 changes: 1 addition & 1 deletion packages/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async function main() {
minify: true,
external: ['graphql'],
banner: {
js: `import{createRequire}from"module";const require=createRequire(import.meta.url);\n`,
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'),
Expand Down
2 changes: 0 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@
"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",
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
65 changes: 33 additions & 32 deletions packages/cli/src/introspection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { AsyncExecutor } from '@graphql-tools/utils';
import type { GraphQLSchema } 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 @@ -24,35 +26,34 @@ export const getRemoteSchema = async (
*/
{ headers }: Pick<IntrospectionOptions, 'headers'> = {}
): Promise<GraphQLSchema> => {
const executor: AsyncExecutor = async ({ document, variables }) => {
const [{ request }, { print }] = await Promise.all([
import('undici'),
import('graphql'),
]);
headers ||=
(await gqtyConfigPromise).config.introspection?.headers ||
defaultConfig.introspection.headers;

const query = print(document);
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

0 comments on commit e079b06

Please sign in to comment.