Skip to content

Commit

Permalink
Don't parse the GraphQL schema twice on load (#3069)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Jun 4, 2020
1 parent fc3e8d9 commit 8df24d2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
7 changes: 7 additions & 0 deletions .changeset/four-trains-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@keystonejs/keystone': major
---

Prevented parsing the GraphQL schema twice.
- `keystone.getTypeDefs` now returns the parsed GraphQL AST instead of the raw SDL.
- `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`.
14 changes: 4 additions & 10 deletions packages/keystone/lib/Keystone/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const fs = require('fs');
const gql = require('graphql-tag');
const flattenDeep = require('lodash.flattendeep');
const memoize = require('micro-memoize');
Expand Down Expand Up @@ -560,7 +559,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 }) {
Expand All @@ -585,16 +584,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.
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.
return ['scalar Upload', ...this.getTypeDefs({ schemaName }).map(t => print(t))].join('\n');
}

createItem(listKey, itemData) {
Expand Down
12 changes: 4 additions & 8 deletions packages/keystone/tests/Keystone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ test('unique typeDefs', () => {
hero: { type: MockFieldType },
},
});
const schemaName = 'public';
const schema = keystone.getTypeDefs({ schemaName }).join('\n');
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);
Expand Down Expand Up @@ -200,8 +199,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 = keystone.dumpSchema();
expect(schema.match(/type FooBar {\s*foo: Int\s*bar: Float\s*}/g) || []).toHaveLength(1);
});

Expand All @@ -227,8 +225,7 @@ describe('Keystone.extendGraphQLSchema()', () => {
},
],
});
const schemaName = 'public';
const schema = keystone.getTypeDefs({ schemaName }).join('\n');
const schema = keystone.dumpSchema();
expect(schema.match(/double\(x: Int\): Int/g) || []).toHaveLength(1);
expect(keystone._customProvider._extendedQueries).toHaveLength(1);
});
Expand All @@ -255,8 +252,7 @@ describe('Keystone.extendGraphQLSchema()', () => {
},
],
});
const schemaName = 'public';
const schema = keystone.getTypeDefs({ schemaName }).join('\n');
const schema = keystone.dumpSchema();
expect(schema.match(/double\(x: Int\): Int/g) || []).toHaveLength(1);
expect(keystone._customProvider._extendedMutations).toHaveLength(1);
});
Expand Down

0 comments on commit 8df24d2

Please sign in to comment.