Skip to content

adding capability to call graphQL APIs from node #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@
"atob": "2.1.2",
"axios": "^1.5.1",
"csrf": "^3.0.4",
"graphql-request": "^7.1.2",
"jsonwebtoken": "^9.0.2",
"query-string": "^6.12.1",
"rsa-pem-from-mod-exp": "^0.8.4",
"winston": "^3.1.0"
},
"devDependencies": {
"@snyk/protect": "^1.657.0",
"btoa": "^1.2.1",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
Expand All @@ -87,8 +89,7 @@
"nock": "^9.2.3",
"nyc": "^15.0.1",
"prettier": "^2.0.5",
"sinon": "^9.0.2",
"@snyk/protect": "^1.657.0"
"sinon": "^9.0.2"
},
"snyk": true
}
93 changes: 93 additions & 0 deletions sample/graphqlClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//Instructions:
// Run the program using this command from the terminal:
// node grapqlClient.js

// Install npm package
// npm install graphql-request

'use strict';

//Import necessary modules
const { GraphQLClient, gql } = require('graphql-request');

//constants
const GRAPHQL_API_ENDPOINT = "https://qb-sandbox.api.intuit.com/graphql";
const ENV = "Sandbox";
const API_TOKEN = "<your api token here>";

const client = new GraphQLClient(GRAPHQL_API_ENDPOINT, {
headers: {
Authorization: `Bearer ${API_TOKEN}`, // Optional: If your API requires authentication
},
});

//define your GraphQL query or mutation
const query = gql`
query RequestEmployerInfo {
payrollEmployerInfo {
employerCompensations {
edges {
node {
id
alternateIds {
id
}
name
type {
key
value
description
}
}
}
}
}
}
`;

//Handle input variables
const queryWithVariables = gql`
query getEmployeeCompensationById {
payrollEmployerInfo {
employerCompensations (filter: {$employeeId: ID!}){
edges {
node {
id
alternateIds {
id
}
name
}
}
}
}
}
`;
const variables = { employeeId: "3" };

//function to call the query with no variables
async function fetchData() {
try {
const data = await client.request(query);
console.log(JSON.stringify(data, null, 2));
} catch (error) {
console.error('Error fetching data:', error);
}
}

async function fetchDataWithVariables() {
try {
const data = await client.request(queryWithVariables, variables);
console.log(JSON.stringify(data, null, 2));
} catch (error) {
console.error('Error fetching data:', error);
}
}

//run the query (one without input variable)
fetchData();

//run the query (one with input variable)
//fetchDataWithVariables();