Skip to content

Commit

Permalink
Possible types error messaging (#1381)
Browse files Browse the repository at this point in the history
* Check for errors in generate possible types and provide human readable errors

* Create changeset
  • Loading branch information
blakewilson authored Apr 13, 2023
1 parent 0ad4567 commit c46eac9
Show file tree
Hide file tree
Showing 3 changed files with 305 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-stingrays-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@faustwp/cli': patch
---

Give human readable errors when `faust generatePossibleTypes` fails. Typically due to having "Public Introspection" disabled
254 changes: 253 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 47 additions & 24 deletions packages/faustwp-cli/src/generatePossibleTypes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'isomorphic-fetch';
import fs from 'fs';

import { infoLog, errorLog } from './stdout/index.js';
import { getGraphqlEndpoint } from './utils/index.js';
import { infoLog, errorLog, debugLog } from './stdout/index.js';
import { getGraphqlEndpoint, getWpUrl } from './utils/index.js';

type PossibleTypes = {
[key: string]: any;
Expand All @@ -20,14 +20,15 @@ type Subtype = {
export async function generatePossibleTypes(): Promise<void> {
const graphqlEndpoint = getGraphqlEndpoint();

const response: Response = await fetch(graphqlEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
variables: {},
query: `
try {
const response: Response = await fetch(graphqlEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
variables: {},
query: `
{
__schema {
types {
Expand All @@ -40,27 +41,49 @@ export async function generatePossibleTypes(): Promise<void> {
}
}
`,
}),
});
}),
});

const json: {
data: { __schema: { types: [] } };
errors?: {
message: string;
}[];
} = await response.json();
const possibleTypes = <PossibleTypes>{};

const json: {
data: { __schema: { types: [] } };
} = await response.json();
const possibleTypes = <PossibleTypes>{};
if (json.errors) {
const errors = json.errors.map((error) => {
return error.message;
});

json.data.__schema.types.forEach((supertype: Supertype) => {
if (supertype.possibleTypes) {
possibleTypes[supertype.name] = supertype.possibleTypes.map(
(subtype: Subtype) => subtype.name,
throw new Error(
`There were errors in the GraphQL request: ${errors.join(', ')}`,
);
}
});

try {
json.data.__schema.types.forEach((supertype: Supertype) => {
if (supertype.possibleTypes) {
possibleTypes[supertype.name] = supertype.possibleTypes.map(
(subtype: Subtype) => subtype.name,
);
}
});

fs.writeFileSync('./possibleTypes.json', JSON.stringify(possibleTypes));

infoLog("This project's possibleTypes schema has been updated!");
} catch (err) {
errorLog("Unable to update this project's possibleTypes schema", err);
debugLog(
`"faust generatePossibleTypes" failed with the following error: `,
err,
);

errorLog("Unable to update this project's possibleTypes schema");
errorLog(
`Make sure you have "Enable Public Introspection" checked in WPGraphQL: ${getWpUrl()}/wp-admin/admin.php?page=graphql-settings`,
);

process.exit(0);
}
infoLog("This project's possibleTypes schema has been updated!");
}

1 comment on commit c46eac9

@headless-platform-by-wp-engine

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out the recent updates to your Atlas environment:

App Environment URL Build
faustjs canary https://hg…wered.com ✅ (logs)

Learn more about building on Atlas in our documentation.

Please sign in to comment.