lynxtaa / awesome-graphql-client

GraphQL Client with file upload support for NodeJS and browser
https://npm.im/awesome-graphql-client
MIT License
59 stars 7 forks source link

Added ability to override how operation is serialized #89

Open onionhammer opened 11 months ago

onionhammer commented 11 months ago

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
            });
        },
    })