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

Fixed an issue where GQL definition of having Union self refs past depth limit was failing with RangeError. #57

Merged
merged 3 commits into from
Apr 5, 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
19 changes: 13 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
name: Tests
name: Test

on:
[pull_request]
push:
branches: [develop, master]
pull_request:

jobs:
Unit-Tests:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x, 18.x]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: '14.x'
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run test
- run: npm run test-lint
- run: npm run test-unit
10 changes: 10 additions & 0 deletions lib/assets/gql-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ module.exports = {

/* Union types */
if (curType.astNode && curType.astNode.kind === 'UnionTypeDefinition') {

/* Make sure UnionTypeDefinition are also not circularly referenced */
if ((crossReferenceKeyList.hasOwnProperty(crossReferenceKey) && crossReferenceKeyList[crossReferenceKey]) ||
curDepth > depthLimit
) {
crossReferenceKeyList[crossReferenceKey] = false;
return '';
}
crossReferenceKeyList[crossReferenceKey] = true;

const types = curType.getTypes();
if (types && types.length) {
const indent = `${' '.repeat(curDepth)}`,
Expand Down
27 changes: 26 additions & 1 deletion test/unit/converter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ var converter = require('../../index'),
invalidSchemaJson = require('./fixtures/invalidSchema.json'),
validSchemaSDL = fs.readFileSync(path.join(__dirname, './fixtures/validSchemaSDL.graphql')).toString(),
selfRefSchema = fs.readFileSync(path.join(__dirname, './fixtures/selfRefUnionTypeExample.graphql')).toString(),
seflRefDepthUnionSchema = fs.readFileSync(
path.join(__dirname, './fixtures/seflRefDepthUnionSchema.graphql')
).toString(),
customTypeNames = fs.readFileSync(path.join(__dirname, './fixtures/custom-queryname.gql')).toString(),
issue10 = fs.readFileSync(path.join(__dirname, './fixtures/issue#10.graphql')).toString(),
circularInput = fs.readFileSync(path.join(__dirname, './fixtures/circularInput.graphql')).toString(),
Expand Down Expand Up @@ -120,7 +123,7 @@ describe('Converter tests', function () {
it('should generate a collection for a valid SDL schema with a union type self referencing', function (done) {
convert({ type: 'string',
data: selfRefSchema
}, {}, function (error, result) {
}, { depth: 3 }, function (error, result) {
if (error) {
expect.fail(null, null, error);
return done();
Expand Down Expand Up @@ -268,6 +271,28 @@ describe('Converter tests', function () {
return done();
});
});

it('should generate a collection for a valid SDL schema with a union type self referencing' +
' and past allowed depth limit', function (done) {
convert({ type: 'string',
data: seflRefDepthUnionSchema
}, { depth: 3 }, function (error, result) {
if (error) {
expect.fail(null, null, error);
return done();
}
const collection = result.output[0].data;
expect(collection.item[0].item[0].request.body.mode).to.be.equal('graphql');
expect(collection.item[0].item[0].request.body.graphql).to.be.an('object');
expect(collection.item[0].item[0].request.body.graphql.query).to.be.a('string');
expect(collection.item[0].item[0].request.body.graphql.variables).to.be.a('string');
expect(collection.item[0].item[0].request.body.graphql.query).to.contain('# self reference detected');
expect(collection.item[0].item[0].request.body.graphql.query).to.contain('# skipping "createdBy"');
expect(collection.item[0].item[0].request.body.graphql.query).to.contain('# skipping "updatedBy"');

return done();
});
});
});

describe('Validate function', function () {
Expand Down
30 changes: 30 additions & 0 deletions test/unit/fixtures/seflRefDepthUnionSchema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# A date-time string at UTC, such as 2019-12-03T09:54:33Z, compliant with the date-time format.
scalar DateTime

type Organization {
id: ID!
createdBy: User!
updatedBy: User!
name: String!
avatarUrl: String
owner: String!
}

# User
type User {
id: ID!
organization: OrganizationOrStringUnion
idpId: String!
firstName: String!
lastName: String!
createdBy: UserOrStringUnion
updatedBy: UserOrStringUnion
}

union OrganizationOrStringUnion = Organization

union UserOrStringUnion = User

type Query {
User(id: String!): User!
}
Loading