From 6644e3b60652015a9898c49278b928d67154c31a Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Tue, 5 May 2020 17:22:59 -0400 Subject: [PATCH] Refactor Docs generation from GQL Schema to a separate function --- pages/graphql.vue | 102 ++++++++++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 45 deletions(-) diff --git a/pages/graphql.vue b/pages/graphql.vue index c0235b1f5..252bb9900 100644 --- a/pages/graphql.vue +++ b/pages/graphql.vue @@ -541,6 +541,56 @@ export default { console.log("Error", error) } }, + + // NOTE : schema required here is the GQL Schema document object, not the schema string + getDocsFromSchema(schema) { + if (schema.getQueryType()) { + const fields = schema.getQueryType().getFields() + const qFields = [] + for (const field in fields) { + qFields.push(fields[field]) + } + this.queryFields = qFields + } + + if (schema.getMutationType()) { + const fields = schema.getMutationType().getFields() + const mFields = [] + for (const field in fields) { + mFields.push(fields[field]) + } + this.mutationFields = mFields + } + + if (schema.getSubscriptionType()) { + const fields = schema.getSubscriptionType().getFields() + const sFields = [] + for (const field in fields) { + sFields.push(fields[field]) + } + this.subscriptionFields = sFields + } + + const typeMap = schema.getTypeMap() + const types = [] + + const queryTypeName = schema.getQueryType() ? schema.getQueryType().name : "" + const mutationTypeName = schema.getMutationType() ? schema.getMutationType().name : "" + const subscriptionTypeName = schema.getSubscriptionType() + ? schema.getSubscriptionType().name + : "" + + for (const type in typeMap) { + if ( + !typeMap[type].name.startsWith("__") && + ![queryTypeName, mutationTypeName, subscriptionTypeName].includes(typeMap[type].name) && + typeMap[type] instanceof gql.GraphQLObjectType + ) { + types.push(typeMap[type]) + } + } + this.gqlTypes = types + }, async getSchema() { const startTime = Date.now() @@ -574,56 +624,18 @@ export default { const data = await sendNetworkRequest(reqOptions, this.$store) const schema = gql.buildClientSchema(data.data.data) + + this.$store.commit("setGQLState", { + value: JSON.stringify(data.data.data), + attribute: "schemaIntrospection", + }) + this.schema = gql.printSchema(schema, { commentDescriptions: true, }) - if (schema.getQueryType()) { - const fields = schema.getQueryType().getFields() - const qFields = [] - for (const field in fields) { - qFields.push(fields[field]) - } - this.queryFields = qFields - } + this.getDocsFromSchema(schema) - if (schema.getMutationType()) { - const fields = schema.getMutationType().getFields() - const mFields = [] - for (const field in fields) { - mFields.push(fields[field]) - } - this.mutationFields = mFields - } - - if (schema.getSubscriptionType()) { - const fields = schema.getSubscriptionType().getFields() - const sFields = [] - for (const field in fields) { - sFields.push(fields[field]) - } - this.subscriptionFields = sFields - } - - const typeMap = schema.getTypeMap() - const types = [] - - const queryTypeName = schema.getQueryType() ? schema.getQueryType().name : "" - const mutationTypeName = schema.getMutationType() ? schema.getMutationType().name : "" - const subscriptionTypeName = schema.getSubscriptionType() - ? schema.getSubscriptionType().name - : "" - - for (const type in typeMap) { - if ( - !typeMap[type].name.startsWith("__") && - ![queryTypeName, mutationTypeName, subscriptionTypeName].includes(typeMap[type].name) && - typeMap[type] instanceof gql.GraphQLObjectType - ) { - types.push(typeMap[type]) - } - } - this.gqlTypes = types this.$refs.queryEditor.setValidationSchema(schema) this.$nuxt.$loading.finish() const duration = Date.now() - startTime