Search code examples
graphqlapollo-servergraphql-js

GraphQL: relationships in not normalizable


I am new to GraphQL not sure what I am doing wrong. Any help will be appreciated. I have included my GraphQL schema, data and query here. I am getting exception in query "Value to set in Organization.relationships in not normalizable: Value to set in Relationships.customers in not normalizable: should be an object or null or undefined".

My Schema:

type Query{
     searchForOrganization: [Organization!]!
}

type Data{
    id: ID
    type: String
}

type Group{
  data:[Data]
}

type User{
    data:[Data]
}

type Customer{
    data:[Data]
}

type Relationships{
    groups: Group
    users: User
    customers: Customer
}

type Attributes{
    name: String!
    description: String
    authenticationUrl: String
}

type Organization{
    id: ID!
    type: String!
    attributes: Attributes!
    relationships: Relationships!

}

Mock Data:

const mocks = {
    Query: () => ({
      searchForOrganization: () => [...new Array(9)],
    }),
    Organization: () => (
        {
            type: () => "organization",
            id: () =>"Test002",
            attributes: ()=>{
                return{
                authenticationUrl: "XXXX",
                description: "Test Company",
                "name": "Test Company"
                };
            },
            relationships: () => {
                return{
                    customers: () => {
                        return{
                       data: [{type: "customer", id: "Test002_Root_customers"},
                                {type: "customer", id: "Test003_Root_customers"}
                            ]
                    };},
                    groups: () => {
                        return{
                        data: [{type: "group", id: "Test003_Root_groups"},
                            {type: "group", id: "Test002_Root_groups"}]
                        };},
                    users: () => {
                        return{
                        data: [{type: "user", id: "Test003_Root_users"},
                        {type: "user", id: "Test002_Root_users"}]
                        };}
                };
            }
          }
),
};

My Query:

query SearchForOrganization {
  searchForOrganization {
    id
    type
    attributes {
      name
      description
      authenticationUrl
    }
    relationships {
      groups {
        data {
          id
          type
        }
        
      }
      users {
        data {
          id
          type
        }
      }
      customers {
        data {
          id
          type
        }
      }
    }
  }
}

My Result:

{
  "errors": [
    {
      "message": "Value to set in Organization.relationships in not normalizable: Value to set in Relationships.customers in not normalizable: should be an object or null or undefined. Received () => {\r\n                        return{\r\n                       data: [{type: \"customer\", id: \"Test002_Root_customers\"},\r\n                                {type: \"customer\", id: \"Test003_Root_customers\"}\r\n                            ]\r\n                    };}",
      "locations": [
        {
          "line": 10,
          "column": 5
        }
      ],
      "path": [
        "searchForOrganization",
        0,
        "relationships"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "Error: Value to set in Organization.relationships in not normalizable: Value to set in Relationships.customers in not normalizable: should be an object or null or undefined. Received () => {\r",
            "                        return{\r",
            "                       data: [{type: \"customer\", id: \"Test002_Root_customers\"},\r",
            "                                {type: \"customer\", id: \"Test003_Root_customers\"}\r",
            "                            ]\r",
            "                    };}",


Solution

  • Change in schema solved the issue

    type TypeAndID{
        id: ID
        type: String
    }
    
    type DataGroup{
      data:[TypeAndID]
    }
    
    type OrgRelationships{
        groups: DataGroup
        users: DataGroup
        customers: DataGroup
    }
    
    type Attributes{
        name: String!
        description: String
        authenticationUrl: String
    }
    
    type Organization{
        id: ID!
        type: String!
        attributes: Attributes!
        relationships: OrgRelationships!
    
    }
    
    type Data{
     data: [Organization!]!
    }
    
    type Query{
       
        getOrganizations: Data
       
    }