Skip to content

Commit

Permalink
Merge pull request #34 from wayfair-incubator/mfaga-support-unions-wh…
Browse files Browse the repository at this point in the history
…en-building-private-queries
  • Loading branch information
mjfaga authored Dec 21, 2023
2 parents 9ae05a8 + a09a02d commit d5e9d23
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 5 deletions.
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.2.1] - 2023-12-15

### Fixed

- feat: support unions when building private queries

## [v1.2.0] - 2023-11-01

### 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.2.0",
"version": "1.2.1",
"description": "GQMock - GraphQL Mocking Service",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
23 changes: 23 additions & 0 deletions src/ApolloServerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,27 @@ export default class ApolloServerManager {

return [];
}

getUnionImplementations(schema: GraphQLSchema, typeName: string): string[] {
const schemaAst = parse(printSchema(schema));
const unionTypeDefinitions = schemaAst.definitions.filter((definition) => {
return definition.kind === Kind.UNION_TYPE_DEFINITION;
});

if (unionTypeDefinitions && unionTypeDefinitions.length > 0) {
return (
unionTypeDefinitions
.filter((unionTypeDefinition) =>
// @ts-expect-error We know this is a union type definition
unionTypeDefinition?.types?.find(
(typeDefinition) => typeDefinition.name.value === typeName
)
)
// @ts-expect-error We know this is a union type definition
.map((unionTypeDefinition) => unionTypeDefinition.name.value) || []
);
}

return [];
}
}
3 changes: 3 additions & 0 deletions src/__fixtures__/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ type SubItemThree implements SubItem {
field3: String
}

union RandomThing = ItemOne | ItemTwo | ItemThree

interface Item {
id: String
type: String
Expand Down Expand Up @@ -101,6 +103,7 @@ type Query {
productByName(name: String!): Product
productBySku(sku: String!): Product
item: Item
random: RandomThing
items(type: String): [Item]
}

Expand Down
80 changes: 78 additions & 2 deletions src/utilities/__tests__/buildPrivateTypeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ const schema = fs.readFileSync(

describe('buildPrivateTypeQuery', function () {
let apolloServerManager;

beforeAll(() => {
apolloServerManager = new ApolloServerManager();
apolloServerManager.createApolloServer(schema, {});
});
it('should build a query for the correct inline fragment', () => {

it('should build a query for the correct interface inline fragment', () => {
const rollingKey = 'data.item';
const query = `query itemQuery {
item {
Expand Down Expand Up @@ -93,6 +95,80 @@ describe('buildPrivateTypeQuery', function () {
).toBe(expectedQuery);
});

it('should build a query for the correct union inline fragment', () => {
const rollingKey = 'data.random';
const query = `query randomQuery {
random {
__typename
id
... on ItemOne {
someField1
subItem1 {
__typename
id
... on SubItemOne {
field1
}
... on SubItemTwo {
field2
}
... on SubItemThree {
field3
}
}
products {
name
}
}
... on ItemTwo {
someField2
}
... on ItemThree {
someField3
}
}
}`;

const expectedQuery = `query gqmock_privateQuery {
gqmock_ItemOne {
__typename
id
someField1
subItem1 {
__typename
id
... on SubItemOne {
field1
__typename
}
... on SubItemTwo {
field2
__typename
}
... on SubItemThree {
field3
__typename
}
}
products {
name
__typename
}
}
__typename
}`;

expect(
buildPrivateTypeQuery({
query,
typeName: 'ItemOne',
operationName: 'randomQuery',
rollingKey,
apolloServerManager,
})
).toBe(expectedQuery);
});

it('should build a query for the correct nested inline fragment', () => {
const rollingKey = 'data.item.subItem1';
const query = `query itemQuery {
Expand Down Expand Up @@ -224,7 +300,7 @@ describe('buildPrivateTypeQuery', function () {
someField5
}
}
query itemsQuery {
officeItems: items(type: "office") {
...commonItemsFields
Expand Down
11 changes: 9 additions & 2 deletions src/utilities/buildPrivateTypeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export default function ({
apolloServerManager.schema as GraphQLSchema,
typeName
);
const unionImplementations = apolloServerManager.getUnionImplementations(
apolloServerManager.schema as GraphQLSchema,
typeName
);
const queryAst = parse(query);
let node: ASTNode = queryAst.definitions.find((definition) => {
return (
Expand Down Expand Up @@ -158,9 +162,12 @@ export default function ({
);
} else if (
selection.typeCondition &&
interfaceImplementations.includes(
(unionImplementations.includes(
selection.typeCondition.name.value
)
) ||
interfaceImplementations.includes(
selection.typeCondition.name.value
))
) {
subQueryNodesToVisit.push(selection);
}
Expand Down

0 comments on commit d5e9d23

Please sign in to comment.