awesome-graphql-client icon indicating copy to clipboard operation
awesome-graphql-client copied to clipboard

Added ability to override how operation is serialized

Open onionhammer opened this issue 1 year ago • 0 comments

Can be used to address #88

Example:

Bit ugly, but this at least makes using persisted queries possible:

	const client = new AwesomeGraphQLClient<string, RequestInit>({
		endpoint: server.endpoint,
		formatOperation: operation => {
			return JSON.stringify({
                                 ...operation,
				query: undefined,
				id: operation.query // <- pass the id as the argument to client.request instead of the raw graphql
			});
		},
	})

You could also institute logic in this callback to print() the document or pass the id if it exists on an object:

	const client = new AwesomeGraphQLClient<string, RequestInit>({
		endpoint: server.endpoint,
		formatOperation: operation => {
			const query = operation.query;

			return JSON.stringify({
                                 ...operation,
				query: "id" in query ? undefined : query,
				id: "id" in query ? query.id : undefined
			});
		},
	})

onionhammer avatar Dec 28 '23 20:12 onionhammer