Skip to content

Commit

Permalink
Fixes for ESM (#4910)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan authored Dec 8, 2022
1 parent b3c95da commit deb9912
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"access": "restricted",
"baseBranch": "master",
"updateInternalDependencies": "patch",
"ignore": ["website"],
"ignore": ["website", "@graphql-mesh/container"],
"changelog": ["@changesets/changelog-github", { "repo": "urigo/graphql-mesh" }],
"snapshot": {
"useCalculatedVersion": true,
Expand Down
10 changes: 10 additions & 0 deletions .changeset/smooth-rocks-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@graphql-mesh/apollo-link': patch
'@graphql-mesh/grpc': patch
'@omnigraph/json-schema': patch
'@graphql-mesh/plugin-newrelic': patch
'@graphql-mesh/string-interpolation': patch
'@graphql-mesh/types': patch
---

Fixes for ESM
16 changes: 10 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ jobs:
POSTGRES_PASSWORD: docker
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
--health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
ports:
# Maps tcp port 5432 on service container to the host
- 5432:5432
Expand All @@ -47,7 +44,9 @@ jobs:
MYSQL_ROOT_PASSWORD: passwd
ports:
- 33306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
options:
--health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s
--health-retries=3

steps:
- name: Checkout
Expand All @@ -67,7 +66,10 @@ jobs:
${{runner.os}}-${{matrix.node-version}}-jest-
- name: Download Postgres Dump
run: 'curl https://raw.githubusercontent.com/morenoh149/postgresDBSamples/master/worldDB-1.0/world.sql -o /tmp/backup.sql'
run:
'curl
https://raw.githubusercontent.com/morenoh149/postgresDBSamples/master/worldDB-1.0/world.sql
-o /tmp/backup.sql'
- name: Load Postgres Data
uses: tj-actions/[email protected]
with:
Expand All @@ -84,3 +86,5 @@ jobs:
NODE_TLS_REJECT_UNAUTHORIZED: 0
GH_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
LC_ALL: en-US
- name: Node.js integrity check
run: yarn bob check
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ package-lock.json
/.husky/_/
newrelic-agent.log
website/public/_redirects
packages/types/src/config-schema.ts
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
"clean": "rm -rf packages/**/dist packages/**/**/dist examples/**/node_modules/.bin/*mesh* .bob",
"copy-config-schema": "cp ./packages/types/src/config-schema.json ./packages/types/dist/esm && cp ./packages/types/src/config-schema.json ./packages/types/dist/cjs",
"fix-bin": "node scripts/fix-bin.js",
"generate-config-schema": "yarn graphql-to-config-schema --schema ./**/yaml-config.graphql --json ./packages/types/src/config-schema.json --typings ./packages/types/src/config.ts --markdown ./website/src/generated-markdown && npx prettier --write ./packages/types/src",
"generate-config-schema": "yarn graphql-to-config-schema --schema ./**/yaml-config.graphql --json ./packages/types/src/config-schema.json --typings ./packages/types/src/config.ts --markdown ./website/src/generated-markdown",
"lint": "eslint --ext .ts \"./packages/**/src/**/*.ts\"",
"postbuild": "yarn copy-config-schema && yarn fix-bin",
"postgenerate-config-schema": "node scripts/create-config-schema-ts.js && npx prettier --write ./packages/types/src",
"postinstall": "patch-package && husky install",
"prebuild": "yarn clean && yarn generate-config-schema",
"prebuild:website": "yarn build",
Expand Down
3 changes: 3 additions & 0 deletions packages/apollo-link/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
"directory": "dist"
},
"sideEffects": false,
"bob": {
"check": false
},
"typescript": {
"definition": "dist/typings/index.d.ts"
}
Expand Down
28 changes: 1 addition & 27 deletions packages/container/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,6 @@
"directory": "packages/container"
},
"license": "MIT",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"exports": {
".": {
"require": {
"types": "./dist/typings/index.d.cts",
"default": "./dist/cjs/index.js"
},
"import": {
"types": "./dist/typings/index.d.ts",
"default": "./dist/esm/index.js"
},
"default": {
"types": "./dist/typings/index.d.ts",
"default": "./dist/esm/index.js"
}
},
"./package.json": "./package.json"
},
"typings": "dist/typings/index.d.ts",
"scripts": {
"start": "graphql-mesh dev"
},
Expand All @@ -53,11 +33,5 @@
"graphql": "16.6.0",
"tslib": "2.4.1"
},
"publishConfig": {
"access": "public",
"directory": "dist"
},
"typescript": {
"definition": "dist/typings/index.d.ts"
}
"bob": false
}
9 changes: 4 additions & 5 deletions packages/handlers/grpc/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable import/no-duplicates */
import './patchLongJs';
import './patchLongJs.js';
import { MeshHandlerOptions, Logger, MeshHandler, YamlConfig } from '@graphql-mesh/types';
import { stringInterpolator } from '@graphql-mesh/string-interpolation';
import { ChannelCredentials, credentials, loadPackageDefinition } from '@grpc/grpc-js';
Expand All @@ -18,7 +18,6 @@ import { AnyNestedObject, IParseOptions, Message, RootConstructor } from 'protob
import protobufjs from 'protobufjs';
import grpcReflection from '@ardatan/grpc-reflection-js';
import { IFileDescriptorSet } from 'protobufjs/ext/descriptor';
import { FileDescriptorSet } from 'protobufjs/ext/descriptor/index.js';
import descriptor from 'protobufjs/ext/descriptor/index.js';

