Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

graphql-request

Package Overview
Dependencies
Maintainers
2
Versions
206
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-request - npm Package Compare versions

Comparing version 1.8.2 to 2.0.0

dist/examples/authentication-via-http-header.d.ts

19

dist/src/index.d.ts

@@ -1,27 +0,26 @@

import { GraphQLError, Headers as HttpHeaders, Options, Variables } from './types';
import { GraphQLError, Variables } from './types';
export { ClientError } from './types';
import 'cross-fetch/polyfill';
export declare class GraphQLClient {
private url;
private options;
constructor(url: string, options?: Options);
rawRequest<T extends any>(query: string, variables?: Variables): Promise<{
constructor(url: string, options?: RequestInit);
rawRequest<T = any>(query: string, variables?: Variables): Promise<{
data?: T;
extensions?: any;
headers: Headers;
headers: Request['headers'];
status: number;
errors?: GraphQLError[];
}>;
request<T extends any>(query: string, variables?: Variables): Promise<T>;
setHeaders(headers: HttpHeaders): GraphQLClient;
request<T = any>(query: string, variables?: Variables): Promise<T>;
setHeaders(headers: Response['headers']): GraphQLClient;
setHeader(key: string, value: string): GraphQLClient;
}
export declare function rawRequest<T extends any>(url: string, query: string, variables?: Variables): Promise<{
export declare function rawRequest<T = any>(url: string, query: string, variables?: Variables): Promise<{
data?: T;
extensions?: any;
headers: Headers;
headers: Request['headers'];
status: number;
errors?: GraphQLError[];
}>;
export declare function request<T extends any>(url: string, query: string, variables?: Variables): Promise<T>;
export declare function request<T = any>(url: string, query: string, variables?: Variables): Promise<T>;
export default request;
"use strict";
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
/// <reference lib="dom" />
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};

@@ -25,4 +29,4 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

while (_) try {
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, t.value];
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {

@@ -59,3 +63,2 @@ case 0: case 1: t = op; break;

exports.ClientError = types_2.ClientError;
require("cross-fetch/polyfill");
var GraphQLClient = /** @class */ (function () {

@@ -77,3 +80,3 @@ function GraphQLClient(url, options) {

});
return [4 /*yield*/, fetch(this.url, __assign({ method: 'POST', headers: Object.assign({ 'Content-Type': 'application/json' }, headers), body: body }, others))];
return [4 /*yield*/, fetch(this.url, __assign({ method: 'POST', headers: __assign({ 'Content-Type': 'application/json' }, headers), body: body }, others))];
case 1:

@@ -108,3 +111,3 @@ response = _b.sent();

});
return [4 /*yield*/, fetch(this.url, __assign({ method: 'POST', headers: Object.assign({ 'Content-Type': 'application/json' }, headers), body: body }, others))];
return [4 /*yield*/, fetch(this.url, __assign({ method: 'POST', headers: __assign({ 'Content-Type': 'application/json' }, headers), body: body }, others))];
case 1:

@@ -132,2 +135,3 @@ response = _b.sent();

GraphQLClient.prototype.setHeader = function (key, value) {
var _a;
var headers = this.options.headers;

@@ -141,3 +145,2 @@ if (headers) {

return this;
var _a;
};

@@ -148,19 +151,9 @@ return GraphQLClient;

function rawRequest(url, query, variables) {
return __awaiter(this, void 0, void 0, function () {
var client;
return __generator(this, function (_a) {
client = new GraphQLClient(url);
return [2 /*return*/, client.rawRequest(query, variables)];
});
});
var client = new GraphQLClient(url);
return client.rawRequest(query, variables);
}
exports.rawRequest = rawRequest;
function request(url, query, variables) {
return __awaiter(this, void 0, void 0, function () {
var client;
return __generator(this, function (_a) {
client = new GraphQLClient(url);
return [2 /*return*/, client.request(query, variables)];
});
});
var client = new GraphQLClient(url);
return client.request(query, variables);
}

@@ -170,16 +163,10 @@ exports.request = request;

function getResult(response) {
return __awaiter(this, void 0, void 0, function () {
var contentType;
return __generator(this, function (_a) {
contentType = response.headers.get('Content-Type');
if (contentType && contentType.startsWith('application/json')) {
return [2 /*return*/, response.json()];
}
else {
return [2 /*return*/, response.text()];
}
return [2 /*return*/];
});
});
var contentType = response.headers.get('Content-Type');
if (contentType && contentType.startsWith('application/json')) {
return response.json();
}
else {
return response.text();
}
}
//# sourceMappingURL=index.js.map
export declare type Variables = {
[key: string]: any;
};
export interface Headers {
[key: string]: string;
}
export interface Options {
method?: RequestInit['method'];
headers?: Headers;
mode?: RequestInit['mode'];
credentials?: RequestInit['credentials'];
cache?: RequestInit['cache'];
redirect?: RequestInit['redirect'];
referrer?: RequestInit['referrer'];
referrerPolicy?: RequestInit['referrerPolicy'];
integrity?: RequestInit['integrity'];
}
export interface GraphQLError {

@@ -41,3 +27,3 @@ message: string;

constructor(response: GraphQLResponse, request: GraphQLRequestContext);
private static extractMessage(response);
private static extractMessage;
}
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
}
return function (d, b) {

@@ -17,4 +20,8 @@ extendStatics(d, b);

var _this = this;
var message = ClientError.extractMessage(response) + ": " + JSON.stringify({ response: response, request: request });
var message = ClientError.extractMessage(response) + ": " + JSON.stringify({
response: response,
request: request,
});
_this = _super.call(this, message) || this;
Object.setPrototypeOf(_this, ClientError.prototype);
_this.response = response;

@@ -21,0 +28,0 @@ _this.request = request;

"use strict";
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};

@@ -25,4 +28,4 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

while (_) try {
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, t.value];
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {

@@ -61,4 +64,4 @@ case 0: case 1: t = op; break;

ava_1.default('minimal query', function (t) { return __awaiter(_this, void 0, void 0, function () {
var data;
var _this = this;
var data;
return __generator(this, function (_a) {

@@ -92,4 +95,4 @@ switch (_a.label) {

ava_1.default('minimal raw query', function (t) { return __awaiter(_this, void 0, void 0, function () {
var data, extensions;
var _this = this;
var data, extensions;
return __generator(this, function (_a) {

@@ -125,4 +128,4 @@ switch (_a.label) {

ava_1.default('minimal raw query with response headers', function (t) { return __awaiter(_this, void 0, void 0, function () {
var data, extensions, reqHeaders;
var _this = this;
var data, extensions, reqHeaders;
return __generator(this, function (_a) {

@@ -163,4 +166,4 @@ switch (_a.label) {

ava_1.default('basic error', function (t) { return __awaiter(_this, void 0, void 0, function () {
var errors;
var _this = this;
var errors;
return __generator(this, function (_a) {

@@ -170,9 +173,9 @@ switch (_a.label) {

errors = {
message: "Syntax Error GraphQL request (1:1) Unexpected Name \"x\"\n\n1: x\n ^\n",
message: 'Syntax Error GraphQL request (1:1) Unexpected Name "x"\n\n1: x\n ^\n',
locations: [
{
line: 1,
column: 1
}
]
column: 1,
},
],
};

@@ -183,3 +186,3 @@ return [4 /*yield*/, mock({ body: { errors: errors } }, function () { return __awaiter(_this, void 0, void 0, function () {

switch (_a.label) {
case 0: return [4 /*yield*/, t.throws(index_1.request('https://mock-api.com/graphql', "x"))];
case 0: return [4 /*yield*/, t.throwsAsync(index_1.request('https://mock-api.com/graphql', "x"))];
case 1:

@@ -199,4 +202,4 @@ err = _a.sent();

ava_1.default('raw request error', function (t) { return __awaiter(_this, void 0, void 0, function () {
var errors;
var _this = this;
var errors;
return __generator(this, function (_a) {

@@ -206,9 +209,9 @@ switch (_a.label) {

errors = {
message: "Syntax Error GraphQL request (1:1) Unexpected Name \"x\"\n\n1: x\n ^\n",
message: 'Syntax Error GraphQL request (1:1) Unexpected Name "x"\n\n1: x\n ^\n',
locations: [
{
line: 1,
column: 1
}
]
column: 1,
},
],
};

@@ -219,3 +222,3 @@ return [4 /*yield*/, mock({ body: { errors: errors } }, function () { return __awaiter(_this, void 0, void 0, function () {

switch (_a.label) {
case 0: return [4 /*yield*/, t.throws(index_1.rawRequest('https://mock-api.com/graphql', "x"))];
case 0: return [4 /*yield*/, t.throwsAsync(index_1.rawRequest('https://mock-api.com/graphql', "x"))];
case 1:

@@ -235,4 +238,4 @@ err = _a.sent();

ava_1.default('content-type with charset', function (t) { return __awaiter(_this, void 0, void 0, function () {
var data;
var _this = this;
var data;
return __generator(this, function (_a) {

@@ -248,3 +251,3 @@ switch (_a.label) {

headers: { 'Content-Type': 'application/json; charset=utf-8' },
body: { data: data }
body: { data: data },
}, function () { return __awaiter(_this, void 0, void 0, function () {

@@ -270,4 +273,4 @@ var _a, _b;

ava_1.default('extra fetch options', function (t) { return __awaiter(_this, void 0, void 0, function () {
var options, client;
var _this = this;
var options, client;
return __generator(this, function (_a) {

@@ -283,3 +286,3 @@ switch (_a.label) {

return [4 /*yield*/, mock({
body: { data: { test: 'test' } }
body: { data: { test: 'test' } },
}, function () { return __awaiter(_this, void 0, void 0, function () {

@@ -286,0 +289,0 @@ var actualOptions, name_1;

{
"name": "graphql-request",
"version": "1.8.2",
"version": "2.0.0",
"main": "dist/src/index.js",

@@ -17,3 +17,3 @@ "typings": "./dist/src/index.d.ts",

"type": "git",
"url": "git+https://github.com/graphcool/graphql-request.git"
"url": "git+https://github.com/prisma/graphql-request.git"
},

@@ -30,25 +30,31 @@ "keywords": [

"bugs": {
"url": "https://github.com/graphcool/graphql-request/issues"
"url": "https://github.com/prisma/graphql-request/issues"
},
"homepage": "https://github.com/graphcool/graphql-request",
"engines": {
"node": ">=10"
},
"homepage": "https://github.com/prisma/graphql-request",
"scripts": {
"format": "prettier --write .",
"prepublish": "npm run build",
"build": "rm -rf dist && tsc -d",
"lint": "tslint --type-check --project tsconfig.json {src,test}/**/*.ts",
"test": "npm run lint && npm run build && ava --serial && npm run size",
"size": "bundlesize"
"lint": "tslint --project . {examples,src,test}/**/*.ts",
"test": "yarn lint && yarn build && ava --serial",
"test:ci": "yarn test && bundlesize"
},
"dependencies": {},
"devDependencies": {
"@prisma-labs/prettier-config": "^0.1.0",
"@types/fetch-mock": "5.12.2",
"@types/node": "8.5.5",
"ava": "0.25.0",
"bundlesize": "0.17.0",
"ava": "^3.8.2",
"bundlesize": "^0.18.0",
"fetch-cookie": "0.7.2",
"fetch-mock": "5.13.1",
"tslint": "5.9.1",
"tslint-config-standard": "7.0.0",
"typescript": "2.7.2"
"prettier": "^2.0.5",
"tslint": "5.11.0",
"tslint-config-standard": "8.0.1",
"typescript": "3.0.3"
},
"dependencies": {
"cross-fetch": "2.2.2"
}
"prettier": "@prisma-labs/prettier-config"
}
# graphql-request
[![CircleCI](https://circleci.com/gh/prismagraphql/graphql-request.svg?style=shield)](https://circleci.com/gh/prismagraphql/graphql-request) [![npm version](https://badge.fury.io/js/graphql-request.svg)](https://badge.fury.io/js/graphql-request)
[![CircleCI](https://circleci.com/gh/prisma/graphql-request.svg?style=shield)](https://circleci.com/gh/prisma/graphql-request) [![npm version](https://badge.fury.io/js/graphql-request.svg)](https://badge.fury.io/js/graphql-request)

@@ -9,7 +9,6 @@ 📡 Minimal GraphQL client supporting Node and browsers for scripts or simple apps

* Most **simple and lightweight** GraphQL client
* Promise-based API (works with `async` / `await`)
* Typescript support (Flow coming soon)
- Most **simple and lightweight** GraphQL client
- Promise-based API (works with `async` / `await`)
- Typescript support (Flow coming soon)
## Install

@@ -37,3 +36,3 @@

request('https://api.graph.cool/simple/v1/movies', query).then(data => console.log(data))
request('https://api.graph.cool/simple/v1/movies', query).then((data) => console.log(data))
```

@@ -47,7 +46,7 @@

// Run GraphQL queries/mutations using a static function
request(endpoint, query, variables).then(data => console.log(data))
request(endpoint, query, variables).then((data) => console.log(data))
// ... or create a GraphQL client instance to send requests
const client = new GraphQLClient(endpoint, { headers: {} })
client.request(query, variables).then(data => console.log(data))
client.request(query, variables).then((data) => console.log(data))
```

@@ -62,20 +61,31 @@

const client = new GraphQLClient('my-endpoint', {
headers: {
Authorization: 'Bearer my-jwt-token',
},
})
async function main() {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
const query = `{
Movie(title: "Inception") {
releaseDate
actors {
name
const graphQLClient = new GraphQLClient(endpoint, {
headers: {
authorization: 'Bearer MY_TOKEN',
},
})
const query = /* GraphQL */ `
{
Movie(title: "Inception") {
releaseDate
actors {
name
}
}
}
}
}`
`
client.request(query).then(data => console.log(data))
const data = await graphQLClient.request(query)
console.log(JSON.stringify(data, undefined, 2))
}
main().catch((error) => console.error(error))
```
[TypeScript Source](examples/authentication-via-http-header.ts)
### Passing more options to fetch

@@ -86,19 +96,30 @@

const client = new GraphQLClient('my-endpoint', {
credentials: 'include',
mode: 'cors'
})
async function main() {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
const query = `{
Movie(title: "Inception") {
releaseDate
actors {
name
const graphQLClient = new GraphQLClient(endpoint, {
credentials: 'include',
mode: 'cors',
})
const query = /* GraphQL */ `
{
Movie(title: "Inception") {
releaseDate
actors {
name
}
}
}
}
}`
`
client.request(query).then(data => console.log(data))
const data = await graphQLClient.request(query)
console.log(JSON.stringify(data, undefined, 2))
}
main().catch((error) => console.error(error))
```
[TypeScript Source](examples/passing-more-options-to-fetch.ts)
### Using variables

@@ -109,18 +130,29 @@

const query = `query getMovie($title: String!) {
Movie(title: $title) {
releaseDate
actors {
name
async function main() {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
const query = /* GraphQL */ `
query getMovie($title: String!) {
Movie(title: $title) {
releaseDate
actors {
name
}
}
}
`
const variables = {
title: 'Inception',
}
}`
const variables = {
title: 'Inception',
const data = await request(endpoint, query, variables)
console.log(JSON.stringify(data, undefined, 2))
}
request('my-endpoint', query, variables).then(data => console.log(data))
main().catch((error) => console.error(error))
```
[TypeScript Source](examples/using-variables.ts)
### Error handling

@@ -131,14 +163,30 @@

const wrongQuery = `{
some random stuff
}`
async function main() {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
request('my-endpoint', query)
.then(data => console.log(data))
.catch(err => {
console.log(err.response.errors) // GraphQL response errors
console.log(err.response.data) // Response data if available
})
const query = /* GraphQL */ `
{
Movie(title: "Inception") {
releaseDate
actors {
fullname # "Cannot query field 'fullname' on type 'Actor'. Did you mean 'name'?"
}
}
}
`
try {
const data = await request(endpoint, query)
console.log(JSON.stringify(data, undefined, 2))
} catch (error) {
console.error(JSON.stringify(error, undefined, 2))
process.exit(1)
}
}
main().catch((error) => console.error(error))
```
[TypeScript Source](examples/error-handling)
### Using `require` instead of `import`

@@ -149,12 +197,21 @@

const query = `{
Movie(title: "Inception") {
releaseDate
actors {
name
async function main() {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
const query = /* GraphQL */ `
{
Movie(title: "Inception") {
releaseDate
actors {
name
}
}
}
}
}`
`
request('my-endpoint', query).then(data => console.log(data))
const data = await request(endpoint, query)
console.log(JSON.stringify(data, undefined, 2))
}
main().catch((error) => console.error(error))
```

@@ -165,25 +222,39 @@

```sh
npm install fetch-cookie/node-fetch
npm install fetch-cookie
```
```js
require('fetch-cookie/node-fetch')(require('node-fetch'))
import { GraphQLClient } from 'graphql-request'
// use this instead for cookie support
global['fetch'] = require('fetch-cookie/node-fetch')(require('node-fetch'))
async function main() {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
const client = new GraphQLClient('my-endpoint')
const graphQLClient = new GraphQLClient(endpoint, {
headers: {
authorization: 'Bearer MY_TOKEN',
},
})
const query = `{
Movie(title: "Inception") {
releaseDate
actors {
name
const query = /* GraphQL */ `
{
Movie(title: "Inception") {
releaseDate
actors {
name
}
}
}
}
}`
`
client.request(query).then(data => console.log(data))
const data = await graphQLClient.rawRequest(query)
console.log(JSON.stringify(data, undefined, 2))
}
main().catch((error) => console.error(error))
```
[TypeScript Source](examples/cookie-support-for-node)
### Receiving a raw response

@@ -197,19 +268,29 @@

const query = `{
Movie(title: "Inception") {
releaseDate
actors {
name
async function main() {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
const query = /* GraphQL */ `
{
Movie(title: "Inception") {
releaseDate
actors {
name
}
}
}
}
}`
`
rawRequest('my-endpoint', query).then(({data, extensions}) => console.log(data, extensions))
const { data, errors, extensions, headers, status } = await rawRequest(endpoint, query)
console.log(JSON.stringify({ data, errors, extensions, headers, status }, undefined, 2))
}
main().catch((error) => console.error(error))
```
[TypeScript Source](examples/receiving-a-raw-response)
### More examples coming soon...
* Fragments
* Using [`graphql-tag`](https://github.com/apollographql/graphql-tag)
* Typed Typescript return values
- Fragments
- Using [`graphql-tag`](https://github.com/apollographql/graphql-tag)

@@ -228,3 +309,2 @@ ## FAQ

## Help & Community [![Slack Status](https://slack.prisma.io/badge.svg)](https://slack.prisma.io)

@@ -231,0 +311,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc