-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #219 from wunderio/feature/NEX-153-codegen-auth
NEX-153: Add necessary authentication to graphql-codegen
- Loading branch information
Showing
6 changed files
with
237 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
# .graphqlrc.yml | ||
# NOTE: this needs to be http and not https: | ||
# Also, this should be updated with the project's lando url: | ||
schema: "http://next-drupal-starterkit.lndo.site/graphql" | ||
documents: | ||
- "lib/graphql/**/*.{graphql,js,ts,jsx,tsx}" | ||
# This file exposes the graphql schema and documents to the graphql.vscode-graphql and other compatible IDE extensions | ||
schema: "./lib/graphql/schema.graphql" | ||
documents: "lib/graphql/**/*.{js,ts,jsx,tsx}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
|
||
const { loadEnvConfig } = require("@next/env"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const os = require("os"); | ||
|
||
loadEnvConfig(process.cwd()); | ||
|
||
/** | ||
* Gets an access token from Drupal and saves it the temporary directory, | ||
* so it can be used by graphql-codegen to authenticate to the Drupal GraphQL API. | ||
*/ | ||
void (async function getAndSaveAccessToken() { | ||
try { | ||
/* eslint-disable n/no-process-env */ | ||
const oauthUrl = `${process.env.NEXT_PUBLIC_DRUPAL_BASE_URL}/oauth/token`; | ||
const credentials = Buffer.from( | ||
`${process.env.DRUPAL_CLIENT_ID}:${process.env.DRUPAL_CLIENT_SECRET}`, | ||
).toString("base64"); | ||
/* eslint-enable n/no-process-env */ | ||
|
||
const response = await fetch(oauthUrl, { | ||
method: "POST", | ||
headers: { | ||
Authorization: `Basic ${credentials}`, | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
}, | ||
body: `grant_type=client_credentials`, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(response?.statusText || "Error getting access token."); | ||
} | ||
|
||
const result = await response.json(); | ||
|
||
const tokenFilePath = path.resolve(os.tmpdir(), ".drupal-access-token.txt"); | ||
|
||
fs.writeFileSync(tokenFilePath, result.access_token); | ||
|
||
console.log(`Access token saved to ${tokenFilePath}`); | ||
} catch (error) { | ||
console.log("Error getting or saving access token:", error); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters