【问题标题】:GraphQL nested query definitionGraphQL 嵌套查询定义
【发布时间】:2017-06-16 16:01:04
【问题描述】:

我正在尝试为我的查询创建树状结构,以摆脱像

这样的查询
peopleList, peopleSingle, peopleEdit, peopleAdd, peopleDelete companyList, companySingle, companyEdit, companyAdd, companyDelete etc. 

最后我想发送这样的查询:

query test {
  people {
    list {
      id
      name
    }
    single(id: 123) {
      id
      name
    }
  }
  company {
    list {
      id
      name
    }
    single(id: 456) {
      id
      name
    }
  }
}

mutation test2 {
  people {
    create(data: $var) {
      id
      name
    }
  }
  people {
    edit(id: 123, data: $var) {
      id
      name
    }
  }
}

这是我在人员模块上的查询对象的一部分:

people: {
  type: //What type this should be?
  name: 'Root of People queries',
  fields: () => ({
    list: {
      type: peopleType,
      description: 'Returns all people in DB.',
      resolve: () => {
        // resolve method implementation
      }
    },
    single: {
      type: peopleType,
      description: 'Single row from people table. Requires ID argument.',
      args: {
        id: { type: new GraphQLNonNull(GraphQLID) }
      },
      resolve: () => {
        // resolve method implementation
      }
    }
  })
}

我曾尝试将此 sn-p 放入 GraphQLObjectType,然后在 RootQuery 中将它们组合在一起(再次使用 GraphQLObjectType) - 没有用。

另一种方法可以是创建新类型——比如 peopleQueriesType,在这个类型中将我的所有查询指定为字段,然后为这个对象创建单个查询。但这对我来说似乎很奇怪 - 用不必要的对象污染我的代码只是为了将我的查询合并为树状形状。

我尝试查看 Apollo 服务器实现,是否可以执行这种查询结构,但在文档中找不到任何帮助。

我在我的服务器上使用 node.js + express + graphql-js。

【问题讨论】:

    标签: javascript schema graphql graphql-js


    【解决方案1】:

    简答:
    类型应该是一个 GraphQLObjectType 包含所有这样的字段:

    type: new GraphQLObjectType({ name: 'patientQuery', fields: { find, findOne } })
    

    详细信息:我使用以下代码完成了此查询:

    {
      patient {
        find {
          id
          active
        }
        findOne(id: "pat3") {
          id
          active
        }
      }
    }
    

    patient/queries/index.js我有这个

    import findOne from './find-one.js';
    import find from './find.js';
    import { GraphQLObjectType } from 'graphql';
    
    export default {
      patient: {
        type: new GraphQLObjectType({ name: 'patientQuery', fields: { find, findOne } }),
        resolve(root, params, context, ast) {
          return true;
        }
      }
    };
    

    然后在queries.js

    import patient from './patient/queries/index.js';
    export default {
      ...patient
    };
    

    最后是我的架构 schema.js 传递给 graphql express 服务器

    import {
      GraphQLObjectType,
      GraphQLSchema
    } from 'graphql';
    
    import queries from './queries';
    import mutations from './mutations';
    
    export default new GraphQLSchema({
      query: new GraphQLObjectType({
        name: 'Query',
        fields: queries
      }),
      mutation: new GraphQLObjectType({
        name: 'Mutation',
        fields: mutations
      })
    });
    

    【讨论】:

    • 在对象基础解析器中返回 true 是我忽略的。很好的帮助!
    猜你喜欢
    • 2021-01-29
    • 2020-12-02
    • 1970-01-01
    • 2018-06-13
    • 2019-05-30
    • 2021-01-14
    • 2017-07-17
    • 1970-01-01
    • 2017-01-30
    相关资源
    最近更新 更多