graphql模块通过嵌套扩展类型def

时间:2019-08-23 22:38:32

标签: graphql apollo graphql-js apollo-server

我正在使用apollo模块,并且我有多个模块: UserProfile 学校项目

我具有以下类型定义:

UserProfile的typeDef:

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
  }
`;

为项目键入def:

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]
  }
`;

这是学校类型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
  }
`;

现在是否执行以下查询:

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

它抱怨不能:

  

“无法查询类型为“ UserProfile”的字段“ schoolInfo”。

此架构中缺少什么?

1 个答案:

答案 0 :(得分:2)

您已导入所有模块吗?

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

否则他们将无法进行交互。