0

Imagine that I have this "Schema":

type User {
    id: ID!
    name: String!
}

Now, in one of my components (A) I need the user id; and in another component (B) I need the user id and the user name.

What I'm doing now is to make a User query in A Component requesting only the ID. And in B Component I'm making another User query requesting id and name.

There's a piece of the query that's always the same:

// Component A request
const query = gql`{
    User(id: 1) {
        id
    }
}`
// Component B request
const query = gql`{
    User(id: 1) {
        id
        name
    }
}`

That User(id:1){} is the same and what is suggested to change is the response parameters. What is the correct approach to this? Is correct to duplicate that fixed part of the query?

Our problem is that if in the future the Schema suffers a change, we gonna need to refactor all the queries in all the places they are. What we want is a way to centralize it and parameterize it in order to be scalable in response of Schema changes.

One of my teammates has made a function that receives a string and places it inside the GQL query. I don't like that approach. What do you think?

Daniel Heras
  • 33
  • 1
  • 5
  • [Use fragments](https://www.apollographql.com/docs/angular/features/fragments/) – Herku Jul 16 '19 at 17:01
  • Fragments does not resolve my problem – Daniel Heras Jul 17 '19 at 15:58
  • Hmm, maybe I don't understand your problem then. You seem to be describing a problem that you don't have yet. Dynamic GraphQL queries are usually not the solution... – Herku Jul 17 '19 at 20:02
  • I dont want to wait for the problem to come in order to solve it, im anticipating to it. What i have are various queries with same targets but diferent response fields. What i dont want is that if in the future one of the required params o something of the query change i need to change all the affected queries, im searching for a scalable solution. – Daniel Heras Jul 18 '19 at 07:35

0 Answers0