import { addIncludePathResolver, addMetaDataToCall, getTypeName } from './utils.js';
Expand Down Expand Up @@ -57,15 +56,15 @@ export default class GrpcHandler implements MeshHandler {
this.rootJsonAndDecodedDescriptorSets = store.proxy('descriptorSet.proto', {
codify: rootJsonAndDecodedDescriptorSets =>
`
import { FileDescriptorSet } from 'protobufjs/ext/descriptor/index.js';
import descriptor from 'protobufjs/ext/descriptor/index.js';
export default [
${rootJsonAndDecodedDescriptorSets
.map(
({ name, rootJson, decodedDescriptorSet }) => `
{
name: ${JSON.stringify(name)},
decodedDescriptorSet: FileDescriptorSet.fromObject(${JSON.stringify(
decodedDescriptorSet: descriptor.FileDescriptorSet.fromObject(${JSON.stringify(
decodedDescriptorSet.toJSON(),
null,
2,
Expand All @@ -81,7 +80,7 @@ ${rootJsonAndDecodedDescriptorSets
return jsonData.map(({ name, rootJson, decodedDescriptorSet }: any) => ({
name,
rootJson,
decodedDescriptorSet: FileDescriptorSet.fromObject(decodedDescriptorSet),
decodedDescriptorSet: descriptor.FileDescriptorSet.fromObject(decodedDescriptorSet),
}));
},
toJSON: rootJsonAndDecodedDescriptorSets => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
JSONSchemaLinkConfig,
JSONSchemaOperationConfig,
OperationHeadersConfiguration,
} from './types';
import { getOperationMetadata, isPubSubOperationConfig } from './utils';
} from './types.js';
import { getOperationMetadata, isPubSubOperationConfig } from './utils.js';
import { IStringifyOptions } from 'qs';
import { getNamedType, GraphQLInt, GraphQLObjectType, GraphQLString } from 'graphql';
import { process } from '@graphql-mesh/cross-helpers';
Expand Down
6 changes: 3 additions & 3 deletions packages/loaders/json-schema/src/addRootFieldResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import {
} from 'graphql';
import lodashSet from 'lodash.set';
import urlJoin from 'url-join';
import { resolveDataByUnionInputType } from './resolveDataByUnionInputType';
import { HTTPMethod } from './types';
import { isFileUpload } from './utils';
import { resolveDataByUnionInputType } from './resolveDataByUnionInputType.js';
import { HTTPMethod } from './types.js';
import { isFileUpload } from './utils.js';
import { stringify as qsStringify, parse as qsParse, IStringifyOptions } from 'qs';
import { createGraphQLError, memoize1 } from '@graphql-tools/utils';
import { getHeadersObj } from '@graphql-mesh/utils';
Expand Down
6 changes: 3 additions & 3 deletions packages/loaders/json-schema/src/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import {
addHTTPRootFieldResolver,
GlobalOptions,
HTTPRootFieldResolverOpts,
} from './addRootFieldResolver';
import { getTypeResolverFromOutputTCs } from './getTypeResolverFromOutputTCs';
import { ObjMapScalar } from './scalars';
} from './addRootFieldResolver.js';
import { getTypeResolverFromOutputTCs } from './getTypeResolverFromOutputTCs.js';
import { ObjMapScalar } from './scalars.js';
import { resolvers as scalarResolvers } from 'graphql-scalars';

export const LengthDirective = new GraphQLDirective({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import {
ResolveRootDirective,
ResolveRootFieldDirective,
TypeScriptDirective,
} from './directives';
} from './directives.js';
import { GraphQLFile, GraphQLVoid } from './scalars.js';

export interface TypeComposers {
Expand Down
29 changes: 18 additions & 11 deletions packages/plugins/newrelic/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ import { MeshPlugin, MeshPluginOptions, YamlConfig } from '@graphql-mesh/types';
import { useNewRelic } from '@envelop/newrelic';
import { stringInterpolator } from '@graphql-mesh/string-interpolation';
import { process } from '@graphql-mesh/cross-helpers';
import recordExternal from 'newrelic/lib/metrics/recorders/http_external';
import NAMES from 'newrelic/lib/metrics/names';
import cat from 'newrelic/lib/util/cat';
import recordExternal from 'newrelic/lib/metrics/recorders/http_external.js';
import NAMES from 'newrelic/lib/metrics/names.js';
import cat from 'newrelic/lib/util/cat.js';
import { getHeadersObj } from '@graphql-mesh/utils';
import { shim as instrumentationApi } from 'newrelic';
import newRelic from 'newrelic';

const EnvelopAttributeName = 'Envelop_NewRelic_Plugin';

export default function useMeshNewrelic(options: MeshPluginOptions<YamlConfig.NewrelicConfig>): MeshPlugin<any> {
export default function useMeshNewrelic(
options: MeshPluginOptions<YamlConfig.NewrelicConfig>,
): MeshPlugin<any> {
const instrumentationApi = newRelic?.shim;
if (!instrumentationApi?.agent) {
options.logger.error(
'Agent unavailable. Please check your New Relic Agent configuration and ensure New Relic is enabled.'
'Agent unavailable. Please check your New Relic Agent configuration and ensure New Relic is enabled.',
);
return {};
}
Expand All @@ -38,11 +41,12 @@ export default function useMeshNewrelic(options: MeshPluginOptions<YamlConfig.Ne
env: process.env,
})
: undefined,
})
}),
);
},
onExecute({ args: { contextValue } }) {
const operationSegment = instrumentationApi.getActiveSegment() || instrumentationApi.getSegment();
const operationSegment =
instrumentationApi.getActiveSegment() || instrumentationApi.getSegment();
segmentByRequestContext.set(contextValue.request || contextValue, operationSegment);
},
onDelegate({ sourceName, fieldName, args, context, key }) {
Expand All @@ -54,7 +58,7 @@ export default function useMeshNewrelic(options: MeshPluginOptions<YamlConfig.Ne
const sourceSegment = instrumentationApi.createSegment(
`source${delimiter}${sourceName || 'unknown'}${delimiter}${fieldName}`,
null,
parentSegment
parentSegment,
);
if (options.includeResolverArgs) {
if (args) {
Expand Down Expand Up @@ -83,7 +87,7 @@ export default function useMeshNewrelic(options: MeshPluginOptions<YamlConfig.Ne
const httpDetailSegment = instrumentationApi.createSegment(
name,
recordExternal(parsedUrl.host, 'graphql-mesh'),
parentSegment
parentSegment,
);
if (!httpDetailSegment) {
logger.error(`Unable to create segment for external request: ${name}`);
Expand Down Expand Up @@ -122,7 +126,10 @@ export default function useMeshNewrelic(options: MeshPluginOptions<YamlConfig.Ne
for (const key in responseHeadersObj) {
httpDetailSegment.addAttribute(`response.headers.${key}`, responseHeadersObj[key]);
}
if (agent.config.cross_application_tracer.enabled && !agent.config.distributed_tracing.enabled) {
if (
agent.config.cross_application_tracer.enabled &&
!agent.config.distributed_tracing.enabled
) {
try {
const { appData } = cat.extractCatHeaders(responseHeadersObj);
const decodedAppData = cat.parseAppData(agent.config, appData);
Expand Down
27 changes: 20 additions & 7 deletions packages/string-interpolation/src/interpolator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defaultOptions } from './statics/DefaultOptions';
import { defaultOptions } from './statics/DefaultOptions.js';
import lodashGet from 'lodash.get';
import { defaultModifiers } from './modifiers';
import { defaultModifiers } from './modifiers/index.js';
import JsonPointer from 'json-pointer';

export class Interpolator {
Expand Down Expand Up @@ -34,7 +34,9 @@ export class Interpolator {
}

if (typeof transform !== 'function') {
return new Error('Modifiers must have a transformer. Transformers must be a function that returns a value.');
return new Error(
'Modifiers must have a transformer. Transformers must be a function that returns a value.',
);
}

this.modifiers.push({ key: key.toLowerCase(), transform });
Expand Down Expand Up @@ -66,12 +68,17 @@ export class Interpolator {
getKeyFromMatch(match) {
const removeReservedSymbols = [':', '|'];
return this.removeDelimiter(
removeReservedSymbols.reduce((val, sym) => (val.indexOf(sym) > 0 ? this.removeAfter(val, sym) : val), match)
removeReservedSymbols.reduce(
(val, sym) => (val.indexOf(sym) > 0 ? this.removeAfter(val, sym) : val),
match,
),
);
}

removeDelimiter(val) {
return val.replace(new RegExp(this.delimiterStart(), 'g'), '').replace(new RegExp(this.delimiterEnd(), 'g'), '');
return val
.replace(new RegExp(this.delimiterStart(), 'g'), '')
.replace(new RegExp(this.delimiterEnd(), 'g'), '');
}

removeAfter(str, val) {
Expand Down Expand Up @@ -121,7 +128,10 @@ export class Interpolator {
if (dataToReplace) {
return str.replace(rule.replace, this.applyModifiers(rule.modifiers, dataToReplace, data));
} else if (rule.alternativeText) {
return str.replace(rule.replace, this.applyModifiers(rule.modifiers, rule.alternativeText, data));
return str.replace(
rule.replace,
this.applyModifiers(rule.modifiers, rule.alternativeText, data),
);
}

const defaultModifier = this.applyModifiers(rule.modifiers, rule.key, data);
Expand Down Expand Up @@ -158,7 +168,10 @@ export class Interpolator {
applyModifiers(modifiers, str, rawData) {
try {
const transformers = modifiers.map(modifier => modifier && modifier.transform);
return transformers.reduce((str, transform) => (transform ? transform(str, rawData) : str), str);
return transformers.reduce(
(str, transform) => (transform ? transform(str, rawData) : str),
str,
);
} catch (e) {
console.error(`An error occurred while applying modifiers to ${str}`, modifiers, e);
return str;
Expand Down
6 changes: 3 additions & 3 deletions packages/string-interpolation/src/modifiers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { uppercase } from './uppercase';
import { lowercase } from './lowercase';
import { titlecase } from './title';
import { uppercase } from './uppercase.js';
import { lowercase } from './lowercase.js';
import { titlecase } from './title.js';

export const defaultModifiers = [
{
Expand Down
3 changes: 1 addition & 2 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import {
} from '@graphql-tools/delegate';
import { TypedDocumentNode } from '@graphql-typed-document-node/core';
import { MeshStore } from '@graphql-mesh/store';
import configSchema from './config-schema.json';
import type { Plugin } from '@envelop/core';
import { PromiseOrValue } from 'graphql/jsutils/PromiseOrValue';
import { BatchDelegateOptions } from '@graphql-tools/batch-delegate';

export const jsonSchema: any = configSchema;
export { jsonSchema } from './config-schema.js';

export { YamlConfig };

Expand Down
11 changes: 11 additions & 0 deletions scripts/create-config-schema-ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { readFileSync, writeFileSync } = require('fs-extra');

const { join } = require('path');

const absoluteConfigSchemaPath = join(__dirname, '../packages/types/src/config-schema.json');
const absoluteConfigSchemaTsPath = join(__dirname, '../packages/types/src/config-schema.ts');

const configSchemaStr = readFileSync(absoluteConfigSchemaPath, 'utf-8');
const configSchemaTs = `export const jsonSchema: any = ${configSchemaStr} as any;`;

writeFileSync(absoluteConfigSchemaTsPath, configSchemaTs);

0 comments on commit deb9912

Please sign in to comment.