Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Field aliases #23

Open
komakino opened this issue Jun 4, 2020 · 3 comments
Open

Feature request: Field aliases #23

komakino opened this issue Jun 4, 2020 · 3 comments

Comments

@komakino
Copy link

komakino commented Jun 4, 2020

Support for aliased fields would be great. The natural format would of course be to set the alias as the property value instead of true.

const query = {
  query: {
    fruits: {
      name: true,
      colour: 'color',
      flavour: 'flavor'
    }
  }
}

should result in:

{
  query: {
    fruits {
      name
      colour: 'color'
      flavour: 'flavor'
    }
  }
}

I do realize however that that would introduce breaking changes for people using truthy values.

@dupski
Copy link
Collaborator

dupski commented Jun 6, 2020

We support aliased queries via __aliasFor. One option could be to support this lower down the tree too, e.g.:

const query = {
  query: {
    fruits: {
      name: true,
      colour: { __aliasFor: 'color'},
      flavour: { __aliasFor: 'flavor'}
    }
  }
}

becomes

{
  query: {
    fruits {
      name
      colour: color
      flavour: flavor
    }
  }
}

(I presume you meant without the quotes in the graphql)

Thoughts?

I guess this would need a new major release, to be safe :)

@notrab
Copy link

notrab commented Oct 14, 2020

Sounds great @dupski 🔥

Perhaps if a object is given, we can use new behaviour? So using aliases is opt-in, leaving current behaviour as is if you use a Boolean.

@james-wallis
Copy link

Hey, just gave the solution proposed by @dupski a go after checking the source. Unless I've misunderstood the initial issue, it looks like it already works?

For example the JS:

const query = {
    query: {
      __name: "SEARCH",
      __variables: {
        queryString: "String!",
      },
      search: {
        __args: {
          query: new VariableType("queryString"),
        },
        objects: {
          __on: {
            __typeName: "Episode",
            testTitle: {
              __aliasFor: "title",
            },
          },
        },
      },
    },
  };

  const graphQLQuery = jsonToGraphQLQuery(query);

Outputs this query:

query SEARCH($queryString: String!) {
  search(query: $queryString) {
    objects {
      ... on Episode {
        testTitle: title
      }
    }
  }
}

("json-to-graphql-query": "^2.2.4",)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants