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

Don't parse the GraphQL schema twice on load #3069

Merged
merged 9 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
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 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`.
timleslie marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -540,7 +539,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 @@ -565,16 +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.
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