chore(refactor): switch to async/await approach (#317)

chore(refactor): switch to async/await approach
This commit is contained in:
Liyas Thomas
2019-11-20 19:43:22 +05:30
committed by GitHub

View File

@@ -140,7 +140,7 @@ export default {
}; };
}, },
methods: { methods: {
getSchema() { async getSchema() {
const startTime = Date.now(); const startTime = Date.now();
this.schemaString = "Loading..."; this.schemaString = "Loading...";
@@ -148,83 +148,82 @@ export default {
// The nuxt axios module will hide it when the request is made. // The nuxt axios module will hide it when the request is made.
this.$nuxt.$loading.start(); this.$nuxt.$loading.start();
axios try {
.post(this.url, { const res = await axios.post(this.url, {
query: gql.getIntrospectionQuery() query: gql.getIntrospectionQuery()
}) })
.then(res => {
const schema = gql.buildClientSchema(res.data.data);
this.schemaString = gql.printSchema(schema, {
commentDescriptions: true
});
if (schema.getQueryType()) { const schema = gql.buildClientSchema(res.data.data);
const fields = schema.getQueryType().getFields(); this.schemaString = gql.printSchema(schema, {
const qFields = []; commentDescriptions: true
for (const field in fields) { });
qFields.push(fields[field]);
} if (schema.getQueryType()) {
this.queryFields = qFields; const fields = schema.getQueryType().getFields();
const qFields = [];
for (const field in fields) {
qFields.push(fields[field]);
} }
this.queryFields = qFields;
}
if (schema.getMutationType()) { if (schema.getMutationType()) {
const fields = schema.getMutationType().getFields(); const fields = schema.getMutationType().getFields();
const mFields = []; const mFields = [];
for (const field in fields) { for (const field in fields) {
mFields.push(fields[field]); mFields.push(fields[field]);
}
this.mutationFields = mFields;
} }
this.mutationFields = mFields;
}
if (schema.getSubscriptionType()) { if (schema.getSubscriptionType()) {
const fields = schema.getSubscriptionType().getFields(); const fields = schema.getSubscriptionType().getFields();
const sFields = []; const sFields = [];
for (const field in fields) { for (const field in fields) {
sFields.push(fields[field]); sFields.push(fields[field]);
}
this.subscriptionFields = sFields;
} }
this.subscriptionFields = sFields;
}
const typeMap = schema.getTypeMap(); const typeMap = schema.getTypeMap();
const types = []; const types = [];
const queryTypeName = schema.getQueryType() const queryTypeName = schema.getQueryType()
? schema.getQueryType().name ? schema.getQueryType().name
: ""; : "";
const mutationTypeName = schema.getMutationType() const mutationTypeName = schema.getMutationType()
? schema.getMutationType().name ? schema.getMutationType().name
: ""; : "";
const subscriptionTypeName = schema.getSubscriptionType() const subscriptionTypeName = schema.getSubscriptionType()
? schema.getSubscriptionType().name ? schema.getSubscriptionType().name
: ""; : "";
for (const type in typeMap) { for (const type in typeMap) {
if ( if (
!typeMap[type].name.startsWith("__") && !typeMap[type].name.startsWith("__") &&
![queryTypeName, mutationTypeName, subscriptionTypeName].includes( ![queryTypeName, mutationTypeName, subscriptionTypeName].includes(
typeMap[type].name typeMap[type].name
) && ) &&
typeMap[type] instanceof gql.GraphQLObjectType typeMap[type] instanceof gql.GraphQLObjectType
) { ) {
types.push(typeMap[type]); types.push(typeMap[type]);
}
} }
this.gqlTypes = types; }
this.gqlTypes = types;
this.$nuxt.$loading.finish(); this.$nuxt.$loading.finish();
const duration = Date.now() - startTime; const duration = Date.now() - startTime;
this.$toast.info(`Finished in ${duration}ms`, { this.$toast.info(`Finished in ${duration}ms`, {
icon: "done" icon: "done"
}); });
}) } catch(error) {
.catch(error => {
this.$nuxt.$loading.finish(); this.$nuxt.$loading.finish();
this.schemaString = error + ". Check console for details."; this.schemaString = error + ". Check console for details.";
this.$toast.error(error + " (F12 for details)", { this.$toast.error(error + " (F12 for details)", {
icon: "error" icon: "error"
}); });
console.log("Error", error); console.log("Error", error);
}); }
} }
} }
}; };