atulmy/gql-query-builder

Aliases

Closed this issue · 3 comments

Hi!

Is there any support or examples for query aliases?
https://graphql.org/learn/queries/#aliases

Nope not at the moment but thank you that is definitely a feature that should be included.

@seahindeniz I had a similar question and found the easiest way around this was to prepend the desired alias to the operation name:

const query = gql.query([
    {
        operation: 'administrators: getUsers',
        fields: ['id', 'email', 'name'],
        variables: {
            role_1: {
                name: 'role',
                value: 'administrator',
            },
        },
    },
    {
        operation: 'public: getUsers',
        fields: ['id', 'email', 'name'],
        variables: {
            role_2: {
                name: 'role',
                value: 'public',
            },
        },
    },
]);

It ends up generating the following:

{
    query: "query ($role_1: String, $role_2: String) { administrators: getUsers (role: $role_1) { id, email, name } public: getUsers (role: $role_2) { id, email, name } }",
    variables: {
        "role_1": "administrator",
        "role_2": "public"
    }
}

This is obviously not the best possible solution and may fall over at any time, but it worked for me when I was trying to figure out how to batch queries with aliases while taking advantage of this library.

Thank you to @Devorein for putting this all together. I'll try to contribute back with real alias support at some point.

I have opened a PR to solve this issue: #71