chx
chx

Reputation: 11760

GraphQL filtering on an embedded ID

I have a custom schema I need to work with, here's a slightly abbreviated version:

schema {
  query: Query
}

type Query {
  environment(locale: LocaleEnum!): Environment
}

type Environment {
  articles(
    offset: Int = 0
    limit: Int = 20
  ): ArticleConnection!
}

type ArticleConnection {
  total: Int!
  items: [Article!]
}

type Article {
  id: ID!
  title: String!
}

I was able to get a list of article IDs by running

query {
  environment(locale: EN) {
    articles {
      total,
      items {
        id
      }
    }
  }
}

Is it possible to filter on the article id? Like, I just want a single, specific article.

Upvotes: 0

Views: 728

Answers (1)

Daniel Rearden
Daniel Rearden

Reputation: 84687

Clients can manipulate the "shape" of a GraphQL service's response in one of three ways:

  • By providing different selection sets (i.e. choosing different fields to query)
  • By utilizing inline fragments, which allow a specific selection set to be applied to a field based on the actual type of the field at runtime
  • By utilizing the @skip and @include directives to explicitly exclude or include an individual field from the selection set.

Outside of the above, GraphQL does not provide any built-in mechanisms for filtering, sorting, pagination or other arbitrary transformations of the response. These features are service-specific and have to implemented as part of your schema, which represents the collective abilities of your service.

For example, you might add another field to your Environment type like:

type Environment {
  article(
    id: ID!
  ): Article
}

If you're developing a client that uses an existing schema, there's not much you can do if the schema doesn't expose the functionality you need.

Upvotes: 1

Related Questions