Merge pull request #508 from AndrewBastin/feat/gql-query-schema-val
GraphQL query validation based on schema
This commit is contained in:
@@ -33,7 +33,8 @@ export default {
|
|||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
editor: null,
|
editor: null,
|
||||||
cacheValue: ""
|
cacheValue: "",
|
||||||
|
validationSchema: null
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -88,9 +89,29 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
setValidationSchema(schema) {
|
||||||
|
this.validationSchema = schema;
|
||||||
|
this.parseContents(this.cacheValue);
|
||||||
|
},
|
||||||
|
|
||||||
parseContents: debounce(function(content) {
|
parseContents: debounce(function(content) {
|
||||||
|
if (content !== "") {
|
||||||
try {
|
try {
|
||||||
gql.parse(content);
|
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) {
|
} catch (e) {
|
||||||
this.editor.session.setAnnotations([
|
this.editor.session.setAnnotations([
|
||||||
{
|
{
|
||||||
@@ -101,6 +122,9 @@ export default {
|
|||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
this.editor.session.setAnnotations([]);
|
||||||
|
}
|
||||||
}, 2000)
|
}, 2000)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -813,6 +813,8 @@ export default {
|
|||||||
}
|
}
|
||||||
this.gqlTypes = types;
|
this.gqlTypes = types;
|
||||||
|
|
||||||
|
this.$refs.queryEditor.setValidationSchema(schema);
|
||||||
|
|
||||||
this.$nuxt.$loading.finish();
|
this.$nuxt.$loading.finish();
|
||||||
const duration = Date.now() - startTime;
|
const duration = Date.now() - startTime;
|
||||||
this.$toast.info(this.$t("finished_in", { duration }), {
|
this.$toast.info(this.$t("finished_in", { duration }), {
|
||||||
|
|||||||
Reference in New Issue
Block a user