I'm wondering how to use both an input type and one of its fields as arguments in the same GraphQL query. I think there are a few valid solutions, but I'm wondering which (if any) are best practice.
Consider the following hypothetical query. We get players by location and status, and team members who are in the same location, but the member field only has a location argument:
input PlayerInput {
location: String!
status: Int!
}
query getPlayers($playerInput: PlayerInput) {
players(playerInput: $playerInput) {
name
team {
name
members(location: ???) { // <-- How to access playerInput.location?
name
}
}
}
}
I can think of a few ways to solve this:
1. Change the query to take individual arguments
query getPlayers($location: String!, $status: Int!) {
players(playerInput: { location: $location, status: $status }) {
name
team {
name
members(location: $location) {
name
}
}
}
}
2. Update the schema so members takes the right input type
query getPlayers($playerInput: PlayerInput) {
players(playerInput: $playerInput) {
name
team {
name
members(playerInput: $playerInput) { // <-- Requires changing schema
name
}
}
}
}
This doesn't seem great for a few reasons, and would only work if you have the ability to update the schema.
3. Pass location in as a redundant individual argument
query getPlayers($playerInput: PlayerInput, $location: String!) {
players(playerInput: $playerInput) {
name
team {
name
members(location: $location) {
name
}
}
}
}
This seems fine, just that there's some duplication when creating the query:
const location = 'US';
const status = 1;
fetch({
query: getPlayersQuery,
variables: {
location,
playerInput: {
location,
status,
}
}
})...
Are any of these the preferred way of doing something like this? Are there other ways I haven't considered?