10 examples of 'graphqlhttp' in JavaScript

Every line of 'graphqlhttp' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
17graphql(queryHash, variables = {}) {
18 return this.http.get('/graphql/query/', {
19 qs: {
20 query_hash: queryHash,
21 variables: JSON.stringify(variables),
22 },
23 });
24}
15export async function requestGraphQL(
16 query: string,
17 variables: any = {},
18 { instanceUrl, accessToken }: Options
19): Promise<{ data?: any; errors?: { message: string; path: string }[] }> {
20 const headers: Record = {
21 Accept: 'application/json',
22 'Content-Type': 'application/json',
23 'User-Agent': 'TypeScript language server',
24 }
25 if (accessToken) {
26 headers.Authorization = 'token ' + accessToken
27 }
28 const response = await got.post(new URL('/.api/graphql', instanceUrl), {
29 headers,
30 body: { query, variables },
31 json: true,
32 })
33 return response.body
34}
58export function graphql(query, options, ...rest) {
59 // The first definition is the main operation
60 const { operation } = query.definitions[0];
61 if (operation !== 'mutation') {
62 return (Component) => apolloGraphql(query, options, ...rest)(Component);
63 }
64 else if ( ! options || ! options.name && operation === 'mutation') {
65 console.warn('Mutation detected without a explicit name. No redux actions will be dispatched.');
66 return (Component) => apolloGraphql(query, options, ...rest)(Component);
67 }
68 else {
69 return (Component) => {
70 const Result = compose(
71 connect(),
72 apolloGraphql(query, options, ...rest),
73 reduxGraphql(options),
74 )(Component);
75 return hoistNonReactStatics(Result, Component);
76 };
77 }
78}
7function graphql (request, query, options) {
8 if (typeof query === 'string') {
9 options = Object.assign({ query }, options)
10 } else {
11 options = query
12 }
13
14 const requestOptions = Object.keys(options).reduce((result, key) => {
15 if (NON_VARIABLE_OPTIONS.includes(key)) {
16 result[key] = options[key]
17 return result
18 }
19
20 if (!result.variables) {
21 result.variables = {}
22 }
23
24 result.variables[key] = options[key]
25 return result
26 }, {})
27
28 return request(requestOptions)
29 .then(response => {
30 if (response.data.errors) {
31 throw new GraphqlError(requestOptions, response)
32 }
33
34 return response.data.data
35 })
36}
11export function graphql(request, query, options) {
12 options =
13 typeof query === "string"
14 ? (options = Object.assign({ query }, options))
15 : (options = query);
16 const requestOptions = Object.keys(options).reduce((result, key) => {
17 if (NON_VARIABLE_OPTIONS.includes(key)) {
18 result[key] = options[key];
19 return result;
20 }
21 if (!result.variables) {
22 result.variables = {};
23 }
24 result.variables[key] = options[key];
25 return result;
26 }, {});
27 return request(requestOptions).then((response) => {
28 if (response.data.errors) {
29 throw new GraphqlError(requestOptions, {
30 data: response.data,
31 });
32 }
33 return response.data.data;
34 });
35}
39graphql(query, variables = {}) {
40 const url = 'https://api.github.com/graphql'
41 const options = {
42 body: JSON.stringify({ query, variables }),
43 method: 'POST',
44 headers: this.getHeaders(),
45 }
46
47 return fetch(url, options).then(response => {
48 console.log(`${response.status} ${JSON.stringify(variables)}`)
49
50 if (!response.ok) {
51 return response.json().then(json => {
52 throw new Error(`${response.status}: ${JSON.stringify(json)}`)
53 })
54 }
55
56 return response.json().then(result => {
57 if (result.errors) {
58 throw new Error(`GraphQL Error: ${result.errors.map(e => e.message).join(', ')}`)
59 }
60
61 return result.data
62 })
63 })
64}
1export function getGraphQLHost() {
2 if (process && process.env && process.env.GRAPHQL_HOST) {
3 return process.env.GRAPHQL_HOST || 'localhost:3000';
4 } else if (typeof window !== 'undefined') {
5 return window && window.location && window.location.host || 'localhost:3000';
6 }
7 return 'localhost:3000';
8}
72async query>(
73 query: DocumentNode,
74 variables?: V,
75 queryParams?: QueryParams,
76): Promise {
77 const response = await this.request(query, variables, queryParams);
78 const result = await this.getResult(response);
79
80 if (response.ok && !result.errors && result.data) {
81 return result.data;
82 } else {
83 const errorResult = typeof result === 'string' ? { error: result } : result;
84 throw new ClientError(
85 { ...errorResult, status: response.status },
86 { query: print(query), variables },
87 );
88 }
89}
81export function callGraphQL({ query, variables = {}, headers = {} } : { query : string, variables? : { [string] : mixed }, headers? : { [string] : string } }) : ZalgoPromise {
82 return request({
83 url: GRAPHQL_URI,
84 method: 'POST',
85 json: {
86 query,
87 variables
88 },
89 headers: {
90 'x-app-name': SMART_PAYMENT_BUTTONS,
91 ...headers
92 }
93 }).then(({ status, body }) => {
94 const errors = body.errors || [];
95
96 if (errors.length) {
97 const message = errors[0].message || JSON.stringify(errors[0]);
98 throw new Error(message);
99 }
100
101 if (status !== 200) {
102 throw new Error(`${ GRAPHQL_URI } returned status ${ status }`);
103 }
104
105 return body.data;
106 });
107}
24export function* fetchGraphQL(query) {
25 const { graphQLUrl, token } = yield call(getGqlParams);
26 const response = yield call(fetchGraphQLApi, {
27 endpoint: graphQLUrl,
28 query,
29 token
30 });
31 return response;
32}

Related snippets