Skip to content

Commit

Permalink
Merge pull request #508 from AndrewBastin/feat/gql-query-schema-val
Browse files Browse the repository at this point in the history
GraphQL query validation based on schema
  • Loading branch information
AndrewBastin committed Jan 20, 2020
2 parents 8b970ed + 744d647 commit cd26801
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
46 changes: 35 additions & 11 deletions components/graphql/queryeditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export default {
data() {
return {
editor: null,
cacheValue: ""
cacheValue: "",
validationSchema: null
};
},
Expand Down Expand Up @@ -87,19 +88,42 @@ export default {
);
}
},
setValidationSchema(schema) {
this.validationSchema = schema;
this.parseContents(this.cacheValue);
},
parseContents: debounce(function(content) {
try {
gql.parse(content);
} catch (e) {
this.editor.session.setAnnotations([
{
row: e.locations[0].line - 1,
column: e.locations[0].column - 1,
text: e.message,
type: "error"
if (content !== "") {
try {
const doc = gql.parse(content);
if (this.validationSchema) {
this.editor.session.setAnnotations(
gql.validate(this.validationSchema, doc)
.map((err) => {
return {
row: err.locations[0].line - 1,
column: err.locations[0].column - 1,
text: err.message,
type: "error"
}
})
)
}
]);
} catch (e) {
this.editor.session.setAnnotations([
{
row: e.locations[0].line - 1,
column: e.locations[0].column - 1,
text: e.message,
type: "error"
}
]);
}
} else {
this.editor.session.setAnnotations([]);
}
}, 2000)
},
Expand Down
2 changes: 2 additions & 0 deletions pages/graphql.vue
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,8 @@ export default {
}
}
this.gqlTypes = types;
this.$refs.queryEditor.setValidationSchema(schema);
this.$nuxt.$loading.finish();
const duration = Date.now() - startTime;
Expand Down

0 comments on commit cd26801

Please sign in to comment.