teebeetee
teebeetee

Reputation: 549

graphql modules extend type defs by nesting

I am using apollo modules, and I have multiple modules: UserProfile, School, Project

i have the following type definitions:

typeDef for UserProfile:

export const typeDefs = gql`
  extend type Query {
    getUserProfile(uuid: String!): UserProfile
  }

  type UserProfile {
    id: ID!
    email: String,
    uuid: String,
    firstName: String,
    lastName: String,
    school: String
  }

  extend type Project {
    userInfo: UserProfile
  }
`;

type def for Project:

export const typeDefs = gql`
  extend type Query {
    getProject(uuid: String!): Project
  }

  type Project {
    _id: ID!
    user: String,
    name: String,
    uuid: String
  }

  extend type UserProfile {
    userProjects: [Project]
  }
`;

and here is the School type def:

export const typeDefs = gql`
  extend type Query {
    getSchool(uuid: String!): School
  }

  type School {
    _id: ID!
    uuid: String,
    name: String,
  }

  extend type UserProfile {
    schoolInfo: School
  }
`;

now if i do the following query:

query {
  getProject(uuid: "a9e2f2ea") {
    name
    uuid
    ownerInfo {
      email
      gender
    }
    userInfo {
      lastName
      school
      schoolInfo
    }
  }
}

it is complaining that it cannot :

"Cannot query field \"schoolInfo\" on type \"UserProfile\".

what is missing in this schema?

Upvotes: 1

Views: 560

Answers (1)

Marcel
Marcel

Reputation: 2794

Have you imported all your modules?

const server = new ApolloServer({
        modules: [
            require('./modules/userProfile'),
            require('./modules/project'),
            require('./modules/school'),
        ],
        context: ...
});

otherwise they won't be able to interact.

Upvotes: 2

Related Questions