From 3c76037f0449c42a3979513325756aa3cdd623a0 Mon Sep 17 00:00:00 2001 From: Sean Grove Date: Wed, 22 Dec 2021 17:07:18 -0800 Subject: [PATCH] Polish bootstrap case --- .../netlify-plugin-netligraph/src/index.mjs | 18 +++++-- .../src/netligraph.mjs | 47 +++++++++++++++++-- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/plugins/netlify-plugin-netligraph/src/index.mjs b/plugins/netlify-plugin-netligraph/src/index.mjs index 7a27c79..d06514a 100644 --- a/plugins/netlify-plugin-netligraph/src/index.mjs +++ b/plugins/netlify-plugin-netligraph/src/index.mjs @@ -1,8 +1,9 @@ // This is the main file for the Netlify Build plugin netligraph. // Please read the comments to learn more about the Netlify Build plugin syntax. // Find more information in the Netlify documentation. -import { extractFunctionsFromOperationDoc, fetchOneGraphSchema, generateFunctionsFile, fetchEnabledServices, readAndParseGraphQLOperationsSourceFile, readGraphQLOperationsSourceFile } from "./netligraph.mjs" +import { extractFunctionsFromOperationDoc, fetchOneGraphSchema, generateFunctionsFile, fetchEnabledServices, readAndParseGraphQLOperationsSourceFile, readGraphQLOperationsSourceFile, upsertAppForSite } from "./netligraph.mjs" import fs from "fs" +import { parse } from 'graphql' /* eslint-disable no-unused-vars */ export default { @@ -79,17 +80,26 @@ export default { const netligraphPath = `${process.cwd()}/netlify`; const appId = SITE_ID const authToken = NETLIFY_API_TOKEN + const oneGraphApp = upsertAppForSite(authToken, appId) const enabledServicesInfo = await fetchEnabledServices(authToken, appId) const enabledServices = enabledServicesInfo.map(service => service.service) const schema = await fetchOneGraphSchema(appId, enabledServices) - const [parsedDoc] = readAndParseGraphQLOperationsSourceFile(netligraphPath) - const appOperationsDoc = readGraphQLOperationsSourceFile(netligraphPath) + let appOperationsDoc = readGraphQLOperationsSourceFile(netligraphPath) + let [parsedDoc] = readAndParseGraphQLOperationsSourceFile(netligraphPath) + + if (appOperationsDoc.trim().length === 0) { + appOperationsDoc = `query PlaceholderQuery { + __typename + }` + parsedDoc = parse(appOperationsDoc) + } + const operations = extractFunctionsFromOperationDoc(parsedDoc) generateFunctionsFile(netligraphPath, schema, appOperationsDoc, operations) } catch (error) { // Report a user error - build.failBuild('Error message', { error }) + build.failBuild('Error generating Netligraph client', { error }) } // Console logs are shown in Netlify logs diff --git a/plugins/netlify-plugin-netligraph/src/netligraph.mjs b/plugins/netlify-plugin-netligraph/src/netligraph.mjs index 41efdfb..ffa4e99 100644 --- a/plugins/netlify-plugin-netligraph/src/netligraph.mjs +++ b/plugins/netlify-plugin-netligraph/src/netligraph.mjs @@ -208,6 +208,34 @@ query AppSchemaQuery( } } } +} + +mutation UpsertAppForSiteMutation( + $nfToken: String! + $siteId: String! +) { + oneGraph( + auths: { netlifyAuth: { oauthToken: $nfToken } } + ) { + upsertAppForNetlifySite( + input: { netlifySiteId: $siteId } + ) { + org { + id + name + } + app { + id + name + corsOrigins + customCorsOrigins { + friendlyServiceName + displayName + encodedValue + } + } + } + } }` export const fetchAppSchema = async ( @@ -224,14 +252,23 @@ export const fetchAppSchema = async ( return result.data?.oneGraph?.app?.graphQLSchema }; -export const getAuthToken = () => { - return '' - return Process.env.ONEGRAPH_ADMIN_JWT -} +export const upsertAppForSite = async ( + nfToken, + siteId +) => { + const result = await fetchOneGraph(nfToken, siteId, operationsDoc, 'UpsertAppForSiteMutation', + { + nfToken: nfToken, + appId: siteId, + }, + ); + + return result.data?.oneGraph?.upsertAppForNetlifySite?.app +}; export const fetchEnabledServices = async (authToken, appId) => { const appSchema = await fetchAppSchema(authToken, appId) - return appSchema?.services + return appSchema?.services || [] } const subscriptionParserName = (fn) => {