Search code examples
graphqlgraphql-javagithub-graphqlgraphql-spring-boot

graphql Validation error of type FieldsConflict coming when alias is not used


I am getting the error "Validation error of type FieldsConflict" when i am not using aliases in my request. kindly confirm if this is expected or if there is a workaround

{
    person(search: [{firstname: "DAN", lastname: "WATLER", country: "FRANCE"}])
    { 
        firstname
        lastname
        country
        age
        address      
    }

    person(search: [{firstname: "FRANK", lastname: "TEE", country: "FRANCE"}])
    { 
        firstname
        lastname
        country
        age
        address      
    }
}

Above code gives validation error but if I use aliases shown below, error doesn't come and I get successful response.

I dont want to use aliases, please suggest if any workaround. Thanks !

{
    dan: person(search: [{firstname: "DAN", lastname: "WATLER", country: "FRANCE"}])
    { 
        firstname
        lastname
        country
        age
        address      
    }

    frank: person(search: [{firstname: "FRANK", lastname: "TEE", country: "FRANCE"}])
    { 
        firstname
        lastname
        country
        age
        address      
    }
}

Solution

  • Usually GraphQL returns data as JSON objects, and it is not possible to have 2 (valid) objects in a JSON document with the same key (in your case person). Hence, it is pretty impossible to achieve what you are describing.

    The result of your first query would be something like:

    {
      "data": {
        "person": {
          "firstname": "DAN",
          ...
        },
        "person": { // this is not valid
          "firstname": "FRANK"
          ...
        }
      }
    }
    

    And that is why you have to use alias.

    Another option would be to see if the GraphQL server has a query that returns a list of person and the result would be inside of an array, like:

    {
      "data": [
        {
          "firstname": "DAN",
          ...
        },
        {
          "firstname": "FRANK",
          ...
        }
      [
    }