Skip to content

Commit

Permalink
fix: ensure code formatting is correct (#604)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonkoops authored Oct 29, 2023
1 parent 8c99d80 commit 3b7b31f
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const document = gql`
}
}
`
const endpoint = 'https://api.spacex.land/graphql/';
const endpoint = 'https://api.spacex.land/graphql/'
const client = new GraphQLClient(endpoint)
await client.request(document)
```
Expand Down
12 changes: 5 additions & 7 deletions examples/configuration-request-json-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import JSONbig from 'json-bigint'

const jsonSerializer = JSONbig({ useNativeBigInt: true })
const graphQLClient = new GraphQLClient(`https://some-api`, { jsonSerializer })
const data = await graphQLClient.request<{ someBigInt: bigint }>(
gql`
{
someBigInt
}
`
)
const data = await graphQLClient.request<{ someBigInt: bigint }>(gql`
{
someBigInt
}
`)
console.log(typeof data.someBigInt) // if >MAX_SAFE_INTEGER then 'bigint' else 'number'
2 changes: 1 addition & 1 deletion examples/other-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const getAccessToken = () => Promise.resolve(`some special token here`)
console.error(
`[${traceId}] Request error:
status ${response.status}
details: ${response.errors.map((_) => _.message).join(`, `)}`
details: ${response.errors.map((_) => _.message).join(`, `)}`,
)
}
}
Expand Down
17 changes: 10 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const buildRequestConfig = <V extends Variables>(params: BuildRequestConfigParam
})
return acc
},
[]
[],
)

return `query=${encodeURIComponent(params_.jsonSerializer.stringify(payload))}`
Expand Down Expand Up @@ -179,7 +179,10 @@ const createHttpMethodFetcher =
* GraphQL Client.
*/
class GraphQLClient {
constructor(private url: string, public readonly requestConfig: RequestConfig = {}) {}
constructor(
private url: string,
public readonly requestConfig: RequestConfig = {},
) {}

/**
* Send a GraphQL query to the server.
Expand Down Expand Up @@ -422,7 +425,7 @@ const makeRequest = async <T = unknown, V extends Variables = Variables>(params:
throw new ClientError(
// @ts-expect-error TODO
{ ...errorResult, status: response.status, headers: response.headers },
{ query, variables }
{ query, variables },
)
}
}
Expand Down Expand Up @@ -586,7 +589,7 @@ const createRequestBody = (
query: string | string[],
variables?: Variables | Variables[],
operationName?: string,
jsonSerializer?: JsonSerializer
jsonSerializer?: JsonSerializer,
): string => {
const jsonSerializer_ = jsonSerializer ?? defaultJsonSerializer
if (!Array.isArray(query)) {
Expand All @@ -603,15 +606,15 @@ const createRequestBody = (
acc.push({ query: currentQuery, variables: variables ? variables[index] : undefined })
return acc
},
[]
[],
)

return jsonSerializer_.stringify(payload)
}

const getResult = async (
response: Response,
jsonSerializer: JsonSerializer
jsonSerializer: JsonSerializer,
): Promise<
| { data: object; errors: undefined }[]
| { data: object; errors: undefined }
Expand Down Expand Up @@ -659,7 +662,7 @@ const callOrIdentity = <T>(value: MaybeLazy<T>) => {
export const gql = (chunks: TemplateStringsArray, ...variables: unknown[]): string => {
return chunks.reduce(
(acc, chunk, index) => `${acc}${chunk}${index in variables ? String(variables[index]) : ``}`,
``
``,
)
}

Expand Down
6 changes: 3 additions & 3 deletions src/parseArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
export const parseRequestArgs = <V extends Variables = Variables>(
documentOrOptions: RequestDocument | RequestOptions<V>,
variables?: V,
requestHeaders?: GraphQLClientRequestHeaders
requestHeaders?: GraphQLClientRequestHeaders,
): RequestOptions<V> => {
return (documentOrOptions as RequestOptions<V>).document
? (documentOrOptions as RequestOptions<V>)
Expand All @@ -29,7 +29,7 @@ export const parseRequestArgs = <V extends Variables = Variables>(
export const parseRawRequestArgs = <V extends Variables = Variables>(
queryOrOptions: string | RawRequestOptions<V>,
variables?: V,
requestHeaders?: GraphQLClientRequestHeaders
requestHeaders?: GraphQLClientRequestHeaders,
): RawRequestOptions<V> => {
return (queryOrOptions as RawRequestOptions<V>).query
? (queryOrOptions as RawRequestOptions<V>)
Expand All @@ -43,7 +43,7 @@ export const parseRawRequestArgs = <V extends Variables = Variables>(

export const parseBatchRequestArgs = <V extends Variables = Variables>(
documentsOrOptions: BatchRequestDocument<V>[] | BatchRequestsOptions<V>,
requestHeaders?: GraphQLClientRequestHeaders
requestHeaders?: GraphQLClientRequestHeaders,
): BatchRequestsOptions<V> => {
return (documentsOrOptions as BatchRequestsOptions<V>).documents
? (documentsOrOptions as BatchRequestsOptions<V>)
Expand Down
6 changes: 3 additions & 3 deletions src/resolveRequestDocument.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { RequestDocument } from './types.js'
/**
* Refactored imports from `graphql` to be more specific, this helps import only the required files (100KiB)
* Refactored imports from `graphql` to be more specific, this helps import only the required files (100KiB)
* instead of the entire package (>500KiB) where tree-shaking is not supported.
* @see https://github.com/jasonkuhrt/graphql-request/pull/543
*/
Expand All @@ -16,7 +16,7 @@ const extractOperationName = (document: DocumentNode): string | undefined => {
let operationName = undefined

const operationDefinitions = document.definitions.filter(
(definition) => definition.kind === `OperationDefinition`
(definition) => definition.kind === `OperationDefinition`,
) as OperationDefinitionNode[]

if (operationDefinitions.length === 1) {
Expand All @@ -27,7 +27,7 @@ const extractOperationName = (document: DocumentNode): string | undefined => {
}

export const resolveRequestDocument = (
document: RequestDocument
document: RequestDocument,
): { query: string; operationName?: string } => {
if (typeof document === `string`) {
let operationName = undefined
Expand Down
2 changes: 1 addition & 1 deletion tests/__helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type MockResult<Spec extends MockSpec | MockSpecBatch = MockSpec> = {
}

export const setupMockServer = <T extends MockSpec | MockSpecBatch = MockSpec>(
delay?: number
delay?: number,
): Context<T> => {
const ctx = {} as Context<T>
beforeAll(async () => {
Expand Down
6 changes: 3 additions & 3 deletions tests/batching.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test(`basic error`, async () => {
})

await expect(batchRequests(mockServer.url, [{ document: `x` }])).rejects.toMatchInlineSnapshot(
`[Error: GraphQL Error (Code: 200): {"response":{"0":{"errors":{"message":"Syntax Error GraphQL request (1:1) Unexpected Name \\"x\\"\\n\\n1: x\\n ^\\n","locations":[{"line":1,"column":1}]}},"status":200,"headers":{}},"request":{"query":["x"],"variables":[null]}}]`
`[Error: GraphQL Error (Code: 200): {"response":{"0":{"errors":{"message":"Syntax Error GraphQL request (1:1) Unexpected Name \\"x\\"\\n\\n1: x\\n ^\\n","locations":[{"line":1,"column":1}]}},"status":200,"headers":{}},"request":{"query":["x"],"variables":[null]}}]`,
)
})

Expand All @@ -61,9 +61,9 @@ test(`successful query with another which make an error`, async () => {
})

await expect(
batchRequests(mockServer.url, [{ document: `{ me { id } }` }, { document: `x` }])
batchRequests(mockServer.url, [{ document: `{ me { id } }` }, { document: `x` }]),
).rejects.toMatchInlineSnapshot(
`[Error: GraphQL Error (Code: 200): {"response":{"0":{"data":{"me":{"id":"some-id"}}},"1":{"errors":{"message":"Syntax Error GraphQL request (1:1) Unexpected Name \\"x\\"\\n\\n1: x\\n ^\\n","locations":[{"line":1,"column":1}]}},"status":200,"headers":{}},"request":{"query":["{ me { id } }","x"],"variables":[null,null]}}]`
`[Error: GraphQL Error (Code: 200): {"response":{"0":{"data":{"me":{"id":"some-id"}}},"1":{"errors":{"message":"Syntax Error GraphQL request (1:1) Unexpected Name \\"x\\"\\n\\n1: x\\n ^\\n","locations":[{"line":1,"column":1}]}},"status":200,"headers":{}},"request":{"query":["{ me { id } }","x"],"variables":[null,null]}}]`,
)
})

Expand Down
2 changes: 1 addition & 1 deletion tests/document-node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ it(`accepts graphql DocumentNode as alternative to raw string`, async () => {
users
}
}
`
`,
)
expect(mock).toMatchSnapshot()
})
8 changes: 4 additions & 4 deletions tests/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ test(`basic error`, async () => {
const res = await request(ctx.url, `x`).catch((x) => x)

expect(res).toMatchInlineSnapshot(
`[Error: GraphQL Error (Code: 200): {"response":{"errors":{"message":"Syntax Error GraphQL request (1:1) Unexpected Name \\"x\\"\\n\\n1: x\\n ^\\n","locations":[{"line":1,"column":1}]},"status":200,"headers":{}},"request":{"query":"x"}}]`
`[Error: GraphQL Error (Code: 200): {"response":{"errors":{"message":"Syntax Error GraphQL request (1:1) Unexpected Name \\"x\\"\\n\\n1: x\\n ^\\n","locations":[{"line":1,"column":1}]},"status":200,"headers":{}},"request":{"query":"x"}}]`,
)
})

Expand All @@ -159,7 +159,7 @@ test(`basic error with raw request`, async () => {
})
const res: unknown = await rawRequest(ctx.url, `x`).catch((x) => x)
expect(res).toMatchInlineSnapshot(
`[Error: GraphQL Error (Code: 200): {"response":{"errors":{"message":"Syntax Error GraphQL request (1:1) Unexpected Name \\"x\\"\\n\\n1: x\\n ^\\n","locations":[{"line":1,"column":1}]},"status":200,"headers":{}},"request":{"query":"x"}}]`
`[Error: GraphQL Error (Code: 200): {"response":{"errors":{"message":"Syntax Error GraphQL request (1:1) Unexpected Name \\"x\\"\\n\\n1: x\\n ^\\n","locations":[{"line":1,"column":1}]},"status":200,"headers":{}},"request":{"query":"x"}}]`,
)
})

Expand Down Expand Up @@ -370,7 +370,7 @@ describe(`operationName parsing`, () => {
query myGqlOperation {
users
}
`
`,
)

const requestBody = mock.requests[0]?.body
Expand All @@ -385,7 +385,7 @@ describe(`operationName parsing`, () => {
query myStringOperation {
users
}
`
`,
)

const requestBody = mock.requests[0]?.body
Expand Down
2 changes: 1 addition & 1 deletion tests/gql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe(`gql`, () => {
query allUsers {
users
}
`
`,
)
expect(mock).toMatchSnapshot()
})
Expand Down
14 changes: 6 additions & 8 deletions tests/graphql-ws.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,11 @@ afterAll(() => {

test(`graphql-ws request`, async () => {
const client = await createClient(ctx.url)
const data = client.request(
gql`
query hello {
hello
}
`
)
const data = client.request(gql`
query hello {
hello
}
`)
expect(await data).toEqual({ hello: `world` })
client.close()
})
Expand All @@ -84,7 +82,7 @@ test(`graphql-ws subscription`, async () => {
complete: () => {
resolve(allGreetings)
},
}
},
)
})
expect(await result).toEqual(`Hi,Bonjour,Hola,Ciao,Zdravo`)
Expand Down

0 comments on commit 3b7b31f

Please sign in to comment.