admin管理员组

文章数量:1126451

I have that query in GraphQL

query getUserByName($name: String!) {
  user(name: $name) {
    id
    name
    email
  }
}

I want to get all users that contains Jon in name, not equal.

How can I modify the query to do contains please (without edit the server side)

I have that query in GraphQL

query getUserByName($name: String!) {
  user(name: $name) {
    id
    name
    email
  }
}

I want to get all users that contains Jon in name, not equal.

How can I modify the query to do contains please (without edit the server side)

Share Improve this question edited Jan 8 at 22:12 Barmar 780k56 gold badges542 silver badges656 bronze badges asked Jan 8 at 21:32 Polo1990Polo1990 1237 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

That will depend on the schema / API provided by the server. If filters according to the partial match for the name field are not supported there, you won't be able to do that solely via GraphQL.

Alternatively, what you can do is query all of the data and then perform the filtering on the client side. Just keep in mind that depending on the data volume, this approach might be problematic since it's not considered an efficient approach.

Unfortunately, the question you're asking is impossible to answer. It is possible that you misunderstand what GraphQL is, so I'll suggest: think of GraphQL like you would think of REST. This isn't the perfect analogy, but pretending it was, I'll rephrase your question using REST:

I have a REST API. I am making this curl request: curl -X GET "https://api.example.com/users?name=Jon"

I want to get all users that contains Jon in name, not equal.

How can I modify the request to do contains please (without edit the server side)

Obviously the answer to this question would be

I have no idea. The only person who can answer this is the person who created your REST API. If, however, they're using some library or framework on top of the REST, that particular framework may have pre-defined rules on how to do what you're asking. Unfortunately, 'it is REST' means very little, because literally anything could be happening behind the scenes, as long as it has the properties of a REST API (client-server architecture, stateless operations, cacheability, a uniform interface, layered system, and optionally, code-on-demand).


GraphQL is a query language for an API (like REST), and an API can do ANYTHING. It is not a query language for data (like SQL).

As a deeper example, from what you've defined here, what follows are a valid GraphQL Schema and a valid JavaScript resolver of that data:

type Anxiety {
  id: Float
  name: Boolean
  email: Int
}

type Query {
  user(name: String): Anxiety
}
const resolvers = {
  Query: {
    user: () => {
      return { id: Math.PI, name: false, email: 5987319 };
    }
  }
}

Note that from this code, it is impossible to do what you're asking for. I don't have any functionality that looks anything up in any database. I don't look at the arguments you passed in. I'm not even returning a "User"; it's an "Anxiety", and the field types aren't exactly what a person would expect them to be from their name.

Obviously this is an absurd example, but the fact that this is all "correct from the standpoint of GraphQL" proves my point I think. GraphQL can do literally anything behind the scenes, as long as the input is in the format defined in the schema and the response is in the format defined in the schema.

本文标签: databaseHow to use contain in GraphQL queryStack Overflow