graphql-request
Advanced tools
Comparing version 7.2.0-next.12 to 7.2.0-next.13
@@ -17,2 +17,5 @@ import type { DocumentNode, ExecutionResult, GraphQLSchema } from 'graphql'; | ||
transport: TransportHttp; | ||
transportConstructorConfig: { | ||
headers?: HeadersInit; | ||
}; | ||
} & HttpProperties) | ({ | ||
@@ -19,0 +22,0 @@ transport: TransportMemory; |
@@ -5,3 +5,3 @@ import { print } from 'graphql'; | ||
import { parseExecutionResult } from '../../lib/graphqlHTTP.js'; | ||
import { CONTENT_TYPE_GQL, CONTENT_TYPE_JSON } from '../../lib/http.js'; | ||
import { CONTENT_TYPE_GQL, CONTENT_TYPE_JSON, mergeHeadersInit } from '../../lib/http.js'; | ||
import { casesExhausted } from '../../lib/prelude.js'; | ||
@@ -119,5 +119,6 @@ import { execute } from '../0_functions/execute.js'; | ||
case `http`: { | ||
const headers = mergeHeadersInit(input.transportConstructorConfig.headers ?? {}, input.request.headers ?? {}); | ||
const response = await slots.fetch(new Request(input.request.url, { | ||
method: input.request.method, | ||
headers: input.request.headers, | ||
headers, | ||
body: input.request.body, | ||
@@ -124,0 +125,0 @@ })); |
@@ -38,2 +38,5 @@ import { GraphQLSchema } from 'graphql'; | ||
schema: input.schema, | ||
transportConstructorConfig: { | ||
headers: input.headers, | ||
}, | ||
context: { | ||
@@ -117,2 +120,5 @@ config: context.config, | ||
transport, | ||
transportConstructorConfig: { | ||
headers: input.headers, | ||
}, | ||
document: rawInput.document, | ||
@@ -119,0 +125,0 @@ schema: input.schema, |
@@ -42,3 +42,3 @@ import type { GraphQLSchema } from 'graphql'; | ||
/** | ||
* Headers to send with the request. | ||
* Headers to send with each sent request. | ||
*/ | ||
@@ -45,0 +45,0 @@ headers?: HeadersInit; |
@@ -8,2 +8,3 @@ export declare const ACCEPT_HEADER = "Accept"; | ||
}; | ||
export declare const mergeHeadersInit: (headers: HeadersInit, additionalHeaders: HeadersInit) => Headers; | ||
//# sourceMappingURL=http.d.ts.map |
@@ -8,2 +8,10 @@ export const ACCEPT_HEADER = `Accept`; | ||
}; | ||
export const mergeHeadersInit = (headers, additionalHeaders) => { | ||
const base = new Headers(headers); | ||
const additional = new Headers(additionalHeaders); | ||
for (const [key, value] of additional.entries()) { | ||
base.set(key, value); | ||
} | ||
return base; | ||
}; | ||
//# sourceMappingURL=http.js.map |
{ | ||
"name": "graphql-request", | ||
"version": "7.2.0-next.12", | ||
"version": "7.2.0-next.13", | ||
"packageManager": "pnpm@9.6.0", | ||
@@ -62,3 +62,3 @@ "type": "module", | ||
"gen:test:schema": "tsx tests/_/schemaGenerate.ts", | ||
"gen:docs:examples:clients:countries": "pnpm graffle --name countriesClient --schema https://countries.trevorblades.com/graphql --output ./examples/generated-clients/countries --libraryPathClient ../../../src/entrypoints/alpha/client.js --libraryPathSchema ../../../src/entrypoints/alpha/schema.js --libraryPathScalars ../../../src/entrypoints/alpha/scalars.js", | ||
"gen:docs:examples:clients:SocialStudies": "pnpm graffle --name SocialStudies --schema https://countries.trevorblades.com/graphql --output ./examples/generated-clients/SocialStudies --libraryPathClient ../../../src/entrypoints/alpha/client.js --libraryPathSchema ../../../src/entrypoints/alpha/schema.js --libraryPathScalars ../../../src/entrypoints/alpha/scalars.js", | ||
"demo": "tsx src/cli/generateSchema.ts && dprint fmt src/demo.ts", | ||
@@ -65,0 +65,0 @@ "dev": "rm -rf dist && tsc --watch", |
@@ -6,3 +6,3 @@ import type { DocumentNode, ExecutionResult, GraphQLSchema } from 'graphql' | ||
import { parseExecutionResult } from '../../lib/graphqlHTTP.js' | ||
import { CONTENT_TYPE_GQL, CONTENT_TYPE_JSON } from '../../lib/http.js' | ||
import { CONTENT_TYPE_GQL, CONTENT_TYPE_JSON, mergeHeadersInit } from '../../lib/http.js' | ||
import { casesExhausted } from '../../lib/prelude.js' | ||
@@ -47,2 +47,5 @@ import { execute } from '../0_functions/execute.js' | ||
transport: TransportHttp | ||
transportConstructorConfig: { | ||
headers?: HeadersInit | ||
} | ||
} & HttpProperties) | ||
@@ -264,6 +267,10 @@ | ({ | ||
case `http`: { | ||
const headers = mergeHeadersInit( | ||
input.transportConstructorConfig.headers ?? {}, | ||
input.request.headers ?? {}, | ||
) | ||
const response = await slots.fetch( | ||
new Request(input.request.url, { | ||
method: input.request.method, | ||
headers: input.request.headers, | ||
headers, | ||
body: input.request.body, | ||
@@ -270,0 +277,0 @@ }), |
@@ -8,6 +8,6 @@ import { describe, expect, expectTypeOf } from 'vitest' | ||
const schemaUrl = new URL(`https://foo.io/api/graphql`) | ||
const endpoint = new URL(`https://foo.io/api/graphql`) | ||
describe(`without schemaIndex only raw is available`, () => { | ||
const graffle = Graffle.create({ schema: schemaUrl }) | ||
const graffle = Graffle.create({ schema: endpoint }) | ||
@@ -33,2 +33,10 @@ test(`unavailable methods`, () => { | ||
describe(`http`, () => { | ||
test(`can set headers in constructor`, async ({ fetch }) => { | ||
fetch.mockImplementationOnce(() => Promise.resolve(createResponse({ data: { id: `abc` } }))) | ||
const graffle = Graffle.create({ schema: endpoint, headers: { 'x-foo': `bar` } }) | ||
await graffle.raw(`query { id }`) | ||
const request = fetch.mock.calls[0]?.[0] | ||
expect(request?.headers.get(`x-foo`)).toEqual(`bar`) | ||
}) | ||
test(`sends well formed request`, async ({ fetch, graffle }) => { | ||
@@ -42,2 +50,9 @@ fetch.mockImplementationOnce(() => Promise.resolve(createResponse({ data: { greetings: `Hello World` } }))) | ||
}) | ||
describe(`memory`, () => { | ||
test(`cannot set headers in constructor`, () => { | ||
// todo: This error is poor for the user. It refers to schema not being a URL. The better message would be that headers is not allowed with memory transport. | ||
// @ts-expect-error headers not allowed with GraphQL schema | ||
Graffle.create({ schema, headers: { 'x-foo': `bar` } }) | ||
}) | ||
}) | ||
}) | ||
@@ -48,3 +63,3 @@ | ||
fetch.mockImplementationOnce(() => Promise.resolve(createResponse({ data: { id: `abc` } }))) | ||
const graffle = Graffle2.create({ schema: schemaUrl, output: { envelope: true } }) | ||
const graffle = Graffle2.create({ schema: endpoint, output: { envelope: true } }) | ||
const result = await graffle.query.id() | ||
@@ -51,0 +66,0 @@ expectTypeOf(result.response).toEqualTypeOf<Response>() |
@@ -186,2 +186,5 @@ import { type ExecutionResult, GraphQLSchema } from 'graphql' | ||
schema: input.schema, | ||
transportConstructorConfig: { | ||
headers: input.headers, | ||
}, | ||
context: { | ||
@@ -278,2 +281,5 @@ config: context.config, | ||
transport, | ||
transportConstructorConfig: { | ||
headers: input.headers, | ||
}, | ||
document: rawInput.document, | ||
@@ -280,0 +286,0 @@ schema: input.schema, |
@@ -47,3 +47,3 @@ import type { GraphQLSchema } from 'graphql' | ||
/** | ||
* Headers to send with the request. | ||
* Headers to send with each sent request. | ||
*/ | ||
@@ -50,0 +50,0 @@ headers?: HeadersInit |
@@ -8,1 +8,10 @@ export const ACCEPT_HEADER = `Accept` | ||
} | ||
export const mergeHeadersInit = (headers: HeadersInit, additionalHeaders: HeadersInit) => { | ||
const base = new Headers(headers) | ||
const additional = new Headers(additionalHeaders) | ||
for (const [key, value] of additional.entries()) { | ||
base.set(key, value) | ||
} | ||
return base | ||
} |
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
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
968925
15097