Jonathan.
Jonathan.

Reputation: 55544

Graphql include directive

I have a query that takes a status as an input variable which is an enum of (future, past, all).

I'd like to return some fields only when the status is future. I've tried using @include but it seems it will only accept a boolean exactly.

Is there some way of getting this to work:

aField @include(if: $status == future)

Upvotes: 3

Views: 2671

Answers (2)

freakomonk
freakomonk

Reputation: 614

variables is a JSON. So, you can also try passing the status : inputStatus==='FUTURE' ? true : false

Upvotes: 0

xs0
xs0

Reputation: 2897

I think you can't use even use boolean fields with @include - literals/query variables only, so expressions are a no-go as well.

If it's really important, and you're able to modify the API/schema, you could (ab)use the interface functionality - have future entities resolve to one type, and past entities to another, then you can use fragments to select fields based on which it is:

interface Competition {
    id: ID!
    name: String!
    status: Status!
    something: String
}

type FutureCompetition implements Competition {
    // same fields
}

type PastCompetition implements Competition {
    // same fields
}

Then you can query with something like this:

competitions {
    id
    name
    status
    ... on FutureCompetition {
        something
    }
}

A possibly easier thing to do would be to simply do two queries in one and merge the results client-side, assuming you can query by status:

pastCompetitions: competitions(withStatus: PAST) {
    id
    name
    status
}

futureCompetitions: competitions(withStatus: FUTURE) {
    id
    name
    status
    something
}

Upvotes: 2

Related Questions