Skip to content

Commit

Permalink
Use fs.readFileSync to get package version
Browse files Browse the repository at this point in the history
Simpler change than #6553 that means we don't need the build system to
understand reading JSON files, just runtime (once at load time).
  • Loading branch information
glasser committed Jun 13, 2022
1 parent 3e3558c commit 0c1a6ce
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 2 additions & 4 deletions packages/server/src/plugin/schemaReporting/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import os from 'os';
import type { InternalApolloServerPlugin } from '../../internalPlugin';
import { v4 as uuidv4 } from 'uuid';
import { printSchema, validateSchema, buildSchema } from 'graphql';
import { SchemaReporter } from './schemaReporter';
import { packageVersion, SchemaReporter } from './schemaReporter';
import { schemaIsFederated } from '../schemaIsFederated';
import type { SchemaReport } from './generated/operations';
import type { BaseContext } from '../../externalTypes';
Expand Down Expand Up @@ -138,9 +138,7 @@ export function ApolloServerPluginSchemaReporting<TContext extends BaseContext>(
// "An identifier for the server instance. Length must be <= 256 characters.
serverId:
process.env.APOLLO_SERVER_ID || process.env.HOSTNAME || os.hostname(),
libraryVersion: `@apollo/server@${
require('../../../package.json').version
}`,
libraryVersion: `@apollo/server@${packageVersion}`,
};
let currentSchemaReporter: SchemaReporter | undefined;

Expand Down
8 changes: 7 additions & 1 deletion packages/server/src/plugin/schemaReporting/schemaReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type {
ReportSchemaResponse,
} from './generated/operations';
import type { Fetcher } from '@apollo/utils.fetcher';
import { readFileSync } from 'fs';
import { join } from 'path';

export const schemaReportGql = `mutation SchemaReport($report: SchemaReport!, $coreSchema: String) {
reportSchema(report: $report, coreSchema: $coreSchema) {
Expand All @@ -24,6 +26,10 @@ export const schemaReportGql = `mutation SchemaReport($report: SchemaReport!, $c
}
`;

export const packageVersion = JSON.parse(
readFileSync(join(__dirname, '..', '..', '..', 'package.json'), 'utf-8'),
).version as string;

// This class is meant to be a thin shim around the gql mutations.
export class SchemaReporter {
// These mirror the gql variables
Expand Down Expand Up @@ -53,7 +59,7 @@ export class SchemaReporter {
'Content-Type': 'application/json',
'x-api-key': options.apiKey,
'apollographql-client-name': 'ApolloServerPluginSchemaReporting',
'apollographql-client-version': require('../../../package.json').version,
'apollographql-client-version': packageVersion,
};

this.endpointUrl =
Expand Down
8 changes: 7 additions & 1 deletion packages/server/src/plugin/usageReporting/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ import type {
} from './options';
import { OurReport } from './stats';
import { makeTraceDetails } from './traceDetails';
import { readFileSync } from 'fs';
import { join } from 'path';

const reportHeaderDefaults = {
hostname: os.hostname(),
agentVersion: `@apollo/server@${require('../../../package.json').version}`,
agentVersion: `@apollo/server@${
JSON.parse(
readFileSync(join(__dirname, '..', '..', '..', 'package.json'), 'utf-8'),
).version
}`,
runtimeVersion: `node ${process.version}`,
// XXX not actually uname, but what node has easily.
uname: `${os.platform()}, ${os.type()}, ${os.release()}, ${os.arch()})`,
Expand Down

0 comments on commit 0c1a6ce

Please sign in to comment.