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

fix: support variables nested in query when making private queries #39

Merged
merged 1 commit into from
Feb 9, 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ and this project adheres to

## [Unreleased]

## [v1.3.1] - 2024-02-08

### Fixed

- fix: support variables nested in query when making private queries

## [v1.3.0] - 2024-02-02

### Added
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wayfair/gqmock",
"version": "1.3.0",
"version": "1.3.1",
"description": "GQMock - GraphQL Mocking Service",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
5 changes: 4 additions & 1 deletion src/ApolloServerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
});
}

private createCustomMocks(fakerConfig: Record<string, any>) {

Check warning on line 74 in src/ApolloServerManager.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const mocks = {};

Object.entries(fakerConfig).forEach(([key, value]) => {
Expand All @@ -86,9 +86,9 @@
key,
value,
}: {
mocks: Record<string, any>;

Check warning on line 89 in src/ApolloServerManager.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
key: string;
value: Record<string, any>;

Check warning on line 91 in src/ApolloServerManager.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
}) {
if (
// detect a faker method definition:
Expand Down Expand Up @@ -129,7 +129,7 @@

private getFakerMethod(
fakerKeys: string[]
): (...args: any[]) => any | null | undefined {

Check warning on line 132 in src/ApolloServerManager.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 132 in src/ApolloServerManager.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
let fakerMethod = faker;

while (fakerKeys.length) {
Expand Down Expand Up @@ -197,11 +197,13 @@

async getNewMock({
query,
variables,
typeName,
operationName,
rollingKey,
}: {
query: string;
variables: Record<string, unknown>;
typeName: string;
operationName: string;
rollingKey: string;
Expand All @@ -213,9 +215,10 @@
rollingKey,
apolloServerManager: this,
});

const queryResult = await this.executeOperation({
query: newQuery,
variables: {},
variables,
operationName: this.getFieldName('privateQuery'),
});

Expand Down
1 change: 1 addition & 0 deletions src/seed/SeedManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ export default class SeedManager {
{
apolloServerManager,
query,
variables,
operationName,
}
);
Expand Down
12 changes: 9 additions & 3 deletions src/utilities/__tests__/buildPrivateTypeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,20 @@ describe('buildPrivateTypeQuery', function () {

it('should build a query for the correct nested inline fragment', () => {
const rollingKey = 'data.item.subItem1';
const query = `query itemQuery {
item {
const query = `query itemQuery($first: Int!, $second: String!) {
item(first: $first) {
__typename
id
... on ItemOne {
someField1
subItem1 {
__typename
id
fieldWithVariable(first: $first)
... on SubItemOne {
field1
anotherWithSameVariable(first: $first)
anotherWithDifferentVariable(second: $second)
}
... on SubItemTwo {
field2
Expand All @@ -209,11 +212,14 @@ describe('buildPrivateTypeQuery', function () {
}
}`;

const expectedQuery = `query gqmock_privateQuery {
const expectedQuery = `query gqmock_privateQuery($first: Int!, $second: String!) {
gqmock_SubItemOne {
__typename
id
fieldWithVariable(first: $first)
field1
anotherWithSameVariable(first: $first)
anotherWithDifferentVariable(second: $second)
}
__typename
}`;
Expand Down
31 changes: 31 additions & 0 deletions src/utilities/buildPrivateTypeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import {
InlineFragmentNode,
Kind,
OperationDefinitionNode,
VariableDefinitionNode,
parse,
print,
visit,
} from 'graphql';
import ApolloServerManager from '../ApolloServerManager';

Expand Down Expand Up @@ -57,6 +59,7 @@ export default function ({
apolloServerManager.schema as GraphQLSchema,
typeName
);

const queryAst = parse(query);
let node: ASTNode = queryAst.definitions.find((definition) => {
return (
Expand Down Expand Up @@ -207,10 +210,38 @@ export default function ({
},
],
},
variableDefinitions: [] as VariableDefinitionNode[],
},
],
};

const allVariableDefinitions: VariableDefinitionNode[] = [];
visit(queryAst, {
VariableDefinition(node) {
allVariableDefinitions.push(node);
},
});

const variableDefinitions: VariableDefinitionNode[] = [];
// @ts-ignore meh
visit(newQueryAst, {
Variable(node) {
const matchingVariable = allVariableDefinitions.find(
(definition) => definition.variable.name.value === node.name.value
);
if (matchingVariable) {
const addedToDefinitions = variableDefinitions.find(
(definition) => definition.variable.name.value === node.name.value
);
if (!addedToDefinitions) {
variableDefinitions.push(matchingVariable);
}
}
},
});

newQueryAst.definitions[0].variableDefinitions = variableDefinitions;

return apolloServerManager.addTypenameFieldsToQuery(
print(newQueryAst as ASTNode)
);
Expand Down
7 changes: 6 additions & 1 deletion src/utilities/deepMerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@
* @param {string} graphqlContext.operationName - GraphQL operation name
* @param {ApolloServerManager} graphqlContext.apolloServerManager - ApolloServerManager instance
* @param {object} options - Merge options
* @param graphqlContext.variables

Check warning on line 51 in src/utilities/deepMerge.ts

View workflow job for this annotation

GitHub Actions / lint

@param path declaration ("graphqlContext.variables") root node name ("graphqlContext") does not match previous real parameter name ("options")

Check warning on line 51 in src/utilities/deepMerge.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "graphqlContext.variables" description

Check warning on line 51 in src/utilities/deepMerge.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "graphqlContext.variables" type
* @returns {object} A merged object and a list of warnings
*/
async function deepMerge(
source: Record<string, unknown>,
seed: Record<string, unknown>,
graphqlContext: {
query: string;
variables: Record<string, unknown>;
operationName: string;
apolloServerManager: ApolloServerManager;
},
Expand All @@ -63,7 +65,7 @@
data: Record<string, unknown>;
warnings: string[];
}> {
const {query, operationName, apolloServerManager} = graphqlContext;
const {query, operationName, apolloServerManager, variables} = graphqlContext;
const warnings = new Set<string>();
/**
* Returns the result of merging target into source
Expand All @@ -87,6 +89,7 @@
) {
source = await apolloServerManager.getNewMock({
query,
variables,
typeName: target.__typename,
operationName,
rollingKey,
Expand All @@ -107,6 +110,7 @@
// this should happen regardless of overrides
const newSourceItemData = await apolloServerManager.getNewMock({
query,
variables,
typeName: sourceItem.__typename,
operationName,
rollingKey: newRollingKey,
Expand Down Expand Up @@ -145,6 +149,7 @@
// this should happen regardless of overrides
const newSourceItemData = await apolloServerManager.getNewMock({
query,
variables,
typeName: sourceItem.__typename,
operationName,
rollingKey: newRollingKey,
Expand Down
Loading