Skip to content

Commit

Permalink
To get app context, use custom deploy token if present
Browse files Browse the repository at this point in the history
x
  • Loading branch information
rebeccahum committed Mar 6, 2024
1 parent a642ccc commit 305ee4e
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/lib/api/app.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { DocumentNode } from '@apollo/client';
import { QueryOptions } from '@apollo/client/core/watchQueryOptions';
import gql from 'graphql-tag';

import { App, Exact, Scalars } from '../../graphqlTypes';
Expand All @@ -21,6 +23,18 @@ interface AppByIdQueryResult {
app?: App;
}

interface AppQueryOptions extends QueryOptions {
query: DocumentNode;
variables: {
id: number;
};
context?: {
headers: {
Authorization: string;
};
};
}

export default async function (
app: string | number,
fields: string = 'id,name',
Expand Down Expand Up @@ -55,7 +69,7 @@ export default async function (
app = parseInt( app, 10 );
}

const res = await api.query< AppByIdQueryResult, AppByIdQueryVariables >( {
const appQuery: AppQueryOptions = {
query: gql`query App( $id: Int ) {
app( id: $id ){
${ fields }
Expand All @@ -65,7 +79,18 @@ export default async function (
variables: {
id: app,
},
} );
};

const customDeployToken = process.env.WPVIP_DEPLOY_TOKEN;
if ( customDeployToken ) {
appQuery.context = {
headers: {
Authorization: `Bearer ${ customDeployToken }`,
},
};
}

const res = await api.query< AppByIdQueryResult, AppByIdQueryVariables >( appQuery );

if ( ! res.data.app ) {
return {};
Expand Down

0 comments on commit 305ee4e

Please sign in to comment.