Passed
Pull Request — master (#187)
by
unknown
01:51
created

graphqlClient.js ➔ fetchDataWithVariables   A

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
//Instructions: 
2
// Run the program using this command from the terminal: 
3
// node grapqlClient.js
4
5
// Install npm package
6
// npm install graphql-request
7
8
'use strict';
9
10
//Import necessary modules
11
const { GraphQLClient, gql } = require('graphql-request');
12
13
//constants
14
const GRAPHQL_API_ENDPOINT = "https://qb-sandbox.api.intuit.com/graphql";
15
const ENV = "Sandbox";
0 ignored issues
show
Unused Code introduced by
The constant ENV seems to be never used. Consider removing it.
Loading history...
16
const API_TOKEN = "<your api token here>";
17
18
const client = new GraphQLClient(GRAPHQL_API_ENDPOINT, {
19
    headers: {
20
      Authorization: `Bearer ${API_TOKEN}`, // Optional: If your API requires authentication
21
    },
22
  });
23
24
  //define your GraphQL query or mutation
25
  const query = gql`
26
  query RequestEmployerInfo {
27
    payrollEmployerInfo {
28
        employerCompensations {
29
            edges {
30
                node {
31
                    id
32
                    alternateIds {
33
                        id
34
                    }
35
                    name
36
                    type {
37
                        key
38
                        value
39
                        description
40
                    }
41
                }
42
            }
43
        }
44
    }
45
}
46
`;
47
48
//Handle input variables
49
const queryWithVariables = gql`
50
query getEmployeeCompensationById {
51
  payrollEmployerInfo {
52
    employerCompensations (filter: {$employeeId: ID!}){
53
      edges {
54
        node {
55
          id
56
          alternateIds {
57
            id
58
          }
59
          name
60
        }
61
      }
62
    }
63
  }
64
}
65
`;
66
const variables = { employeeId: "3" };
67
68
//function to call the query with no variables
69
async function fetchData() {
70
    try {
71
      const data = await client.request(query);
72
      console.log(JSON.stringify(data, null, 2));
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
73
    } catch (error) {
74
      console.error('Error fetching data:', error);
75
    }
76
  }
77
78
  async function fetchDataWithVariables() {
79
    try {
80
      const data = await client.request(queryWithVariables, variables);
81
      console.log(JSON.stringify(data, null, 2));
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
82
    } catch (error) {
83
      console.error('Error fetching data:', error);
84
    }
85
  }
86
87
//run the query (one without input variable)
88
fetchData();
89
90
//run the query (one with input variable)
91
//fetchDataWithVariables();
92
93
94