[GH-ISSUE #48] Aliases #122

Closed
opened 2026-03-15 11:33:50 +03:00 by kerem · 3 comments
Owner

Originally created by @seahindeniz on GitHub (Sep 15, 2020).
Original GitHub issue: https://github.com/atulmy/gql-query-builder/issues/48

Hi!

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

Originally created by @seahindeniz on GitHub (Sep 15, 2020). Original GitHub issue: https://github.com/atulmy/gql-query-builder/issues/48 Hi! Is there any support or examples for query aliases? https://graphql.org/learn/queries/#aliases
kerem 2026-03-15 11:33:50 +03:00
Author
Owner

@Devorein commented on GitHub (Sep 24, 2020):

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

<!-- gh-comment-id:698344663 --> @Devorein commented on GitHub (Sep 24, 2020): Nope not at the moment but thank you that is definitely a feature that should be included.
Author
Owner

@nicholasohrn commented on GitHub (Jan 19, 2021):

@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.

<!-- gh-comment-id:762530333 --> @nicholasohrn commented on GitHub (Jan 19, 2021): @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.
Author
Owner

@leeroyrose commented on GitHub (Jul 20, 2022):

I have opened a PR to solve this issue: https://github.com/atulmy/gql-query-builder/pull/71

<!-- gh-comment-id:1190709776 --> @leeroyrose commented on GitHub (Jul 20, 2022): I have opened a PR to solve this issue: https://github.com/atulmy/gql-query-builder/pull/71
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/gql-query-builder#122
No description provided.