Skip to content

Commit

Permalink
Polish bootstrap case
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrove committed Dec 23, 2021
1 parent 05d1967 commit 3c76037
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
18 changes: 14 additions & 4 deletions plugins/netlify-plugin-netligraph/src/index.mjs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
Expand Down
47 changes: 42 additions & 5 deletions plugins/netlify-plugin-netligraph/src/netligraph.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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) => {
Expand Down

0 comments on commit 3c76037

Please sign in to comment.