Skip to content

Commit

Permalink
Increase print/visit performance
Browse files Browse the repository at this point in the history
Co-Authored-By: Benjie <[email protected]>
  • Loading branch information
JoviDeCroock and benjie committed Dec 16, 2024
1 parent 48afd37 commit e53c6ce
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 6 deletions.
5 changes: 5 additions & 0 deletions benchmark/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ exports.bigSchemaSDL = fs.readFileSync(
'utf8',
);

exports.bigDocumentSDL = fs.readFileSync(
path.join(__dirname, 'kitchen-sink.graphql'),
'utf8',
);

exports.bigSchemaIntrospectionResult = JSON.parse(
fs.readFileSync(path.join(__dirname, 'github-schema.json'), 'utf8'),
);
65 changes: 65 additions & 0 deletions benchmark/kitchen-sink.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
query queryName($foo: ComplexType, $site: Site = MOBILE) @onQuery {
whoever123is: node(id: [123, 456]) {
id
... on User @onInlineFragment {
field2 {
id
alias: field1(first: 10, after: $foo) @include(if: $foo) {
id
...frag @onFragmentSpread
}
}
}
... @skip(unless: $foo) {
id
}
... {
id
}
}
}

mutation likeStory @onMutation {
like(story: 123) @onField {
story {
id @onField
}
}
}

subscription StoryLikeSubscription(
$input: StoryLikeSubscribeInput @onVariableDefinition
) @onSubscription {
storyLikeSubscribe(input: $input) {
story {
likers {
count
}
likeSentence {
text
}
}
}
}

fragment frag on Friend @onFragmentDefinition {
foo(
size: $size
bar: $b
obj: {
key: "value"
block: """
block string uses \"""
"""
}
)
}
{
unnamed(truthy: true, falsy: false, nullish: null)
query
}
query {
__typename
}
16 changes: 16 additions & 0 deletions benchmark/printer-benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const { parse } = require('graphql/language/parser.js');
const { print } = require('graphql/language/printer.js');

const { bigDocumentSDL } = require('./fixtures.js');

const document = parse(bigDocumentSDL);

module.exports = {
name: 'Print kitchen sink document',
count: 1000,
measure() {
print(document);
},
};
23 changes: 17 additions & 6 deletions src/language/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,23 @@ export function visit(
}
}
} else {
node = Object.defineProperties(
{},
Object.getOwnPropertyDescriptors(node),
);
for (const [editKey, editValue] of edits) {
node[editKey] = editValue;
const descriptors = Object.getOwnPropertyDescriptors(node);
node = { ...node };
for (const nodeKey of Object.keys(descriptors)) {
if (!(nodeKey in node)) {
const descriptor = descriptors[nodeKey];
if (
descriptor.enumerable &&
descriptor.configurable &&
descriptor.writable &&
!descriptor.get &&
!descriptor.set

Check warning on line 235 in src/language/visitor.ts

View check run for this annotation

Codecov / codecov/patch

src/language/visitor.ts#L232-L235

Added lines #L232 - L235 were not covered by tests
) {
// We already own this by means of the spread

Check warning on line 237 in src/language/visitor.ts

View check run for this annotation

Codecov / codecov/patch

src/language/visitor.ts#L237

Added line #L237 was not covered by tests
} else {
Object.defineProperty(node, nodeKey, descriptor);
}
}
}
}
}
Expand Down

0 comments on commit e53c6ce

Please sign in to comment.