From 699ccd1217a2803cbee36e1d9f82e4aec6ed5dcc Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Sun, 31 May 2020 04:55:54 +1100 Subject: [PATCH 1/7] Don't parse the GraphQL schema twice --- .changeset/four-trains-turn.md | 5 +++++ packages/keystone/lib/Keystone/index.js | 14 ++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) create mode 100644 .changeset/four-trains-turn.md diff --git a/.changeset/four-trains-turn.md b/.changeset/four-trains-turn.md new file mode 100644 index 00000000000..42d82070435 --- /dev/null +++ b/.changeset/four-trains-turn.md @@ -0,0 +1,5 @@ +--- +'@keystonejs/keystone': patch +--- + +Don't parse the GraphQL schema twice. diff --git a/packages/keystone/lib/Keystone/index.js b/packages/keystone/lib/Keystone/index.js index 7eb6734cc0e..6278bab7300 100644 --- a/packages/keystone/lib/Keystone/index.js +++ b/packages/keystone/lib/Keystone/index.js @@ -540,7 +540,7 @@ module.exports = class Keystone { subscriptions.length > 0 && `type Subscription { ${subscriptions.join('\n')} }`, ] .filter(s => s) - .map(s => print(gql(s))); + .map(s => gql(s)); } getResolvers({ schemaName }) { @@ -568,13 +568,11 @@ module.exports = class Keystone { dumpSchema(file, schemaName) { // The 'Upload' scalar is normally automagically added by Apollo Server // See: https://blog.apollographql.com/file-uploads-with-apollo-server-2-0-5db2f3f60675 - // Since we don't execute apollo server over this schema, we have to - // reinsert it. - const schema = ` - scalar Upload - ${this.getTypeDefs({ schemaName }).join('\n')} - `; - fs.writeFileSync(file, schema); + // Since we don't execute apollo server over this schema, we have to reinsert it. + fs.writeFileSync( + file, + ['scalar Upload', ...this.getTypeDefs({ schemaName }).map(t => print(t))].join('\n') + ); } createItem(listKey, itemData) { From 50688a9b6dfa752cb02816aab5b9d81ff3053885 Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Mon, 1 Jun 2020 03:35:33 +1100 Subject: [PATCH 2/7] Attempt to fix tests --- packages/keystone/tests/Keystone.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/keystone/tests/Keystone.test.js b/packages/keystone/tests/Keystone.test.js index e067c7e203f..62e379bf3be 100644 --- a/packages/keystone/tests/Keystone.test.js +++ b/packages/keystone/tests/Keystone.test.js @@ -228,7 +228,7 @@ describe('Keystone.extendGraphQLSchema()', () => { ], }); const schemaName = 'public'; - const schema = keystone.getTypeDefs({ schemaName }).join('\n'); + const schema = keystone.dumpSchema({ schemaName }); expect(schema.match(/double\(x: Int\): Int/g) || []).toHaveLength(1); expect(keystone._customProvider._extendedQueries).toHaveLength(1); }); @@ -256,7 +256,7 @@ describe('Keystone.extendGraphQLSchema()', () => { ], }); const schemaName = 'public'; - const schema = keystone.getTypeDefs({ schemaName }).join('\n'); + const schema = keystone.dumpSchema({ schemaName }); expect(schema.match(/double\(x: Int\): Int/g) || []).toHaveLength(1); expect(keystone._customProvider._extendedMutations).toHaveLength(1); }); From c8bc2223b1d115bdda6e1af1a98d178ab59c7503 Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Mon, 1 Jun 2020 03:40:26 +1100 Subject: [PATCH 3/7] Fooo --- packages/keystone/tests/Keystone.test.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/keystone/tests/Keystone.test.js b/packages/keystone/tests/Keystone.test.js index 62e379bf3be..6df13b60d80 100644 --- a/packages/keystone/tests/Keystone.test.js +++ b/packages/keystone/tests/Keystone.test.js @@ -3,6 +3,8 @@ const Keystone = require('../lib/Keystone'); const { List } = require('../lib/ListTypes'); const { Text, Relationship } = require('@keystonejs/fields'); +const { print } = require('graphql/language/printer'); + class MockFieldAdapter {} class MockFieldImplementation { @@ -228,7 +230,10 @@ describe('Keystone.extendGraphQLSchema()', () => { ], }); const schemaName = 'public'; - const schema = keystone.dumpSchema({ schemaName }); + const schema = keystone + .getTypeDefs({ schemaName }) + .map(t => print(t)) + .join('\n'); expect(schema.match(/double\(x: Int\): Int/g) || []).toHaveLength(1); expect(keystone._customProvider._extendedQueries).toHaveLength(1); }); @@ -256,7 +261,10 @@ describe('Keystone.extendGraphQLSchema()', () => { ], }); const schemaName = 'public'; - const schema = keystone.dumpSchema({ schemaName }); + const schema = keystone + .getTypeDefs({ schemaName }) + .map(t => print(t)) + .join('\n'); expect(schema.match(/double\(x: Int\): Int/g) || []).toHaveLength(1); expect(keystone._customProvider._extendedMutations).toHaveLength(1); }); From b567776d9e2b2365b591a3479035672b75b0c80c Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Mon, 1 Jun 2020 03:58:34 +1100 Subject: [PATCH 4/7] Na na na na na --- packages/keystone/tests/Keystone.test.js | 26 +++++++++++------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/keystone/tests/Keystone.test.js b/packages/keystone/tests/Keystone.test.js index 6df13b60d80..561d6f5d15e 100644 --- a/packages/keystone/tests/Keystone.test.js +++ b/packages/keystone/tests/Keystone.test.js @@ -5,6 +5,14 @@ const { Text, Relationship } = require('@keystonejs/fields'); const { print } = require('graphql/language/printer'); +const getSchemaAsString = keystone => { + const schemaName = 'public'; + return keystone + .getTypeDefs({ schemaName }) + .map(t => print(t)) + .join('\n'); +}; + class MockFieldAdapter {} class MockFieldImplementation { @@ -99,8 +107,7 @@ test('unique typeDefs', () => { hero: { type: MockFieldType }, }, }); - const schemaName = 'public'; - const schema = keystone.getTypeDefs({ schemaName }).join('\n'); + const schema = getSchemaAsString(keystone); expect(schema.match(/scalar Foo/g) || []).toHaveLength(1); expect(schema.match(/getFoo: Boolean/g) || []).toHaveLength(1); expect(schema.match(/mutateFoo: Boolean/g) || []).toHaveLength(1); @@ -202,8 +209,7 @@ describe('Keystone.extendGraphQLSchema()', () => { }); keystone.extendGraphQLSchema({ types: [{ type: 'type FooBar { foo: Int, bar: Float }' }] }); - const schemaName = 'public'; - const schema = keystone.getTypeDefs({ schemaName }).join('\n'); + const schema = getSchemaAsString(keystone); expect(schema.match(/type FooBar {\s*foo: Int\s*bar: Float\s*}/g) || []).toHaveLength(1); }); @@ -229,11 +235,7 @@ describe('Keystone.extendGraphQLSchema()', () => { }, ], }); - const schemaName = 'public'; - const schema = keystone - .getTypeDefs({ schemaName }) - .map(t => print(t)) - .join('\n'); + const schema = getSchemaAsString(keystone); expect(schema.match(/double\(x: Int\): Int/g) || []).toHaveLength(1); expect(keystone._customProvider._extendedQueries).toHaveLength(1); }); @@ -260,11 +262,7 @@ describe('Keystone.extendGraphQLSchema()', () => { }, ], }); - const schemaName = 'public'; - const schema = keystone - .getTypeDefs({ schemaName }) - .map(t => print(t)) - .join('\n'); + const schema = getSchemaAsString(keystone); expect(schema.match(/double\(x: Int\): Int/g) || []).toHaveLength(1); expect(keystone._customProvider._extendedMutations).toHaveLength(1); }); From 397a415c50a9c213c4af741d14bc236560edf717 Mon Sep 17 00:00:00 2001 From: Mike Date: Mon, 1 Jun 2020 10:15:45 +1000 Subject: [PATCH 5/7] Update .changeset/four-trains-turn.md --- .changeset/four-trains-turn.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/four-trains-turn.md b/.changeset/four-trains-turn.md index 42d82070435..61ab53535d3 100644 --- a/.changeset/four-trains-turn.md +++ b/.changeset/four-trains-turn.md @@ -2,4 +2,4 @@ '@keystonejs/keystone': patch --- -Don't parse the GraphQL schema twice. +Prevented parsing theGraphQL schema twice. Internal refactor only. From 2f36dfa40ff692a90c70976859d07d06a0892af5 Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Mon, 1 Jun 2020 18:17:54 +1100 Subject: [PATCH 6/7] Made dumpSchema return a string --- .changeset/four-trains-turn.md | 6 ++++-- packages/keystone/lib/Keystone/index.js | 8 ++------ packages/keystone/tests/Keystone.test.js | 18 ++++-------------- 3 files changed, 10 insertions(+), 22 deletions(-) diff --git a/.changeset/four-trains-turn.md b/.changeset/four-trains-turn.md index 61ab53535d3..08d0e96770c 100644 --- a/.changeset/four-trains-turn.md +++ b/.changeset/four-trains-turn.md @@ -1,5 +1,7 @@ --- -'@keystonejs/keystone': patch +'@keystonejs/keystone': major --- -Prevented parsing theGraphQL schema twice. Internal refactor only. +Prevented parsing the GraphQL schema twice. +- `keystone.getTypeDefs` now returns the parsed GraphQL AST instead of the raw SDL. +- `keystone.dumpSchema` now returns a the GraphQL schema as a string instead of writing it to file. Additionally, its first `file` argument was removed and now only takes a the schema name, which defaults to `public`. diff --git a/packages/keystone/lib/Keystone/index.js b/packages/keystone/lib/Keystone/index.js index 6278bab7300..a90924d5028 100644 --- a/packages/keystone/lib/Keystone/index.js +++ b/packages/keystone/lib/Keystone/index.js @@ -1,4 +1,3 @@ -const fs = require('fs'); const gql = require('graphql-tag'); const flattenDeep = require('lodash.flattendeep'); const memoize = require('micro-memoize'); @@ -565,14 +564,11 @@ module.exports = class Keystone { ); } - dumpSchema(file, schemaName) { + dumpSchema(schemaName = 'public') { // The 'Upload' scalar is normally automagically added by Apollo Server // See: https://blog.apollographql.com/file-uploads-with-apollo-server-2-0-5db2f3f60675 // Since we don't execute apollo server over this schema, we have to reinsert it. - fs.writeFileSync( - file, - ['scalar Upload', ...this.getTypeDefs({ schemaName }).map(t => print(t))].join('\n') - ); + return ['scalar Upload', ...this.getTypeDefs({ schemaName }).map(t => print(t))].join('\n'); } createItem(listKey, itemData) { diff --git a/packages/keystone/tests/Keystone.test.js b/packages/keystone/tests/Keystone.test.js index 561d6f5d15e..f9cb92f9d8d 100644 --- a/packages/keystone/tests/Keystone.test.js +++ b/packages/keystone/tests/Keystone.test.js @@ -3,16 +3,6 @@ const Keystone = require('../lib/Keystone'); const { List } = require('../lib/ListTypes'); const { Text, Relationship } = require('@keystonejs/fields'); -const { print } = require('graphql/language/printer'); - -const getSchemaAsString = keystone => { - const schemaName = 'public'; - return keystone - .getTypeDefs({ schemaName }) - .map(t => print(t)) - .join('\n'); -}; - class MockFieldAdapter {} class MockFieldImplementation { @@ -107,7 +97,7 @@ test('unique typeDefs', () => { hero: { type: MockFieldType }, }, }); - const schema = getSchemaAsString(keystone); + const schema = keystone.dumpSchema(); expect(schema.match(/scalar Foo/g) || []).toHaveLength(1); expect(schema.match(/getFoo: Boolean/g) || []).toHaveLength(1); expect(schema.match(/mutateFoo: Boolean/g) || []).toHaveLength(1); @@ -209,7 +199,7 @@ describe('Keystone.extendGraphQLSchema()', () => { }); keystone.extendGraphQLSchema({ types: [{ type: 'type FooBar { foo: Int, bar: Float }' }] }); - const schema = getSchemaAsString(keystone); + const schema = keystone.dumpSchema(); expect(schema.match(/type FooBar {\s*foo: Int\s*bar: Float\s*}/g) || []).toHaveLength(1); }); @@ -235,7 +225,7 @@ describe('Keystone.extendGraphQLSchema()', () => { }, ], }); - const schema = getSchemaAsString(keystone); + const schema = keystone.dumpSchema(); expect(schema.match(/double\(x: Int\): Int/g) || []).toHaveLength(1); expect(keystone._customProvider._extendedQueries).toHaveLength(1); }); @@ -262,7 +252,7 @@ describe('Keystone.extendGraphQLSchema()', () => { }, ], }); - const schema = getSchemaAsString(keystone); + const schema = keystone.dumpSchema(); expect(schema.match(/double\(x: Int\): Int/g) || []).toHaveLength(1); expect(keystone._customProvider._extendedMutations).toHaveLength(1); }); From f08794c2b8600f092d05912c2789a9f5252bb4b6 Mon Sep 17 00:00:00 2001 From: Tim Leslie Date: Thu, 4 Jun 2020 16:00:01 +1000 Subject: [PATCH 7/7] Update .changeset/four-trains-turn.md --- .changeset/four-trains-turn.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/four-trains-turn.md b/.changeset/four-trains-turn.md index 08d0e96770c..db4ac7f9947 100644 --- a/.changeset/four-trains-turn.md +++ b/.changeset/four-trains-turn.md @@ -4,4 +4,4 @@ Prevented parsing the GraphQL schema twice. - `keystone.getTypeDefs` now returns the parsed GraphQL AST instead of the raw SDL. -- `keystone.dumpSchema` now returns a the GraphQL schema as a string instead of writing it to file. Additionally, its first `file` argument was removed and now only takes a the schema name, which defaults to `public`. +- `keystone.dumpSchema` now returns the GraphQL schema as a string instead of writing it to file. Additionally, its first `file` argument was removed and now only takes a the schema name, which defaults to `public`.